首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误:在gcc中‘wstat’的类型冲突

错误:在gcc中‘wstat’的类型冲突
EN

Stack Overflow用户
提问于 2019-06-17 17:24:46
回答 1查看 796关注 0票数 4

我从这个文档中复制了这个程序:https://docs.oracle.com/cd/E19455-01/806-4750/signals-7/index.html

代码语言:javascript
复制
#include <stdio.h>
#include <signal.h>
#include <sys/wait.h>
#include <sys/resource.h>

void proc_exit()
{
        int wstat;
        union wait wstat;
        pid_t   pid;

        while (TRUE) {
            pid = wait3 (&wstat, WNOHANG, (struct rusage *)NULL );
            if (pid == 0)
                return;
            else if (pid == -1)
                return;
            else
                printf ("Return code: %d\n", wstat.w_retcode);
        }
}
main ()
{
        signal (SIGCHLD, proc_exit);
        switch (fork()) {
            case -1:
                perror ("main: fork");
                exit (0);
            case 0:
                printf ("I'm alive (temporarily)\n");
                exit (rand());
            default:
                pause();
        }
}

当我运行gcc main.c时,我得到这个错误:

代码语言:javascript
复制
main.c: In function ‘proc_exit’:
main.c:9:13: error: conflicting types for ‘wstat’
  union wait wstat;
             ^~~~~
main.c:8:6: note: previous declaration of ‘wstat’ was here
  int wstat;
      ^~~~~
main.c:9:13: error: storage size of ‘wstat’ isn’t known
  union wait wstat;
             ^~~~~
main.c:12:9: error: ‘TRUE’ undeclared (first use in this function)
  while (TRUE) {

据我所知,wstat被定义了两次。这是否意味着文档是不正确的?以及如何修复它?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-17 17:52:02

是啊,那个密码就是坏了。我不知道union wait wstat;的意图是什么。可能是文档作者的一些复制粘贴错误。谁知道呢。一般来说,代码写得很差,IMO。

无论如何,以下是代码实际要做的事情:

代码语言:javascript
复制
int wstat;
pid_t pid;

while (1) {
    pid = wait3(&wstat, WNOHANG, NULL);
    if (pid == 0 || pid == -1)
        return;
    printf("Return code: %d\n", wstat);
}
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56628418

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档