Created
March 31, 2017 18:12
-
-
Save morsoinferno/c4911155d4893e6c3b4638de1a5a5e8f to your computer and use it in GitHub Desktop.
02_fork2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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