39 lines
972 B
C
39 lines
972 B
C
|
// demonstrate non-blocking waitpid() in excess
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <sys/wait.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
|
int main(int argc, char* argv){
|
||
|
|
||
|
char *child_argv[] = {"./complain",NULL};
|
||
|
char *child_cmd = "complain";
|
||
|
|
||
|
printf("PARENT: Junior is about to '%s', I'll keep an eye on him\n",
|
||
|
child_cmd);
|
||
|
|
||
|
pid_t child_pid = fork();
|
||
|
|
||
|
// CHILD CODE
|
||
|
if(child_pid == 0){
|
||
|
printf("CHILD: I'm %d and I'm about to '%s'\n",
|
||
|
getpid(), child_cmd);
|
||
|
execvp(child_cmd, child_argv);
|
||
|
}
|
||
|
|
||
|
// PARENT CODE
|
||
|
int status;
|
||
|
int checked = 0;
|
||
|
while(1){
|
||
|
int cpid = waitpid(child_pid,&status,WNOHANG); // Check if child done, but don't actually wait
|
||
|
if(cpid == child_pid){ // Child did finish
|
||
|
break;
|
||
|
}
|
||
|
printf("Oh, junior's taking so long. Is he among the 50%% of people that are below average?\n");
|
||
|
checked++;
|
||
|
}
|
||
|
printf("PARENT: Good job junior. I only checked on you %d times.\n",checked);
|
||
|
return 0;
|
||
|
}
|
||
|
|