繁体   English   中英

C ++随机数并在处理时等待所有孩子的父母

[英]c++ random number and wait parent to all children when 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
        srand(time(NULL));
        int r = rand()%4+1;
        cout<<"pid:"<<getpid() <<"num": "<<r<<endl;
    }
    else{ 
        //parent process
    }

}

我在所有过程中都遇到了问题随机数,如何重置rand()? 我已经尝试过对每个无效的进程使用不同的srand()。

  1. time(NULL)+getpid()放到所有兰特的不同种子上;
  2. http://man7.org/linux/man-pages/man3/pthread_join.3.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM