我们应该使用在课堂上提供给我们的代码来了解叉的工作方式以及我们在不同输入中看到的差异。我唯一的问题是,当我尝试运行代码时,我收到语法错误,所以我不确定自己做错了什么。任何帮助或指导,非常感谢。语法错误testfork C代码
编辑:忘了澄清我是如何运行这个。学校有一台我连接到的UNIX机器来运行它。编译它只是一个问题,其文件名运行它之后是testfork.c
错误输出:
./testfork.c: line 6: syntax error near unexpected token `('
./testfork.c: line 6: `int main(int argc, char *argv[])'
码块:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
pid_t childpid; int count;
if (argc < 2) {
printf("usage: testfork count\n");
return -1;
}
else count = atoi(argv[1]);
childpid = fork();
if (childpid == -1) {
printf("Error in fork; program terminated\n");
return -1;
}
if (childpid != 0) {
/*Code executed by parent process*/
int i;
/*The lines below will avoid output interleaving*/
/*int status;*/
/*wait(&status);*/
for (i = 0; i < count; i++)
printf("parent process\n");
}
else {
/*Code executed by child process*/
int j;
for (j = 0; j < count; j++)
printf(" CHILD PROCESS\n");
}
return 0;
}
你是如何试图运行该程序中的任何语法错误? – jwodder
我假设你使用'sh'来运行它,对吗? –