Can we use Pthread_cond_signal() between between a thread and a child of same parent? - linux

I have a parent process and I have created a new thread from the parent process using pthread_create(). I have also created a child process 'C' from the parent Process using fork() and exited the parent process. Now the child is running as daemon.
Can I use pthread_cond_signal between child process and the thread? Or can pthread_cond_signal be used only between threads of the same process?

You can use condition variables across processes if you make the condition variable process-shared, using condition variable attribute configured with the pthread_condattr_setpshared function and a value of PTHREAD_PROCESS_SHARED. You will also have to make the associated mutex process-shared, using a mutex attribute configured with pthread_mutexattr_setpshared.
By default, condition variables and mutexes cannot be shared across processes.

Related

Kill parent and child thread by reading status of a variable from Child thread in python

My program takes input as a json file and runs various functions on the parameters by creating threads for each argument passed.Now i want to create sub-threads from these multiple parent threads which would monitor an alarm, if the alarm goes off the sub-thread and its corresponding parent thread should stop.
So the Child thread is an alarm monitoring thread if it sees the alarm is True it should kill the corresponding parent thread without affecting any other threads.
The flow is Flow of THREADS

Creating child processes in Linux

Processes who can create other processes (child processes) and meanwhile the following is correct:
a) The process has PID=0
b) Child process can continue to live independent
c) Process creator copies its context to the child processes
d) All of above are true
My opinion is that a) can't be because PID=0 is reserved, processor creator doesn't copy its context to child processes either, so I am thinking about b, because it can live independent but wouldn't it be called daemon then?? So is there are a correct (True) answer for this question?
Read about fork to know how child process is created. That might well answer your question
The fork() system call creates a clone/copy of the existing process. Now two processes are running, with the same data, location in the program, and process image. The difference is the return value of the fork() system call.
Since the fork() function returns different values to the parent (forking) process and the child (forked) process, you can determine whether you are running in the parent or child.
The parent (forking) process receives the result fork() == childpid when in the parent process, so the parent knows the process id of the child (and can thus kill it, wait for it to terminate, etc).
The parent could also receive the result fork() == -1, which indicates that the fork() system call failed.
The child (forked) process receives the result fork() == 0 when in the child process, and should you want to know the process id of the child, you would simply ask the current process id to get the process id of the child.
Here is a related post with code sample I posted, How to use fork().
The subtlety is that (a) is not true, because each of parent and child have a process id which is not =0, but the return from fork() is always =0 in the child. So it might appear that the manual is saying that the PID is =0. The part (b) is true. The part (c) is not true because it is not the process creator that copies anything, it is the system call (in the context of the parent process) which does the copying.

making parent process wait till child has called exec

In linux, after calling fork(), my child process is going to call exec soon. Is there a way for the parent process to wait() and not do anything till the child has exec'ed?
Thanks.
There is no (API) way for the parent to know that the child is performing an exec().
But there is a nice pipe-trick: have the child inherit a filedescriptor (for a pipe) and (before the fork() ) set the close-on-exec flag for the pipe. The parent will be notified by an EOF on the pipe when it is closed by the exec().
Please note that this does not need any collaboration from the child.
Use vfork() instead of fork(). That causes the parent to be suspended until the child either exits or calls one of the execve() family of functions.
You need to use waitpid using the process ID returned from the fork call that is returned to the parent.
EDIT
Or if you mean that you want to know that the child is about to call exec use pause in the parent. Get the child to call kill with a suitable signal to the parent (whose process ID can be obtained from getppid). USR1 signal might be useful to use. Do this just before the exec.

Set a guardian process for the child processes in case parent dies

There is a function in QNX procmgr_guardian which sets a child process as the guardian of the other child process in case of the parent's death.
Is there such functionality in Linux too ? How do I implement it in Linux? Any pointers are appreciated.
There is no direct method for monitoring a process that is not your own child. However, there is a hack you can use: Create a FIFO using pipe(); ensure that the process to be monitored holds the write end of the pipe (and that it is closed in all other processes). If the process dies, an EOF condition will be signalled in the read end of the pipe (ie, select will indicate a readable condition).
If you want the process to be reparented when its immediate parent dies, you may be able to achieve this with PID namespaces. By arranging for the 'guardian' process to be PID 1 in the PID namespace, it will inherit all orphaned processes in that namespace.

ChildThread inside a Parent Thread

Can you please help me in this:
How to create a child thread in a parent thread. And the parent thread should be terminated only after the child thread is terminated. and the parent thread is completed only after completing child thread.
Use NSCondition. Have the child thread signal the condition on termination. Have the parent wait on the condition. Add a predicate (a bool will do it) to work around the 'spurious signals' issues. See Using Conditions on the Threading Programming Guide.

Resources