Created
September 26, 2011 17:46
-
-
Save uhziel/1242844 to your computer and use it in GitHub Desktop.
apue.2e.fig1.7.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "apue.h" | |
#include <sys/wait.h> | |
int main() | |
{ | |
char buf[MAXLINE]; | |
pid_t pid; | |
int status; | |
printf("%% "); | |
while (fgets(buf, MAXLINE, stdin) != NULL) | |
{ | |
if (buf[strlen(buf) - 1] == '\n') | |
{ | |
buf[strlen(buf) - 1] = '\0'; | |
} | |
pid = fork(); | |
if (pid < 0) | |
{ | |
err_sys("fork error"); | |
} | |
else if (pid == 0) | |
{ | |
/* child */ | |
execlp(buf, buf, (char*)0); | |
err_ret("couldn't exec: %s", buf); | |
return 127; | |
} | |
else | |
{ | |
/* parent */ | |
if ((pid = waitpid(pid, &status, 0)) < 0) | |
{ | |
err_sys("waitpid error"); | |
} | |
printf("%% "); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment