Last active
January 27, 2019 21:38
-
-
Save micfan/f543ee4f9118abe2e4ab to your computer and use it in GitHub Desktop.
APUE_examples
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
// APUE.Chapter.8 | |
// fork(), exec(), wait() | |
// Michael.Fan | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <sys/wait.h> | |
int main () | |
{ | |
pid_t fpid; //fpid表示fork函数返回的值 | |
int count=0; | |
int status; | |
fpid = vfork(); | |
if (fpid < 0) | |
printf("error in fork!"); | |
else if (fpid == 0) { | |
char * argv[] = {" ."}; | |
execv("/bin/ls", argv); | |
printf("i am the child process, my process id is %d\n",getpid()); | |
printf("我是儿子\n\n");//对某些人来说中文看着更直白。 | |
count++; | |
_exit(0); | |
} | |
if (wait(&status) == fpid) { | |
printf("i am the parent process, my process id is %d\n",getpid()); | |
printf("我是爹\n"); | |
printf("我儿子进程id是:%d\n", fpid); | |
count++; | |
} | |
printf("统计结果是: %d\n",count); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment