Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ wait()返回0和errno=";中断系统调用“;_C++_Process_Wait_System Calls_Waitpid - Fatal编程技术网

C++ wait()返回0和errno=";中断系统调用“;

C++ wait()返回0和errno=";中断系统调用“;,c++,process,wait,system-calls,waitpid,C++,Process,Wait,System Calls,Waitpid,我正在做一个学校作业,基本上包括创建两个子进程,这些子进程近似于pi,并将其结果写入一个与子进程id同名的文本文件。然后,父进程等待所有子进程,并根据存储在子进程id中的值计算pi的平均近似值文本文件 事情运行得很好,但偶尔(大约10%的时间我执行程序)wait(NULL)返回-1,并且errno表示其中一个子进程的系统调用中断。我不知道为什么这看起来是随机发生的 在下面的代码中,MonteCarlo是一个处理Pi近似值的自定义类 #include <random> #include

我正在做一个学校作业,基本上包括创建两个子进程,这些子进程近似于
pi
,并将其结果写入一个与子进程id同名的文本文件。然后,父进程等待所有子进程,并根据存储在子进程id中的值计算
pi
的平均近似值文本文件

事情运行得很好,但偶尔(大约10%的时间我执行程序)
wait(NULL)
返回-1,并且
errno
表示其中一个子进程的
系统调用中断。我不知道为什么这看起来是随机发生的

在下面的代码中,
MonteCarlo
是一个处理Pi近似值的自定义类

#include <random>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <unistd.h>
#include "MonteCarlo.h"


int main(int argc,char** argv){
    int approximationProcessCount = 10;
    int approximationRandomCount = 1000;


    for(int i=0; i<approximationProcessCount; i++){
        //pidArray[i] = fork();
        pid_t pid = fork();

        if( pid == -1 ){
            // Fork error
            perror("Fork error");
        }
        else if( pid == 0 ){
            // Child process
            MonteCarlo mc(approximationRandomCount);
            std::string filename(std::to_string(getpid()) + ".txt");
            std::ofstream fs( filename );

            if( !fs ){
                std::cerr << "Cannot open '" << filename << "' for writing." << std::endl;
                return 1;
            }

            fs << mc.approximate();
            fs.flush();
            fs.close();
            return 0;
        }
        else{
            // Parent process
        }
    }

    // Parent process
    double averageApproximation = 0.0;

    for(int i=0; i<approximationProcessCount; i++){
        pid_t childPid = wait(NULL);

        if( childPid == -1 ){
            perror( std::string("Error when waiting for child process [-1] (i=" + std::to_string(i) + ")").c_str());
            continue;
        }
        else if( childPid == 0 ){
            perror( std::string("Error when waiting for child process [0] (i=" + std::to_string(i) + ")").c_str());
            continue;
        }

        std::string filename(std::to_string(childPid) + ".txt");
        std::ifstream fs( filename );

        if( !fs ){
            std::cerr << "Cannot open '" << filename << "' for reading." << std::endl;
            return 1;
        }

        double approximation;
        fs >> approximation;
        fs.close();
        remove(filename.c_str());

        // Calculate average "on the fly"
        averageApproximation += (approximation - averageApproximation)/(i+1);
    }

    std::cout << "Average approximation of Pi = " << averageApproximation << std::endl;

}

我正在Mac OSx El Capitan上的XCode中运行此程序。

您在这里检查过吗?这是一个非常糟糕的重复,@molbdnilo。
errno
的值是无关的,除非前一个系统调用返回了-1。你的
perror()
呼叫无效。@Hayt链接的答案解决了我的问题,重复的答案让我更加困惑……你检查过这里吗?这是一个非常糟糕的重复,@molbdnilo。
errno
的值是无关的,除非前一个系统调用返回了-1。你的
perror()
呼叫无效。@Hayt链接的答案解决了我的问题,重复的答案让我更加困惑。。。
Error when waiting for child process [-1] (i=9): Interrupted system call
Average approximation of Pi = 3.14444