【问题标题】:how to use wait union in c languagec语言中等待联合的使用方法
【发布时间】:2020-05-18 13:32:12
【问题描述】:
union wait wstat;
        pid_t pid;

        while(1)
        {
            pid = wait3(&wstat,WNOHANG,(struct rusage *)NULL);
            if(pid == 0 )
                return;
            else if(pid == -1)
                return;
            else
            {
                fprintf(stderr,"\nProcess with PID : %d exited with return value: %d\n",pid,wstat.w_retcode);
                deleteJob(pid);
            }
        }

我正在尝试在我想使用等待联合的地方制作 unix shell,但我无法使用它,因为我收到错误 wait' isn't known warning: implicit declaration of functionwait3'的存储大小

【问题讨论】:

  • 你用了什么#includes?!

标签: c linux posix ubuntu-18.04


【解决方案1】:

您应该添加以下包含文件:

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>

man wait3

这是 Ubuntu 18 的完整示例,编译时没有错误:

#include <stdio.h>

#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>

void deleteJob(int pid)
{
        return;
}


int main(int argc, char **argv)
{

        int wstat;
        pid_t pid;

        while(1)
        {
            pid = wait3(&wstat,WNOHANG,(struct rusage *)NULL);
            if(pid == 0 )
                return 0;
            else if(pid == -1)
                return -1;
            else
            {
                fprintf(stderr,"\nProcess with PID : %d exited with return value: %d\n",pid,wstat);
                deleteJob(pid);
            }
        }
}

在 Ubuntu 18 上:

$ gcc -o testwait testwait.c
$ echo $?
0

【讨论】:

  • 在我的答案和手册页中每行放置一个 #include 语句。
  • 我的包含用于我想使用等待联合但我无法使用它的程序`#include #include #include #include #include #include #include #include #include #include `
  • 我想在unbuntu上使用它我应该怎么做
  • 我在回答中修改了代码:根据wait3手册页,wait3的第一个参数应该是int(而不是union wait)。
  • 在 CentOS 7 中,您可以找到在 /usr/include/bits/waitstatus.h 中定义的 union wait,但我在 Ubuntu 18 中找不到这个(可能是因为 Ubuntu 18 使用的是 Linux 内核 4,而 CentOS 7 仍然是 Linux 内核 3) .无论如何,您不应使用 wait3 或 wait4,而应使用 waitpidwaitid (如果您想使用 BSD 功能,则不应使用基于 Linux 内核的操作系统,而应使用 FreeBSD 或 NetBSD 之类的东西) .这就解释了为什么我可以在 CentOS 7 上编译但不能在 Ubuntu 18 上编译。
猜你喜欢
  • 2021-08-03
  • 1970-01-01
  • 1970-01-01
  • 2017-06-06
  • 2022-08-07
  • 2010-12-08
  • 1970-01-01
  • 1970-01-01
  • 2020-09-02
相关资源
最近更新 更多