Skip to content

Instantly share code, notes, and snippets.

@dcalacci
Created October 31, 2012 01:48
Show Gist options
  • Save dcalacci/3984335 to your computer and use it in GitHub Desktop.
Save dcalacci/3984335 to your computer and use it in GitHub Desktop.
execute function from hw4.c
static void execute(int argc, char *argv[])
{
pid_t childpid; /* child process ID */
childpid = fork();
if (childpid == -1) { /* in parent (returned error) */
perror("fork"); /* perror => print error string of last system call */
printf(" (failed to execute command)\n");
}
if (childpid == 0) { /* child: in child, childpid was set to 0 */
/* Executes command in argv[0]; It searches for that file in
* the directories specified by the environment variable PATH.
*/
if (-1 == execvp(argv[0], argv)) {
perror("execvp");
printf(" (couldn't find command)\n");
}
/* NOT REACHED unless error occurred */
exit(1);
} else /* parent: in parent, childpid was set to pid of child process */
waitpid(childpid, NULL, 0); /* wait until child process finishes */
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment