C 处理O/pipes的父子级

C 处理O/pipes的父子级,c,process,fork,C,Process,Fork,我有一个用管道创建流程的建议,我已经构建了20个子流程。它起作用了!但最复杂的事情是满足以下要求: 我必须为每个孩子创建一个孙子,每个孙子都有一对数字(c.e.2nd,4rth,6th,…),最后我必须为每个孙子创建一个可以被6整除的曾孙。(c.e。 第六个孙子,第十二个,第十八个) 很抱歉,我是unix和并发程序的新手。这里是我的简单代码作为开始的基础 守则: #include <unistd.h> #include <sys/types.h> main(){ pid_

我有一个用管道创建流程的建议,我已经构建了20个子流程。它起作用了!但最复杂的事情是满足以下要求:

我必须为每个孩子创建一个孙子,每个孙子都有一对数字(c.e.2nd,4rth,6th,…),最后我必须为每个孙子创建一个可以被6整除的曾孙。(c.e。 第六个孙子,第十二个,第十八个)

很抱歉,我是unix和并发程序的新手。这里是我的简单代码作为开始的基础

守则:

#include <unistd.h>
#include <sys/types.h>
main(){
pid_t pid;
int i, n=20;

for (i=0; i<n; i++) {
 pid=fork();
 if (pid == 0) break;
}
printf(“\n The father in the process %d is %d”, getpid(),getppid());
}
#包括
#包括
main(){
pid_t pid;
int i,n=20;

对于(i=0;i未测试,但我认为这符合您的要求:

#include <unistd.h>
#include <sys/types.h>
main(){
pid_t pid;
pid_t spid;
pid_t sspid;
int i, n=20;

for (i=0; i<n; i++) {
    pid=fork();
    if (pid == 0){
        // Son process
        if(i%2 == 0){
            //Son process && even
            spid = fork();
            if (spid == 0){
                // Grand son process
                if(i%3 == 0){
                    sspid = fork();
                    if (sspid == 0){
                        // Great grand son process
                    } else {
                        // Grand son process
                    }
                }
            }
        }
        break; // to avoid continuing the for in the child processes
    } else {
        // Main process
    }
 }
 printf(“\n The father in the process %d is %d”, getpid(),getppid());
}
#包括
#包括
main(){
pid_t pid;
pid_t spid;
pid_t sspid;
int i,n=20;

对于(i=0;iHi再次!!我测试了它,但仍然没有运行。我编译了它,这是错误:“未定义对fork()的引用”和“未定义对getpid()的引用”在windows上这可能会有所帮助:在linux上,我想你不会遇到这个错误。在其他op系统上,我不知道。