Skip to content

Instantly share code, notes, and snippets.

@morsoinferno
Last active March 31, 2017 18:05
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/6c16c0fc8919bb29c7be7b3c6f4c62ee to your computer and use it in GitHub Desktop.
Save morsoinferno/6c16c0fc8919bb29c7be7b3c6f4c62ee to your computer and use it in GitHub Desktop.
01_fork1
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
pid_t fork(void);
pid_t getpid(void);
pid_t getppid(void);
int main()
{
pid_t pid;
int status=10;
pid = fork();
if (pid == -1) {
perror("no se pudo crear hijo");
exit(1);
}
if (pid == 0) { // soy el hijo
printf("Soy el hijo y mi pid es %d\n", getpid());
printf("El pid de mi padre es %i\n", getppid());
//for (;;);
exit(-1);
}
else // soy el padre
{
printf("Soy el padre y mi pid es %d\n", getpid());
printf("Soy el padre y el pid de mi padre es %d\n", getppid());
printf("El pid de mi hijo es %d\n", pid);
wait(&status); // Espera por cualquiera de sus hijos
//printf("%i\n", status);
//waitpid(pid, &status, 0); // Espera por el hijo con pid especifico
exit(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment