how a father process know which child process send the signal SIGCHLD
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
The process ID of the child which exited, or zero if WNO-
HANG was used and no child was available, or -1 on error
(in which case errno is set to an appropriate value).
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.
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.
handler seems to be having parameter "sig" but its not been used at all in the function...so whats that for?
The handler function is registered with the kernel so the kernel knows that it has to call that function when a signal arrives.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.