// p3.c —— 原书 Figure 5.3:fork() + wait() + exec(),子进程变身为 wc #include #include #include #include #include int main(int argc, char *argv[]) { printf("hello (pid:%d)\n", (int) getpid()); fflush(stdout); // 保证 fork 前缓冲区是空的(见 README "缓冲区陷阱") int rc = fork(); if (rc < 0) { // fork failed; exit fprintf(stderr, "fork failed\n"); exit(1); } else if (rc == 0) { // child (new process) printf("child (pid:%d)\n", (int) getpid()); fflush(stdout); char *myargs[3]; myargs[0] = strdup("wc"); // program: "wc" myargs[1] = strdup("p3.c"); // arg: input file myargs[2] = NULL; // mark end of array execvp(myargs[0], myargs); // runs word count printf("this shouldn't print out"); } else { // parent goes down this path int rc_wait = wait(NULL); printf("parent of %d (rc_wait:%d) (pid:%d)\n", rc, rc_wait, (int) getpid()); } return 0; }