不会写代码的鹅崽 2023-02-14 21:33 采纳率: 100%
浏览 92
已结题

[Error] 'fork' was not declared in this scope

进程互斥实验
头文件已包含该函数,但是编译时仍然报错
之前也是这么写的但是没有报错
代码

#include<iostream>
#include<unistd.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
int main()
{
    pid_t pid1=-1;
    pid1=fork();
    if(pid1>0)
    {
        pid_t pid2=fork();
    if(pid2<0)
    {
        cout<<"perror"<<endl;
    }
    if(pid2>0)
    {
        lockf(1,1,1);
        int son ,father;
        son=getpid();
        father=getppid();
        cout<<"father pid:"<<son<<""<<"其父进程 pid:"<<father<<endl;
        for(int i=1;i<=10;i++) 
        {
            cout<<"father"<<i<<endl;
            sleep(3);
        }
        lockf(1,0,1);
    }
    if(pid2==0)
    {
        lockf(1,1,1);
        int son ,father;
        son=getpid();
        father=getppid();
        cout<<"son pid:"<<son<<" "<<"其父进程 pid:"<<father<<endl;
        for(int i=1;i<=10;i++)
        {
            cout<<"son "<<i<<endl;
            sleep(4);
        }
        lockf(1,0,1);
    }
    }
if(pid1==0)
{
    lockf(1,1,1);
    int son ,father;
    son=getpid(); 
    father=getppid();
    cout<<"daughter pid:"<<son<<""<<"其父进程 pid:"<<father<<endl;
    for(int i=1;i<=10;i++)
    {
        cout<<"daughter "<<i<<endl;
        sleep(5);
    }
    lockf(1,0,1);
}
}

img

img

  • 写回答

2条回答 默认 最新

  • 梦想橡皮擦 Python领域优质创作者 2023-02-14 21:50
    关注

    方案来自 梦想橡皮擦 狂飙组基于 GPT 编写的 “程秘”


    • 这个错误提示表明在当前的作用域中,编译器找不到名为“fork”的函数或变量的定义。

    • 这可能是由于头文件包含有误或者编译器环境发生变化导致的。

    • 建议确认头文件是否正确包含并且确保编译器环境没有发生变化,如修改了编译器版本或编译选项等。

    • 另外,如果您使用的是C++编译器,还需要确认头文件是否使用了C++语法,如需要在头文件包含处添加extern "C"来指定使用C语言语法。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月26日
  • 已采纳回答 2月18日
  • 修改了问题 2月14日
  • 创建了问题 2月14日

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法