Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/24.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
创建n个子进程链 在C++中,以n为输入,创建n个进程链,进程的输出应为父一->子1(PARTEN2)->HARD2(PARTEN3),通过递归函数IM能够生成输出,但不能退出循环,也需要帮助发送N个循环,循环应该中断。p>_C_Linux_Fork_Systems Programming - Fatal编程技术网

创建n个子进程链 在C++中,以n为输入,创建n个进程链,进程的输出应为父一->子1(PARTEN2)->HARD2(PARTEN3),通过递归函数IM能够生成输出,但不能退出循环,也需要帮助发送N个循环,循环应该中断。p>

创建n个子进程链 在C++中,以n为输入,创建n个进程链,进程的输出应为父一->子1(PARTEN2)->HARD2(PARTEN3),通过递归函数IM能够生成输出,但不能退出循环,也需要帮助发送N个循环,循环应该中断。p>,c,linux,fork,systems-programming,C,Linux,Fork,Systems Programming,下面是我的代码: #include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> int foo(const char *whoami) { printf("I am a %s. My pid is:%d my ppid is %d\n", whoami, getpid(), ge

下面是我的代码:

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

int foo(const char *whoami) {
    printf("I am a %s.  My pid is:%d  my ppid is %d\n", whoami, getpid(), getppid() );
    return 1;
}

int func() {
    pid_t pid=fork();
    if (pid==0) { /* only execute this if child */
        foo("child");
        pid_t pid=fork();
        if (pid==0) { /* only execute this if child */
            foo("child");
            func();
            exit(0);
        }
      }
      exit(0);
    }
    wait(0);  /* only the parent waits */
    return 0;     
}

int main(void){
    foo("parent");
    func(); 
    return 0;
}
#包括
#包括
#包括
#包括
#包括
int foo(常量字符*whoami){
printf(“我是%s。我的pid是:%d我的ppid是%d\n”,whoami,getpid(),getppid());
返回1;
}
int func(){
pid_t pid=fork();
如果(pid==0){/*仅当子级*/
富(“儿童”);
pid_t pid=fork();
如果(pid==0){/*仅当子级*/
富(“儿童”);
func();
出口(0);
}
}
出口(0);
}
等待(0);/*只有父级等待*/
返回0;
}
内部主(空){
富(母公司);
func();
返回0;
}

根据您所说的,我知道您有以下问题:

第一。您正试图将“数据”从一个进程发送到另一个进程

第二。您正在试图找到一种方法来阻止程序运行

现在是第一个。如果你想做到这一点,并且我理解正确,有两种方法可以实现。一个是使用共享内存,另一个是使用管道。共享内存在所做的事情上非常明显。管道正在获取进程的
stdout
,并在下一个进程中将其重定向为
stdin

现在你需要结束你的程序。子进程在执行命令(
exec
)或被告知执行命令(例如使用IF语句和返回)时执行。您可以根据自己的喜好创建一个声明,当子进程满足您的要求时,您可以使其死亡(还有一种方法可以使用
kill(pid,SIGKILL);
命令从子进程中杀死父进程

我没有向您提供任何代码,因为我不清楚您的问题的确切性质。
希望我的假设能让你有所收获!

你不能退出循环,原因很简单,那就是,你会无休止地产生子进程。每当你
fork()
一个新进程启动时,它就会再次分叉

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>

int n=5;

int foo(const char *whoami) {
    printf("I am a %s.  My pid is:%d  my ppid is %d\n", whoami, getpid(), getppid() );
    return 1;
}

int func(int n) 
{
    if (n == 0)
    { 
        return 0;
    }
    int pid = fork(); 
    if (pid == -1) {
        exit(0);
    }
    if (pid==0) { 
        foo("child");
        n = n-1;
        func(n);
        exit(0);
    }
    else {
       wait(NULL);
    } 
    return 0;   
}


int main()
{
    func(n); 
    return 0;
}

同时请删除java标记,然后选择C或C++标签。您的解释帮助我澄清了一些关于进程的疑惑。
I am a child. My pid is: 1159 my ppid is 1158
I am a child. My pid is: 1160 my ppid is 1159
I am a child. My pid is: 1161 my ppid is 1160
I am a child. My pid is: 1162 my ppid is 1161
I am a child. My pid is: 1163 my ppid is 1162