大佬教程收集整理的这篇文章主要介绍了c – fork和exec许多不同的进程,并从每个进程获得结果,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
pid_t pID = fork(); if (pID == 0){ int proc = execl(BOLDAGENT,BOLDAGENT,"-u","2","-c","walkevo.xml",null); std::cout << strerror(errno) << std::endl; }
例如,我如何通过PID控制哪个子(根据参数xml文件)获得了哪个结果(通过管道或标准输出),从而采取相应的行动?我是否必须将子进程封装在一个对象中,然后从那里开始工作,还是可以将它们完全分组?
在每个fork()之后,它将返回一个孩子的PID.您可以存储此pid和相关信息,如下所示:
#define MAX_CHILD=8 pid_t pids[MAX_CHILD]; int pipe_fd[MAX_CHILD]; for(int child=0;child<MAX_CHILD;child++) { int pipe[2]; /* create a pipe; save one of pipe fd to the pipe_fd[child] */ int ret; ret = fork(); if(ret) { /* parent */ /* close alien half of pipe */ pids[child] = ret; /* save the pid */ } else { /* child */ /* close alien half of pipe */ /* We are child #child,exec needed program */ exec(...); /* here can be no more code in the child,as `exec` will not return if there is no error! */ } } /* there you can do a `SELEct` to wait data from several pipes; SELEct will give you number of fd with data waiTing,you can find a pid from two arrays */
以上是大佬教程为你收集整理的c – fork和exec许多不同的进程,并从每个进程获得结果全部内容,希望文章能够帮你解决c – fork和exec许多不同的进程,并从每个进程获得结果所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。