Skip to content

Instantly share code, notes, and snippets.

@Blizzardo1
Created December 9, 2022 03:58
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 Blizzardo1/4202c525c931a06728f2addc4488630e to your computer and use it in GitHub Desktop.
Save Blizzardo1/4202c525c931a06728f2addc4488630e to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
pid_t proc1 = fork();
pid_t proc2 = fork();
if(proc1 < 0 || proc2 < 0) {
perror("Forking failed");
}
if(proc1 == 0) {
printf("I am the first child proccess: %d\n", (int) getpid());
}
if(proc2 == 0) {
printf("I am the second child process: %d\n", (int) getpid());
}
if(getpid() > 0) {
printf("I am the parent process: %d\n", (int) getpid());
printf("Parent is waiting for children to die...");
}
sleep(5);
wait(NULL);
printf("\nChildren are now dead!\n");
exit(0);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment