Skip to content

Instantly share code, notes, and snippets.

@jdpaton
Created October 25, 2012 18:40
Show Gist options
  • Save jdpaton/3954580 to your computer and use it in GitHub Desktop.
Save jdpaton/3954580 to your computer and use it in GitHub Desktop.
Fork x number of child processes which have a set lifetime
#include <stdio.h>
#include <stdlib.h>
static int expiry = 30;
int main(int argc, char *argv[]){
int k = *argv[1];
int i=0;
for(i = 0; i<k; i++){
if(fork() == 0){
printf("Forked child %d %d\n", i, ++k);
sleep(expiry);
printf("Forked child %d done\n",i);
exit(0);
}else{
fprintf(stderr, "!! Unable to fork child: %d", i);
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment