how to capture the child process termination when the parent process is waiting the signal from child process - multiprocessing

now my parent process is suspended to wait the siguser1 from child process like this
sigset_t mask;
sigfillset(&mask);
sigdelset(&mask, SIGUSR1);
sigsuspend(&mask);
so my parent has been blocked.
but sometimes the child process will not send the siguser1 to the parent process but terminate first. So how can the parent process know the child process terminate already and not to wait the signal and do the next process.
what's more, i find that wait is also not working because it will block the parent process.

Related

Best way to wait for all child processes to complete in Ruby?

Looking for a way to wait for the completion of all child processes, I found this code:
while true
p "waiting for child processes"
begin
exited_pid = Process.waitpid(-1,Process::WNOHANG)
if exited_pid and exited_pid > 0 then
p "Process exited : #{exited_pid} with status #{$?.exitstatus }"
end
sleep 5
rescue SystemCallError
puts "All children collected!"
break
end
end
This looks like it works in a similar way to Unix-systems process management, as I read on tutorialspoint HERE.
So in summary, it looks like this code:
Calls Process.waitpid, for any child process that exists. If no child process has exited, continue anyway.
If a child process has exited, then notify the user. Otherwise sleep, and check again.
When all child processes have exited an error is thrown, which is caught and the user is notified that processes are complete.
But looking at a similar question on waiting for child processes in C (Make parent wait for all child processes), which has as an answer:
POSIX defines a function: wait(NULL);. It's shorthand for waitpid(-1,
NULL, 0);, which will block until all children processes exit.
I tested that Process.wait() in Ruby achieves pretty much the same thing as the more verbose code above.
What is the benefit of the more verbose code above? Or, which is considered a better approach to waiting for child processes? It seems in the verbose code that I would be able to wait for specific processes and listen for specific exit codes. But if I don't need to do this is there any benefit?
Also, regarding the more verbose code:
Why does the call to Process.waitpid() throw an error if there are no more child processes?
If more than 1 child process exists within the 5 second sleep period, it seems like there is a queue of completed processes and that Process.waitpid just returns the top member of the queue. What is actually happening here?

What does fork() returns to its parent when it is called in a child process of another process?

I guess it should not be zero.
EDIT: It is zero.
The PID of the child process is returned in the parent and 0 in the child upon success and -1 upon failure
A Fork() call always returns pid(process id) of the created child to its parent.
pid_t fork(void);

Windows: Notification when child dies (crash or exits normally)

I am trying to figure out when a child process (created with CreateProcess() on Windows) crashes or exits normally. I am porting Unix code to Windows and have a lot of issue with this.
I use a SIGCHLD on Unix, as such:
struct sigaction act;
bzero(&act, sizeof(act));
act.sa_handler = sig_handler;
act.sa_flags = SA_RESTART;
sigaction(SIGCHLD, &act, 0);
void sig_handler(int signal)
{
int pid, status;
if (signal == SIGCHLD) {
while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
// Remove pid from a data structure
}
}
}
I was looking at the solution of creating a new thread that calls WaitForMultipleObjects but the issue is that new processes can be created at any time, and I don't think I can interrupt WaitForMultipleObjects when a new process gets created. I don't want to spawn a new thread for each process that has WaitOnSingleObject (I'd prefer to pull in a single thread if I have to).
What is the best solution in this case please?
Actually there is a way to interrupt WaitForMultipleObjects(). Create a designated manual-reset event object using CreateEvent(). Include the event handle to WaitForMultipleObjects() along with your existing child process handles. Signal the event with SetEvent() each time a new child process is created. This way, WaitForMultipleObjects() exits whenever a child process exits (for whatever reason) or the event is signaled, then you can update the list of handles as needed, reset the event with ResetEvent(), and call WaitForMultipleObjects() again.

How can I get the PID of a new process before it executes?

So that I can do some injecting and interposing using the inject_and_interpose code, I need to way to get the PID of a newly-launched process (a typical closed-source user application) before it actually executes.
To be clear, I need to do better than just "notice it quickly"--I can't be polling, or receiving some asynchronous notification that means that the process has already been executing for a few milliseconds by the time I take action.
I need to have a chance to do my injecting and interposing before a single statement executes.
I'm open to writing a background process that gets synchronously notified when a process by a particular name comes into existence. I'm also open to writing a launcher application that in turn fires up the target application.
Any solution needs to support 64-bit code, at a minimum, under 10.5 (Leopard) through 10.8 (Mountain Lion).
In case this proves to be painfully simple, I'll go ahead and admit that I'm new to OS X :) Thanks!
I know how to do this on Linux, so maybe it would be the same(-ish) on OSX.
You first call fork() to duplicate your process. The return value of fork() indicates whether you are the parent or child. The parent gets the pid of the child process, and the child gets zero.
So then, the child calls exec() to actually begin executing the new executable. With the use of a pipe created before the call to fork, the child could wait on the parent to do whatever it needed before execing the new execuatable.
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(1);
}
if (pid > 0) {
// I am the parent, and pid is the PID of the child process.
//TODO: If desired, somehow notify child to proceed with exec
}
else {
// I am the child.
//TODO: If desired, wait no notification from parent to continue
execl("path/to/executable", "executable", "arg1", NULL);
// Should never get here.
fprintf(stderr, "ERROR: execl failed!\n");
}

fork() and wait() connection to pid

I know that fork() creates a child process, returns 0 to child and returns child's pid to parent.
From what I understand wait() also returns some kind of pid of the child process that's terminated. Is this the same pid as the one that's returned to parent after fork?
I don't understand how to use wait().
My textbook just shows
int ReturnCode;
while (pid!=wait(&ReturnCode));
/*the child has terminated with Returncode as its return code*/
I don't even understand what this means.
How do I use wait()? I am using execv to create a child process but I want parent to wait. Someone please explain and give an example.
Thanks
wait() does indeed return the PID of the child process that died. If you only have one child process, you don't really need to check the PID (do check that it's not zero or negative though; there are some conditions that may cause the wait call to fail). You can find an example here: http://www.csl.mtu.edu/cs4411/www/NOTES/process/fork/wait.html
wait() takes the address of an integer
variable and returns the process ID of
the completed process.
More about the wait() system call
The
while (pid!=wait(&ReturnCode));
loop is comparing the process id (pid) returned by wait() to the pid received earlier from a fork or any other process starter. If it finds out that the process that has ended IS NOT the same as the one this parent process has been waiting for, it keeps on wait()ing.

Resources