论坛风格切换切换到宽版
  • 649阅读
  • 1回复

[问题求助]关于FORK的一个程序 [复制链接]

上一主题 下一主题
 
发帖
2059
C币
-132341
威望
398
贡献值
1
银元
-2
铜钱
4694
人人网人气币
0
只看楼主 正序阅读 使用道具 楼主  发表于: 2009-05-01
#include<st.dio.h>             汽车
#include<stdlib..h>           女人
#include<sys/t.ypes.h>域名
#.include<unistd.h>电脑
#include<sys/wait..h>.
int main(void)
{
  pid_t child1,ch..ild2,child;.
  child1=fork().;.
  child2=fo.rk();.
  if(child1.==-1)
    {
    .  perror("child fo.rk");电脑
      exit(1);
    }
  else if(child1=.=0).
         {
.          pri.ntf("In child1:execute'ls -l'\n");电脑
          . .if(execlp("ls","ls","-l",NULL)<0)服务器
             perror(."child .execlp");.
         }
  if(chi.ld2==-1)域名
    {
.     perror("child2 fork.");            杀毒
      exit(1);
    }
  else if(child2.==0).
         {
           printf("In child2:slee.p for 5 seconds andthen ex.it\n");<性病>
      .     sleep(5);教育
   .        exit(0);--------------彩票
         }
       else
         {
           pr.intf("In father process;\n").;学习
           do
             {
               child=waitpid(child2,NU.LL,WNOHAN.G);服务器
.              if(child=.=0)投资
                 .{.
                   printf("The .child2 process has not ex.ited!\n");.
  . .                sleep(1);         外汇
                 .}           鲜花
        .   .  }while(child==0);--------------彩票
       .    if(.child==child2)服务器
             printf("Get .child2\n.");域名
           e.lse.
             printf(."Error occured".);学习
         }
}
运行结果(我运行过2次.,2次不一样):.
第一次:.In child1:e.xecute'ls -l'.
In child1:execute'l.s -l'.
In child2:sleep for 5. seconds an.dthen exit           女人
In fa.ther process;             电子
The child2 p.rocess has not e.xited![成人用品]
总用量 68
-rwxr-xr-x  1 root ro.ot 6725 10月 14 11:2.9 greet.
-.rw-r--r--  1 ro.ot root  494 10月 14 11:28 greet.c           鲜花
-rwxr-xr-x  1 .root root 6799 1.0月 14 19:33 inittab.
-rw-r--r--  1 root root 1620 1.0月 14 10:53 ini.ttab~服务器
-rw-r--r--  1 root r.oot.  688 10月 14 19:32 inittab.c(        游戏          )
-rwxr-xr-x  1 .root root 57.33 10月 15 00:18 p232页电脑
-rw-r--r--.  1 root ro.ot 1126 10月 15 00:17 p232页.c.
总用量 68
-rwxr-xr-x  1 ro.ot root 6725 10.月 14 11:29 greet--------------彩票
-rw-r--r--  1 ro.ot .root  494 10月 14 11:28 greet.c投资
-rw.xr-xr-x  1 root root 6799 10月 14 19:33. inittab.
-rw-r--r--  1 root root 1620. 10月 14 10:53 in.ittab~投资
-rw-r--r--  1 ro.ot root  688 10月 14 19:32 init.tab.c.
-rwxr-xr-x  1 root root 573.3 10月 15 00:1.8 p232页           鲜花
-rw-r--r--  1 roo.t root 1126 10月 15 00:17 p232页..c[成人用品]
The child.2 process has not exit.ed![成人用品]
The c.hild2 process has not. exited!电脑
The .child2 process .has not exited![成人用品]
The child2 proces.s .has not exited!.
Get child2

第2次
In .child2:sleep for 5. seconds andthen exit.
In father p.rocess;    外汇
The child2 p.rocess h.as not exited!健康
In child1:execute'.ls -l'电脑
总用量 68
-rwxr-xr-x . 1 root r.oot 6725 10月 14 11:29 greet           建材
-rw-r--r--  1 root. root  494 10.月 14 11:28 greet.c服务器
-rwxr-x.r-x  1 root root 6799 10月. 14 19:33 inittab--- 印刷
-r.w-r--r--.  1 root root 1620 10月 14 10:53 inittab~.
-r.w-.r--r--  1 root root  688 10月 14 19:32 inittab.c<性病>
-.rwxr-xr-x  1 root r.oot 5733 10月 15 00:18 p232页    健康
-rw-r--r--  1 root root 1126 10月 15 00.:17 p2.32页.c.
In child.1:execute'ls -l'              乙肝
总用量 68
-rwxr-xr-x  1 root .root 6725 10月 14 11:29 gr.eet    外汇
-rw-r--r--  1 ro.ot. root  494 10月 14 11:28 greet.c           建材
-rwxr-.xr-x  1 root root 6799 10.月 14 19:33 inittab
-rw.-r--r--  1 root root 1620 10月 1.4 10:53 inittab~.
-rw-r--r--  1 root ro.ot  688 10月. 14 19:32 inittab.c           女人
-rwxr-xr-x  1. root r.oot 5733 10月 15 00:18 p232页           鲜花
-rw-r--r.--  .1 root root 1126 10月 15 00:17 p232页.c虚拟主机
The ch.ild2 pro.cess has not exited!(        游戏          )
The child2 p.rocess has not .exited!    健康
The child2 process has. not exite.d!域名
The child2 process has. not exi.ted!<性病>
Get child2


我知道2次结果显示不一样是因为子进程和父进程被调度的顺序不一样,我就想问为什么In child1:execute'ls -l'会显示2次,应该只执行一次阿,而且.3个进程都是穿插执行的,不知道执行顺序到底是怎么的!比如先执行child1的程序显示料In child1.:execut.e'ls -l',但-rwxr-xr-x  1 root root 6725 10月 14 .11:29 greet还没显示却.就开始执行父进程的程序。    健康

评价一下你浏览此帖子的感受

精彩

感动

搞笑

开心

愤怒

无聊

灌水
离线aaer.
发帖
2090
C币
-152446
威望
394
贡献值
1
银元
-6
铜钱
4772
人人网人气币
0
只看该作者 沙发  发表于: 2010-04-14
帮忙看下啊
快速回复
限100 字节
如果您在写长篇帖子又不马上发表,建议存为草稿
 
上一个 下一个