Skip to content

Instantly share code, notes, and snippets.

@kpp
Created July 29, 2020 14:34
Show Gist options
  • Save kpp/3247275491687c3c921f6d97002e1c3d to your computer and use it in GitHub Desktop.
Save kpp/3247275491687c3c921f6d97002e1c3d to your computer and use it in GitHub Desktop.
#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