Linux创建父子进程出现error: ‘fork‘ was not declared in this scope pid=fork();
Linux创建父子进程出现error: 'fork' was not declared in this scope pid=fork();
·
实验要求在Linux系统中创建父子进程,现在windows系统,vscode中敲了个代码方便找bug,发现出现报错(源代码附文末)
error: 'fork' was not declared in this scope
pid=fork();
后续放到Linux中编译运行成功。
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
pid_t pid;
char*msg;
int k;
printf("观察父子进程执行的先后顺序,了解调度算法的特征\n");
pid=fork();
switch(pid) {
case 0:
msg="子进程在运行";
k=3;
break;
case -1:
msg="进程创建失败";
break;
default:
msg="父进程在运行";
k=5;
break;
}
while(k>0)
{
puts(msg);
sleep(1);
k--; }
exit(0);
}
更多推荐
已为社区贡献1条内容
所有评论(0)