![]() |
how a father process know which child process send the signal SIGCHLD
If a process create a lot of child processes.
When a child process ends, it will send a signal of SIGCHLD to its father process. But how the father process know which of its child processes sends the signal? |
By catching SIGCHLD and then calling wait() in the signal handler.
Code:
RETURN VALUE |
As far as I know there's no way to know who send some signal...
Calling wait() only waits until some signal is recieved. If you already recieved the signal, I think you are out of luck. |
The wait3()/wait/waitpid function allows the calling process to obtain status information for specified child processes, stored in stat_loc.
See man of following call: pid_t wait(int *stat_loc); wait3(stat_loc, options, resource_usage); or waitpid((pid_t)-1, stat_loc, options); |
Blah...here:
Code:
itsme@dreams:~/C$ cat wait.cCode:
itsme@dreams:~/C$ ./wait |
thank you so much.
What I just want is to get pid of the exitting process,and to run waitpid() to end it. These days I am programming a mini shell, and I meet a problem. When the shell runs a lot of commands once , and the commands will end in no order, how does the shell run wait() or waitpid() to deal with the zombie processes. When the shell runs commands, it must be blocked. So I think it is available to run wait() when it receives a signal SIGCHLD. |
Quote:
i got one question for u.. Code:
void handler(int sig) |
Hi.
You only have to keep tracking for your child processes at creation time. The value of SIGCHLD is the PID of any child process. At creation of a child process the parent get the PID of that new child and the child gets PID=0. Now you only have to store all your child PIDs in that creation order. Or you can handle different process names (p1, p2 ...), that a new process gets a new name. |
Quote:
Quote:
When the handler is called, it will pass the received signal number as a parameter (an int), so you'll always need to specify that parameter to be able to register it using signal(2), or (the newer) sigaction(2). ...whether you use it or not. |
There's the SA_SIGINFO extension to sigaction(2) too. You may even know if the signal was faked.
|
For MUltiple Child one has to write
signal action again into handler void handler(int sig) { pid_t pid; pid = wait(NULL); printf("Pid %d exited.\n", pid); signal(SIGCHLD, handler); return; } |
| All times are GMT -5. The time now is 05:51 PM. |