Created
July 29, 2020 14:34
-
-
Save kpp/3247275491687c3c921f6d97002e1c3d to your computer and use it in GitHub Desktop.
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 <stdio.h> | |
#include <unistd.h> | |
void child(int n) { | |
while (1) | |
{ | |
sleep(1); | |
printf("child %d!\n", n); | |
} | |
} | |
void parent() { | |
while (1) | |
{ | |
sleep(1); | |
printf("parent!\n"); | |
} | |
} | |
int main(int argc, char **argv) | |
{ | |
printf("--beginning of program\n"); | |
int i = 0; | |
for (i = 0; i < 5; i++) { | |
pid_t pid = fork(); | |
if (pid == 0) | |
{ | |
child(i); | |
} | |
else if (pid > 0) | |
{ | |
// parent | |
} | |
else | |
{ | |
// fork failed | |
printf("fork() failed!\n"); | |
return 1; | |
} | |
} | |
parent(); | |
printf("--end of program--\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment