Skip to content

Instantly share code, notes, and snippets.

@morsoinferno
Created March 31, 2017 18:17
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/aff455a70c5641dfc94c8d55b1d58a38 to your computer and use it in GitHub Desktop.
Save morsoinferno/aff455a70c5641dfc94c8d55b1d58a38 to your computer and use it in GitHub Desktop.
07_execv
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main()
{
pid_t pid;
int status;
if ((pid= fork()) == -1) {
printf("No se pudo crear hijo\n");
exit(-1);
}
if (pid== 0) // soy el hijo
{
printf("soy el hijo y el pid de mi padre es %d\n", getppid());
fflush(stdout);
if (execl("/bin/ls", "ls", "/usr", (char*)NULL) == -1)
{
printf("No se pudo exec\n");
//exit(-1);
}
printf("Fin de ls\n");
//for (;;);
}
else // soy el padre
/*
if (execlp("ls", "ls", "/usr", 0) == -1)
{
printf("No se pudo exec\n");
exit(-1);
}
*/
printf("soy el padre y el pid de mi hijo es %d\n", pid);
// aqui soy el padre
wait(&status);
printf("Mi hijo ls termino\n");
exit(0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment