LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 725|回复: 4

问linux进程控制问题。

[复制链接]
发表于 2007-4-22 20:04:27 | 显示全部楼层 |阅读模式
照着参考书写了一个多进程的简单程序,编译执行后发现子进程child1竟然被创建了两次。不太明白为什么?而且我把child1子进程中的execlp函数调用去掉以后,child2好像也被创建了两次,好像一次是父进程创建的,一次是child1进程创建的。不太明白linux下进程创建和执行的过程。望各位高手指点。
谢谢了阿。

#include <stdio.h>

#include <stdlib.h>

#include <sys/types.h>

#include <unistd.h>

#include <sys/wait.h>

int main(void)

{
       printf("the parent pid is %d\n",getpid());

        pid_t child1,child2,child;

        child1 = fork();

        child2 = fork();

        if( child1 == -1 ){

                perror("child1 fork");

                exit(1);

        }

        else if( child1 == 0 ){
           printf("the child1 pid is %d\n",getpid());

                printf("In child1: execute 'ls -l'\n");

                if(execlp("ls","ls","-l",NULL)<0)

                  perror("child1 execlp");

        }


        if( child2 == -1 ){

                perror("child2 fork");

                exit(1);

        }

        else if( child2 == 0 ){
           printf("the child2 pid is %d\n",getpid());

                printf("In child2: sleep for 5 seconds and then exit\n");

                sleep(5);

                exit(0);

        }

        else{
           printf("the parent of the child2 pid is %d\n",getpid());

                printf("In father process:\n");

                do{

                        child = waitpid( child2, NULL, WNOHANG );

                        if( child ==0 ){

                                printf("The child2 process has not exited!\n");

                                sleep(1);

                        }

                }while( child == 0 );

                if( child == child2 )

                        printf("Get child2\n");

                else

                        printf("Error occured!\n");

        }

}
 楼主| 发表于 2007-4-22 20:13:33 | 显示全部楼层
上面那个程序的运行结果是这样的:
the parent pid is 5696
the child1 pid is 5698
In child1: execute 'ls -l'
the child2 pid is 5699
In child2: sleep for 5 seconds and then exit
the parent of the child2 pid is 5696
In father process:
The child2 process has not exited!
the child1 pid is 5697
In child1: execute 'ls -l'
总计 87
-rwxrwxrwx 1 root root  627 2006-08-22 dameon.c
-rwxrwxrwx 1 root root 6024 04-22 20:08 example
-rwxrwxrwx 1 root root 1234 04-22 19:21 example1~
-rwxrwxrwx 1 root root 1361 04-22 19:27 example.c
-rwxrwxrwx 1 root root 1234 04-22 19:22 example.c~
-rwxrwxrwx 1 root root  175 2006-08-22 execl.c
-rwxrwxrwx 1 root root  274 2006-08-22 execle.c
-rwxrwxrwx 1 root root  172 2006-08-22 execlp.c
-rwxrwxrwx 1 root root  295 2006-08-22 execve.c
-rwxrwxrwx 1 root root 4807 04-21 16:33 exit
-rwxrwxrwx 1 root root  167 04-21 16:36 _exit.c
-rwxrwxrwx 1 root root  146 04-21 16:35 exit.c
-rwxrwxrwx 1 root root 5827 04-22 19:09 exp1
-rwxrwxrwx 1 root root 1385 04-22 19:08 exp1.c
-rwxrwxrwx 1 root root 1702 04-22 18:52 exp1.c~
-rwxrwxrwx 1 root root 6041 04-22 18:04 exp2
-rwxrwxrwx 1 root root 1304 04-22 18:11 exp2.c
-rwxrwxrwx 1 root root 1192 04-22 18:04 exp2.c~
-rwxrwxrwx 1 root root 6463 04-22 19:41 fork
-rwxrwxrwx 1 root root  504 04-22 19:37 fork.c
-rwxrwxrwx 1 root root  506 04-22 19:36 fork.c~
-rwxrwxrwx 1 root root  207 2006-08-22 process.c
-rwxrwxrwx 2 root root  862 2006-08-22 syslog_dameon.c
-rwxrwxrwx 1 root root 5330 04-21 16:54 wait
-rwxrwxrwx 1 root root  517 04-21 16:54 waitpid.c
总计 87
-rwxrwxrwx 1 root root  627 2006-08-22 dameon.c
-rwxrwxrwx 1 root root 6024 04-22 20:08 example
-rwxrwxrwx 1 root root 1234 04-22 19:21 example1~
-rwxrwxrwx 1 root root 1361 04-22 19:27 example.c
-rwxrwxrwx 1 root root 1234 04-22 19:22 example.c~
-rwxrwxrwx 1 root root  175 2006-08-22 execl.c
-rwxrwxrwx 1 root root  274 2006-08-22 execle.c
-rwxrwxrwx 1 root root  172 2006-08-22 execlp.c
-rwxrwxrwx 1 root root  295 2006-08-22 execve.c
-rwxrwxrwx 1 root root 4807 04-21 16:33 exit
-rwxrwxrwx 1 root root  167 04-21 16:36 _exit.c
-rwxrwxrwx 1 root root  146 04-21 16:35 exit.c
-rwxrwxrwx 1 root root 5827 04-22 19:09 exp1
-rwxrwxrwx 1 root root 1385 04-22 19:08 exp1.c
-rwxrwxrwx 1 root root 1702 04-22 18:52 exp1.c~
-rwxrwxrwx 1 root root 6041 04-22 18:04 exp2
-rwxrwxrwx 1 root root 1304 04-22 18:11 exp2.c
-rwxrwxrwx 1 root root 1192 04-22 18:04 exp2.c~
-rwxrwxrwx 1 root root 6463 04-22 19:41 fork
-rwxrwxrwx 1 root root  504 04-22 19:37 fork.c
-rwxrwxrwx 1 root root  506 04-22 19:36 fork.c~
-rwxrwxrwx 1 root root  207 2006-08-22 process.c
-rwxrwxrwx 2 root root  862 2006-08-22 syslog_dameon.c
-rwxrwxrwx 1 root root 5330 04-21 16:54 wait
-rwxrwxrwx 1 root root  517 04-21 16:54 waitpid.c
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
The child2 process has not exited!
Get child2
回复 支持 反对

使用道具 举报

发表于 2007-4-22 23:32:03 | 显示全部楼层
Post by topology
照着参考书写了一个多进程的简单程序,编译执行后发现子进程child1竟然被创建了两次。不太明白为什么?而且我把child1子进程中的execlp函数调用去掉以后,child2好像也被创建了两次,好像一次是父进程创建的,一次是child1进程创建的。不太明白linux下进程创建和执行的过程。

好好看书,谢谢!
回复 支持 反对

使用道具 举报

发表于 2007-4-23 12:23:53 | 显示全部楼层
第二次fork之后,你得到两个child1==0的进程,所以ls也执行了两遍

贴代码记得用code标签
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-4-23 16:35:19 | 显示全部楼层
谢谢了阿。说一下我的理解你看看对吗?第一个fork调用后产生一个子进程child1,然后这个子进程就开始运行,他向下运行程序的时候会调用下面那个fork,从而创建了一个它自己的子进程child2,这两个进程的child1都为0,因此才会执行两次ls -l.您觉得对不对阿
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表