Re: Forking and catching SIGCHLD, si_pid always contains 0.
On Sat, May 1, 2010 at 2:01 PM, Ted Unangst wrote: > On Sat, May 1, 2010 at 9:59 AM, Jesus Sanchez wrote: >>As said in "Advanced Programming in the Unix Environment" book, when >> calling a sigaction function there is a siginfo_t * with data about the >> process sending the signal. On this struct, the member int si_pid >> contains the PID of the process sending the signal. I tried in a very >> simple code to obtain the PID of the child process but si_pid member >> always contains 0 when I print it, and don't know what's wrong with it. > > The si_pid is not generally reliable, so it's a good idea not to use > it. What happens if two processes send the same signal before you > receive it? Sure, but it can be reliable for the SIGCHLD generated when a process exits, as the kernel has to keep track of the zombies anyway. This is on my todo list, but it seems to keep getting pushed down by other things... Philip Guenther
Re: Forking and catching SIGCHLD, si_pid always contains 0.
On Sat, May 1, 2010 at 9:59 AM, Jesus Sanchez wrote: >As said in "Advanced Programming in the Unix Environment" book, when > calling a sigaction function there is a siginfo_t * with data about the > process sending the signal. On this struct, the member int si_pid > contains the PID of the process sending the signal. I tried in a very > simple code to obtain the PID of the child process but si_pid member > always contains 0 when I print it, and don't know what's wrong with it. The si_pid is not generally reliable, so it's a good idea not to use it. What happens if two processes send the same signal before you receive it?
Re: Forking and catching SIGCHLD, si_pid always contains 0.
El 01/05/2010 18:14, Otto Moerbeek escribis: On Sat, May 01, 2010 at 03:59:07PM +0200, Jesus Sanchez wrote: Hi, using 4.6 release. I'm doing some code on process forking and catching signals on OpenBSD. My interest here is to catch the SIGCHLD signal and do things with the pid which sended the signal on the function called to treat it. As said in "Advanced Programming in the Unix Environment" book, when calling a sigaction function there is a siginfo_t * with data about the process sending the signal. On this struct, the member int si_pid contains the PID of the process sending the signal. I tried in a very simple code to obtain the PID of the child process but si_pid member always contains 0 when I print it, and don't know what's wrong with it. I included a very simple source code to try this with the mail, what I'm missing? On OpenBSD, only a few fields are filled for a few signals. Use any of the wait(2) functions to get your info. well thats a relief, i thought that I was screwing up something. It's in some kind of TODO list or the OpenBSD project isn't interested in implement such a things. Thanks a lot for your answer -J -Otto Google didn't helped at all. Thanks for your time. -J #include #include #include #include #include #include /* this func is called when SIGCHLD is received */ void mysigaction(int nsig, siginfo_t * info , void *nothing){ /* print some info values */ printf("info->si_pid: %d info->si_code: %d info->si_status: %d --\n", info->si_pid , (*info). si_code, (*info).si_status); printf("nsig: %d\n",nsig); return ; } int main(){ struct sigaction myaction; myaction.sa_sigaction=mysigaction; myaction.sa_flags=SA_SIGINFO ; /* use sa_sigaction instead sa_handler to * have siginfo_t * values */ sigaction(SIGCHLD,&myaction,(void*)NULL); int pid=fork(); if (pid> 0 ) { // father printf("father, child pid is: %d\n",pid); wait(NULL); } if (pid == 0){ // son printf("son, getpid() returns: %d\n",getpid()); exit(0); } return 0; }
Re: Forking and catching SIGCHLD, si_pid always contains 0.
On Sat, May 01, 2010 at 03:59:07PM +0200, Jesus Sanchez wrote: > Hi, using 4.6 release. > > I'm doing some code on process forking and catching signals on > OpenBSD. My interest here is to catch the SIGCHLD signal and do things > with the pid which sended the signal on the function called to treat it. > > As said in "Advanced Programming in the Unix Environment" book, when > calling a sigaction function there is a siginfo_t * with data about the > process sending the signal. On this struct, the member int si_pid > contains the PID of the process sending the signal. I tried in a very > simple code to obtain the PID of the child process but si_pid member > always contains 0 when I print it, and don't know what's wrong with it. > > I included a very simple source code to try this with the mail, what > I'm missing? On OpenBSD, only a few fields are filled for a few signals. Use any of the wait(2) functions to get your info. -Otto > > Google didn't helped at all. > > Thanks for your time. > -J > #include > #include > #include > #include > #include > #include > > /* this func is called when SIGCHLD is received */ > void mysigaction(int nsig, siginfo_t * info , void *nothing){ > >/* print some info values */ > printf("info->si_pid: %d info->si_code: %d info->si_status: %d --\n", > info->si_pid , (*info). si_code, (*info).si_status); > printf("nsig: %d\n",nsig); > > return ; > } > > int main(){ > > struct sigaction myaction; > myaction.sa_sigaction=mysigaction; > myaction.sa_flags=SA_SIGINFO ; >/* use sa_sigaction instead sa_handler to > * have siginfo_t * values */ > > sigaction(SIGCHLD,&myaction,(void*)NULL); > > int pid=fork(); > if (pid > 0 ) { // father > printf("father, child pid is: %d\n",pid); > wait(NULL); > } > > if (pid == 0){ // son > printf("son, getpid() returns: %d\n",getpid()); > exit(0); > } > > return 0; > }
Forking and catching SIGCHLD, si_pid always contains 0.
Hi, using 4.6 release. I'm doing some code on process forking and catching signals on OpenBSD. My interest here is to catch the SIGCHLD signal and do things with the pid which sended the signal on the function called to treat it. As said in "Advanced Programming in the Unix Environment" book, when calling a sigaction function there is a siginfo_t * with data about the process sending the signal. On this struct, the member int si_pid contains the PID of the process sending the signal. I tried in a very simple code to obtain the PID of the child process but si_pid member always contains 0 when I print it, and don't know what's wrong with it. I included a very simple source code to try this with the mail, what I'm missing? Google didn't helped at all. Thanks for your time. -J #include #include #include #include #include #include /* this func is called when SIGCHLD is received */ void mysigaction(int nsig, siginfo_t * info , void *nothing){ /* print some info values */ printf("info->si_pid: %d info->si_code: %d info->si_status: %d --\n", info->si_pid , (*info). si_code, (*info).si_status); printf("nsig: %d\n",nsig); return ; } int main(){ struct sigaction myaction; myaction.sa_sigaction=mysigaction; myaction.sa_flags=SA_SIGINFO ; /* use sa_sigaction instead sa_handler to * have siginfo_t * values */ sigaction(SIGCHLD,&myaction,(void*)NULL); int pid=fork(); if (pid > 0 ) { // father printf("father, child pid is: %d\n",pid); wait(NULL); } if (pid == 0){ // son printf("son, getpid() returns: %d\n",getpid()); exit(0); } return 0; }