fork问题
1 #include <stdio.h>
2 #include <sys/wait.h>
3 #include <unistd.h>
4
5 #define MAXLINE 1024
6
7 int main(int argc, char** argv)
8 {
9 char buf[MAXLINE];
10 pid_t pid;
11 int status;
12
13 printf("%% ");
14 while(fgets(buf, MAXLINE, stdin) != NULL)
15 {
16 if(buf[strlen(buf) - 1] == '\n')
17 buf[strlen(buf) - 1] = '\0';
18 printf("buf is %s\n",buf);
19
20 if(pid = fork() < 0)
21 printf("the fork is worry!\n");
22 else if(pid == 0)
23 {
24 printf("the children run\n");
25 execlp(buf,buf, (char*)0);
26 printf("couldn't execute:%s,the pid is %d\n",buf, getpid());
27
28 }
29
30 if(pid = waitpid(pid, &status, 0) < 0)
31 printf("waitpid is error\n");
32
33 printf("%% ");
34 }
35
36 exit(0);
37 }
以上是源代码
这里是运行的结果
% ls
buf is ls
the children run
the children run
run run.c
run run.c
我想问为什么子进程会执行两次,而且父进程后面的都没有执行?