请教一个linux 线程编程问题
本帖最后由 lixiang201101 于 2013-10-21 22:10:49 编辑
/*************************************************************************
> File Name: exc.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: 2013年10月19日 星期六 18时35分46秒
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#include<sys/wait.h>
int main(void)
{
pid_t ch,ch1,ch2;
ch1=fork();
ch2=fork();
if(ch1==-1)
{
printf("error\n""");
exit(1);
}
else if(ch1==0)
{
printf("In ch1 :execute ' ls -l'\n");
if(execlp("ls","ls","-l",NULL)<0)
perror("ch1 execlp\n");
exit(1);
}
if(ch2==-1)
{
printf("ch2 fork");
exit(1);
}
else if(ch2==0)
{
printf("ch2 will sleep for about 5 seconds\n");
sleep(5);
exit(0);
}
else
{
printf("In father process:\n");
do
{
ch=waitpid(ch2,NULL,WNOHANG);
if(ch==0)
{
printf("The child2 process has not exited!\n");
sleep(1);
}
}while(ch==0);
if(ch==ch2)
printf("Get ch2\n");
else
printf("Error occured\n");
}
}
我在ubuntu上通过编译后在桌面上执行出来如下
root@lx-virtual-machine:~/桌面# ./exec
In father process:
The child2 process has not exited!
ch2 will sleep for about 5 seconds
In ch1 :execute ' ls -l'
In ch1 :execute ' ls -l'
总用量 28
总用量 28
drwxr-xr-x 2 root root 4096 10月 17 22:14 2-4
drwxr-xr-x 2 root root 4096 10月 21 20:51 2-5
-rwxrw-rw- 1 root root 1051 10月 19 18:50 exc.c
-rwxr-xr-x 1 root root 7625 10月 21 21:38 exec
drwxrwxrwx 2 root root 4096 10月 14 20:57 File
drwxrwxrwx 2 root root 4096 10月 17 15:53 Time
lrwxrwxrwx 1 root root 9 10月 13 22:17 到 hgfs 的链接 -> /mnt/hgfs
drwxr-xr-x 2 root root 4096 10月 17 22:14 2-4
drwxr-xr-x 2 root root 4096 10月 21 20:51 2-5
-rwxrw-rw- 1 root root 1051 10月 19 18:50 exc.c
-rwxr-xr-x 1 root root 7625 10月 21 21:38 exec
drwxrwxrwx 2 root root 4096 10月 14 20:57 File
drwxrwxrwx 2 root root 4096 10月 17 15:53 Time
lrwxrwxrwx 1 root root 9 10月 13 22:17 到 hgfs 的链接 -> /mnt/hgfs
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 ch2
在这里我就有问题了,
问题一:进程在经过了两个fork之后加上主进程数应该四四格进程吧?
问题二:为什么会出现两个
In ch1 :execute ' ls -l'
In ch1 :execute ' ls -l'
主进程经过第一个fork之后返回的是child1的进程号,然后child1返回0,下面的应该就只是会出现一个In ch1 :execute ' ls -l'啊,因为主进程经过第一个fork之后就创建了一个child1啊,两个是怎么来的啊?
问题三:在这里执行到后面应该会有四格进程,第一个child1创建之后,再执行child2=fork()的时候也会创建出一个子进程,所以相对来说一共就有2个父进程啊,到下面父进程判断的时候也应该会出现
The child2 process has not exited!
ch2 will sleep for about 5 seconds
两次再加上
The child2 process has not exited!
The child2 process has not exited!
五次一共就十次出现
The child2 process has not exited!
啊,为什么这里却只出现了5次啊??
这是我的几个问题,希望大家给点解答。 linux c
[解决办法]
问题一:是4个
问题二:用P1, P2, P3, P4来表示进程号
第一步
P1 -----> P2
ch1=p2 ch1=0
ch2=未知 ch2=未知
第二步
p1 ----> p3 p2 ----> p4
ch1=p1 ch1=p1 ch1=0 ch1=0
ch2=p2 ch2=0 ch2=p4 ch2=0
有两个进程的ch1=0,所以执行两次
问题三:还是第二个问题,因为两个ch1=0,退出了两个,也就是一对父子,所以你就只能看到5个了。
[解决办法]
p1--fork-->p2
p1--fork-->p3
p2--fork-->p4
四个进程的ch1和ch2的值分别是:
p1:ch1=p2的进程Id,ch2=p3的进程Id
p2:ch1=0,ch2=p4的进程Id
p3:ch1=p2的进程Id,ch2=0
p4:ch1=0,ch2=0
你在看一下上面的if语句,结果输出应该是正确的。