I have c code as such
int childid;
int parentid;
void siginthandler(int param)
{
// if this is the parent process
if(getpid() == parentid){
//handler code which sends SIGINT signal to child process
kill(childid, SIGINT);
}
else // if this is the child process
exit(0);
}
int main()
{
parentid = getpid(); // store parent id in variable parentid, for the parent as well as child process
int pid = fork();
int breakpid;
if(pid != 0){
childid = pid;
breakpid = wait();
printf("pid = %d\n", breakpid);
}
else
sleep(1000);
}
Now, when I run this code, the parent process waits, while the child process sleeps. If I now interrupt (by pressing ctrl+c) the parent program, UNIX (POSIX standard) documentation tells me that the wait function should return -1, with errno set to EINTR. My code implementation however, kills the child process since the parent process sends the SIGINT signal to the child process. But, surprisingly,(in the parent process) wait does not return pid = -1 with errno set to EINTR. Instead, it returns the id of the child process, which got killed.
Is there an explanation for this?
When you install a signal handler with
signal()
, one of 2 things occurWhich of these 2 depends on your platform, on yours it's
2
(probably, you don't show any code that installs a signal handler.)If you instead install a signal handler with sigaction(), you can control which behavior of
1
or2
you want with theSA_RESTART
flag.