红联Linux门户
Linux帮助

请教关于wait()函数的问题

发布时间:2010-01-18 18:13:43来源:红联作者:rosetango
代码如下:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 int main(void)
9 {
10 pid_t child;
11 int status;
12 printf("This will demostrate how to get child status\n");
13 if((child=fork())==-1) //用fork()函数创建一个子进程并把该函数的返回值保存在变量 child 中
14 { //如果 child 值为 -1,则说明 fork() 函数创建子进程失败(内存不足或者是用户的最大进程数已到)
15 printf("Fork Error :%s\n",strerror(errno)); //strerror() //转换指定的错误码(errno)为对应的错误信息
16 //errno 是错误代码,在 errno.h 头文件中
17 exit(1);
18 }
19 else if(child==0) //如果 child 值为1,则说明 fork() 函数创建子进程成功,并由子进程执行下面的代码
20 {
21 int i;
22 printf("I am the child:%ld\n",getpid()); //获得当前进程的ID(当前进程为子进程)
23 for(i=0;i<1000000;i++) sin(i);
24 i=5;
25 printf("I exit with %d\n",i);
26 exit(i); //退出子进程,参数i的值代表退出的特征码, 0 表示正常退出, 非0表示非正常退出
27 }
28 while(((child=wait(&status))==-1)&(errno==EINTR));
29 if(child==-1)
30 printf("Wait Error:%s\n",strerror(errno));
31 else if(!status)
32 printf("Child %ld terminated normally return status is zero\n",
33 child);
34 else if(WIFEXITED(status)) //判断子进程退出值是非0
35 printf("Child %ld terminated normally return status is %d\n",
36 child,WEXITSTATUS(status));
37 else if(WIFSIGNALED(status)) //子进程由于有没有获得的信号而退出.
38 printf("Child %ld terminated due to signal %d znot caught\n",
39 child,WTERMSIG(status));
40 }

第28行的代码
while(((child=wait(&status))==-1)&(errno==EINTR));
是不是只是让父进程阻塞?!然后把wait()函数的返回值保存起来

还有
最后有一个分号
while后面括号的内容只是一个逻辑表达式
在括号有又跟一分号
是否说明该循环没有循环体
文章评论

共有 4 条评论

  1. himila 于 2010-01-19 10:57:04发表:

    不停检测状态,等待继续执行

  2. deepwhite 于 2010-01-19 09:30:38发表:

    wait 语句本身就可以让父进程等待子进程结束了,这里while语句的用途,我没有看出来。

  3. rosetango 于 2010-01-18 21:58:45发表:

    while(((child=wait(&status))==-1)&(errno==EINTR))
    ;
    请问这条空语句的作用是什么

  4. zhaoyuzhong3694 于 2010-01-18 20:51:09发表:

    [i=s] 本帖最后由 zhaoyuzhong3694 于 2010-1-18 21:00 编辑 [/i]

    while(((child=wait(&status))==-1)&(errno==EINTR))
    ;

    后边跟了一个空语句。