不会写代码的鹅崽 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 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等