I am playing around with system calls in C and I am stuck trying to understand this program I made -
int main(int argc, char* argv[])
{
int a;
char *args[]={"sleep"," 10",NULL};
a = fork();
int stat;
if(a==0){
setpgid(getpid(),getpid());
printf("%d\n",getpgid(getpid()));
execvp(args[0],args);}
else
{
int t2;
waitpid(-a,&t2,0);
}
printf("Parent pid = %d\n", getpid());
printf("Child pid = %d\n", a);
}
According to my understanding, I have set pgid of child as its own pid. When I call waitpid
with -a as argument, I am basically asking it to wait(blocking) till any process in pgid=a is finished. However, the output of the program is not what I expected! Child process isn't being reaped at all. It is as if waitpid is in non-blocking mode. Output:
Parent pid = 11372
Child pid = 11373
11373
(The output is instantaneous, it doesn't wait for 10 seconds!)
EDIT : I added printf("Here")
and exit(1)
below execvp and printed out waitpid's output as suggested in comments. Here doesn't get printed and waitpid prints -1