Skip to content

Instantly share code, notes, and snippets.

@morsoinferno
Created March 31, 2017 18:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morsoinferno/c4911155d4893e6c3b4638de1a5a5e8f to your computer and use it in GitHub Desktop.
Save morsoinferno/c4911155d4893e6c3b4638de1a5a5e8f to your computer and use it in GitHub Desktop.
02_fork2
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#define NFORKS 100
void do_nothing(){
sleep(10); // 10 second
}
int main(){
int pid, j, status;
for (j=0; j < NFORKS; j++){
if ((pid = fork()) < 0){
printf("no se pudo crear hijo\n");
exit(1);
}
if (pid == 0){ // hijo
do_nothing();
exit(0);
}else{ // padre
waitpid(pid, &status, 0);
printf("%d\n", pid);
exit(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment