Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 什么';这是我们的意图_Linux - Fatal编程技术网

Linux 什么';这是我们的意图

Linux 什么';这是我们的意图,linux,Linux,下面的示例代码来自linux手册页waitpid函数。最后一个else if是否可以替换为else?当我编写代码时,我会这样写:if,elseif,并以else结尾。因此,我认为在示例代码中很奇怪 #include <sys/wait.h> #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char *ar

下面的示例代码来自linux手册页waitpid函数。最后一个else if是否可以替换为else?当我编写代码时,我会这样写:if,elseif,并以else结尾。因此,我认为在示例代码中很奇怪

   #include <sys/wait.h>
   #include <stdlib.h>
   #include <unistd.h>
   #include <stdio.h>

   int
   main(int argc, char *argv[])
   {
       pid_t cpid, w;
       int status;

       cpid = fork();
       if (cpid == -1) {
           perror("fork");
           exit(EXIT_FAILURE);
       }

       if (cpid == 0) {            /* Code executed by child */
           printf("Child PID is %ld\n", (long) getpid());
           if (argc == 1)
               pause();                    /* Wait for signals */
           _exit(atoi(argv[1]));

       } else {                    /* Code executed by parent */
           do {
               w = waitpid(cpid, &status, WUNTRACED | WCONTINUED);
               if (w == -1) {
                   perror("waitpid");
                   exit(EXIT_FAILURE);
               }

               if (WIFEXITED(status)) {
                   printf("exited, status=%d\n", WEXITSTATUS(status));
               } else if (WIFSIGNALED(status)) {
                   printf("killed by signal %d\n", WTERMSIG(status));
               } else if (WIFSTOPPED(status)) {
                   printf("stopped by signal %d\n", WSTOPSIG(status));
               } else if (WIFCONTINUED(status)) { /* can this be 
                                                   *replaced with else ???
                                                   */
                   printf("continued\n");
               }
           } while (!WIFEXITED(status) && !WIFSIGNALED(status));
           exit(EXIT_SUCCESS);
       }
   }
#包括
#包括
#包括
#包括
int
main(int argc,char*argv[])
{
pid_t cpid,w;
智力状态;
cpid=fork();
如果(cpid==-1){
佩罗尔(“福克”);
退出(退出失败);
}
如果(cpid==0){/*子级执行的代码*/
printf(“子PID为%ld\n”,(长)getpid();
如果(argc==1)
暂停();/*等待信号*/
_出口(atoi(argv[1]);
}else{/*由父级执行的代码*/
做{
w=等待PID(cpid和状态,WUNTRACED | WCONTINUED);
如果(w==-1){
佩罗(“waitpid”);
退出(退出失败);
}
如果(妻子退出(状态)){
printf(“已退出,状态=%d\n”,WEXITSTATUS(状态));
}否则如果(WIFSIGNALED(状态)){
printf(“被信号%d\n杀死”,WTERMSIG(状态));
}否则如果(WIFSTOPED(状态)){
printf(“被信号%d\n停止”,WSTOPSIG(状态));
}否则,如果(WIFCONTINUED(status)){/*这是否可以
*换成其他的???
*/
printf(“续”);
}
}而(!WIFEXITED(status)和&!WIFSIGNALED(status));
退出(退出成功);
}
}

我想你在问这样一个问题:

waitpid
是否可以返回一个
状态
,使得
WIFEXITED(状态)
WIFSIGNALED(状态)
WIFSTOPPED(状态)
,或
WIFCONTINUED(状态)
返回非零

答案几乎可以肯定是“不”,这句话暗示了很多,但没有(不幸的)明确地说出来

UNIX标准说明了更多,并特别保证其中一个宏在某些情况下返回非零。但是Linux并不(一定)符合这个标准

但是我认为在代码中最好的解决方案是在这四个选项后面有一个
else
子句,并且(取决于您的应用程序)认为这意味着状态未知。即使现在不是这样,这种情况也可能发生——也许在未来的Linux版本中,这里没有介绍另一种退出条件,您不希望代码在这种情况下崩溃

这个标准似乎是随着时间的推移而演变的。例如,早期版本没有
WIFCONTINUED
案例,我还在一些其他系统的
WIFCORED
中在线找到了一些参考。因此,如果您关心代码的灵活性,那么最好使其灵活