Skip to content

Instantly share code, notes, and snippets.

@morsoinferno
Created April 25, 2018 13:53
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/8eede79de11662c84a36ac247984cf66 to your computer and use it in GitHub Desktop.
Save morsoinferno/8eede79de11662c84a36ac247984cf66 to your computer and use it in GitHub Desktop.
fork3.c
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int num;
pid_t pid;
for (num = 0; num < 3; num++)
{
pid = fork();
printf("Soy el proceso PID=%d y mi padre PID=%d\n", getpid(), getppid());
if (pid != 0)
{
break;
}
srandom(getpid());
sleep(random()%3);
}
if (pid != 0)
{
printf("fin del proceso PID=%d\n", wait(NULL));
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment