tags:

views:

149

answers:

1

I have a child process which has received a SIGTSTP signal. When I call

waitpid(-1,NULL,0);

the parent blocks, but in documentation, its written that waitpid returns with pid for stopped jobs.

#include<unistd.h>
#include<stdio.h>
#include<signal.h>
#include<sys/wait.h>
main() {
int pid;
if( (pid=fork()) > 0) {
    sleep(5);
    if(kill(pid,SIGTSTP) < 0)
     printf("kill error\n");
    int status;
    waitpid(-1,&status,0);
    printf("Returned %d\n",WIFSTOPPED(status));
}
else if(pid==0) {
    while(1);
}
}
+1  A: 

You missed the option WUNTRACED for waitpid (3rd argument). Otherwise it doesn't return until the job is terminated. When the WUNTRACED option is set, children of the current process that are stopped due to a SIGTTIN, SIGTTOU, SIGTSTP, or SIGSTOP signal also have their status reported (from the mac man page).

Petesh
Thank u very much for ur answer. I should have read the documentaion properly. I wasted two hours in my large program looking for other errors.
avd