我的下面的代码有错误
#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/