Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.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++ c++;随机数,并在处理时等待所有子项的父项_C++_C_Process - Fatal编程技术网

C++ c++;随机数,并在处理时等待所有子项的父项

C++ c++;随机数,并在处理时等待所有子项的父项,c++,c,process,C++,C,Process,这是我的密码 #include <iostream> #include <string> #include <ctime> int main() { for (int i=0;i<3;i++) { if((pid()==fork())==0){ break; } } if(pid==0) { // son process sra

这是我的密码

    #include <iostream>
#include <string>
#include <ctime>

int main()
{

    for (int i=0;i<3;i++) {
        if((pid()==fork())==0){
            break;
        }
    }

    if(pid==0) { 
        // son process
        srand(time(NULL));
        int r = rand()%4+1;
        cout<<"pid:"<<getpid() <<"num": "<<r<<endl;
    }
    else{ 
        //parent process
    }

}
#包括
#包括
#包括
int main()
{
对于(int i=0;i
  • time(NULL)+getpid()
    放入所有随机数的不同种子

  • if((pid()==fork())==0)
    -这个构造很有趣……我怀疑这段代码能否编译。如果希望每个进程中的
    srand()
    得到不同的随机数,则需要在每个进程中为它们指定不同的参数。因为所有进程都在同一时间启动,
    time(NULL)
    将是相同的。您应该一次只问一个问题,而不是三个不同的问题。即第一个过程给出srand(2),第二个srand(3)等等…?让我们假设,我希望所有进程在特定的时间内运行,10秒,直到它们完成运行。我应该在创建上面声明变量并进行while循环,如果是,在哪里?