从主进程执行多个进程 [英] execute multiple processes from a master process

查看:190
本文介绍了从主进程执行多个进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要从一个主进程创建多个进程。我知道我想使用一个来自exec家族的函数,但它似乎并没有按我的意图的方式进行。看来exec()是一个阻塞调用,或者也许我只是使用它错了。无论如何,对代码:

  const char * ROUTERLOCATION =../../router; 
int main(int argc,char ** argv){
manager manager;
vector< string> instructions = manager.readFile(argv [1]);
...
//文件给出了我想要产生的进程数,并且该值在
中// rCount
for(int i = 0; i < rCount; i ++){
cout<< creating:< i<< endl;
execl(ROUTERLOCATION,,NULL);
}
}

我看到的输出是:

 创建:0 
HI!

然后一切顺利退出。我可以不使用 execl()


产生多个进程同时,我想与每个

解决方案

这些进程正在运行,因此我不想阻止。 $ c> fork 在您的主进程中,在您的子进程中调用 execl 。 ( exec 系列函数会用您的新进程替换当前的过程映像,因此您的for循环将永远不会完成。)


I want to create multiple processes from one master process. I know I want to use a function from the exec family, but it does not seem to be preforming in the way I intended it to. It seems that exec() is a blocking call, or maybe I am just using it wrong. Anyway, on to the code:

const char* ROUTERLOCATION = "../../router";
int main(int argc, char** argv) {
  manager manager;
  vector<string> instructions = manager.readFile(argv[1]);
  ...
  //file gives me the number of proceses i want to spawn and that value goes in 
  //rCount
  for(int i = 0; i < rCount; i++){
    cout << "creating:" << i << endl;
    execl(ROUTERLOCATION, "",NULL);
    }
}

The output I see is:

creating:0
HI!!!

And then everything exits gracefully. Can I not spawn more than one process using execl()?

Also, I would like to communicate with each of these processes, so I don't want to be blocking while these processes are running.

解决方案

You need to fork in your master process, the in your child processes call execl. (exec family of functions replaces your current process image with your new process, so hence why your for loop never completes.)

这篇关于从主进程执行多个进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆