c - C 中的错误代码

标签 c gcc unistd.h

我的下面的代码有错误

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main()
{
        int i, status;
        pid_t child;
        child=fork();
        if(child == 0){
        for(i=0; i<10; i++){
            printf("\tChild PID = %d\n", getpid());
            printf("\tChild PPID = %d\n", getppid());
            sleep(1);
        }
        exit(0);
        }
        else{
        for(i=0; i<10; i++){
            printf("Parent PID = %d\n", getpid());
            printf("Parent PPID = %d\n", getppid());
        }
        }
        waitpid(child, &status, 0);
        return 0;
}

我在 GCC(Unix) 中编码,并收到以下错误:

test.c:27:1: error: expected identifier '(' before '}' token

有人能给我建议吗?预先感谢:)

最佳答案

waitpid() 的手册页指出:

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

无论如何,该错误可能是由于使用 sys/types.h 中定义的 pid_t 造成的。

使用-Wall打开所有编译器警告会指出缺少waitpid()原型(prototype)。

更新:这假设是 Linux。

关于c - C 中的错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12771151/

相关文章:

C、clock_gettime,返回了不正确的纳秒值?

c++ - 共享库中的全局变量如何链接?

openSUSE 13.1 中的 R 包

c - 我对 read() 和 write() 做错了什么?

c - 可以直接投写吗?

从 fcntl.h 读取函数中的 C++ 字符串

C/C++ 的 CMake 命令列表

c - Ruby sqlite3 gem 无法在 WSL 上编译

C - 打印没有标准库的参数

无法弄清楚为什么这个修剪功能不能正常工作