c - c中unix_error函数的问题

标签 c linux unix error-handling

所以我在使用 unix_error 函数时遇到了问题,我相信我没有包含特定的文件,但我似乎无法在互联网上找到我需要包含的文件。有什么建议吗?

编辑:我写了这样的东西..

while((pid = waitpid(-1, NULL, 0)) > 0){
        printf("SERVER: Handler reaped child %d\n", (int) pid);
        child_count--;
    }
    if(errno != ECHILD){
        unix_error("waitpid error");
    }
    sleep(2);
    return;

尝试编译 unix_error 时出现错误

最佳答案

没有标准函数unix_error()

出于多种目的,您可以使用此代码。

unixerr.h

#ifndef UNIXERR_H_INCLUDED
#define UNIXERR_H_INCLUDED

extern void unix_error(const char *msg);

#endif /* UNIXERR_H_INCLUDED */

unixerr.c

#include "unixerr.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void unix_error(const char *msg)
{
    int errnum = errno;
    fprintf(stderr, "%s (%d: %s)\n", msg, errnum, strerror(errnum));
    exit(EXIT_FAILURE);
}

还有很多可以改进的地方。第一个方法是将程序名称添加到输出的开头。另一种方法是让函数支持可变长度参数列表,就像 printf() 那样。有些人不喜欢打印错误号;有些人不喜欢打印错误号。我更喜欢它。

关于c - c中unix_error函数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23035413/

相关文章:

c++ - C 和 C++ 标准是否暗示地址空间中的特殊值必须仅存在以表示空指针的值?

c++ - C++程序中的虚拟内存大小增加

c - GCC错误但VS没有错误

c - 如何确定文件描述符是否附加到 Linux 中的文件或套接字

c - 打开系统调用

c - nlopt 中的固定区间约束

c - OCIStmtPrepare 返回 OCI_INVALID_HANDLE

Android - 找不到命令

linux - 非常慢的脚本

linux - 在远程服务器上执行本地脚本/命令