C++ fork() -- 创建进程“列表”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16890546/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:44:13  来源:igfitidea点击:

C++ fork() -- creating a "list" of processes

c++fork

提问by user2262230

I have a program that creates new processes "one by one". Is it possible to change this code so it creates a "list" of processes – i.e. child 1 being the parent of child 2, child 2 being the parent of child 3, etc.?

我有一个程序可以“一个一个”地创建新进程。是否可以更改此代码以创建进程的“列表”——即子进程 1 是子进程 2 的父进程,子进程 2 是进程子进程 3 的父进程,等等?

#include <string>
#include <iostream>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "err.h"

using namespace std;
int main ()
{
 pid_t pid;
 int i;

 cout << "My process id = " << getpid() << endl;

 for (i = 1; i <= 4; i++)
  switch ( pid = fork() ) {
  case -1:
    syserr("Error in fork");

  case 0:
    cout << "Child process: My process id = " << getpid() << endl;
    cout << "Child process: Value returned by fork() = " << pid << endl;
    return 0;

  default:
    cout << "Parent process. My process id = " << getpid() << endl;
    cout << "Parent process. Value returned by fork() = " << pid << endl;

   if (wait(NULL) == -1) 
   syserr("Error in wait");

 }  
 return 0;
 }

回答by e2-e4

If you want to keep the loop in order to dynamically set the depth of the forktree,

如果要保持循环以动态设置fork树的深度,

// Set DEPTH to desired value

#define DEPTH 4

int main ()
{
  pid_t pid;
  int i;

  cout << "My process id = " << getpid() << endl;

  for (i=1 ; i <= DEPTH ; i++) {

    pid = fork();    // Fork

    if ( pid ) {
       break;        // Don't give the parent a chance to fork again
    }
    cout << "Child #" << getpid() << endl; // Child can keep going and fork once
  }

  wait(NULL);        // Don't let a parent ending first end the tree below
  return 0;
}

Output

输出

My process id = 6596
Child #6597
Child #6598
Child #6599
Child #6600

回答by rjv

Use forkin a set of nested ifs

使用fork一组嵌套的if小号

#include<stdio.h>
int main()
{   
printf("Parent PID %d\n",getpid());
if(fork()==0)
{
    printf("child 1 \n");
    if(fork()==0)
    {
        printf("child 2 \n");
        if(fork()==0)
            printf("child 3 \n");
    }

}
return 0;
}    

Output

输出

Parent PID 3857
child 1
child 2
child 3

父 PID 3857
子 1
子 2
子 3

For n processes,

对于 n 个进程,

#include<stdio.h>
void spawn(int n)
{
if(n)
{
    if(fork()==0)
    {
        if(n)
        {
            printf("Child %d \n",n);
            spawn(n-1);
        }
        else
            return;
    }
}
}
int main()
{   
printf("Parent PID %d\n",getpid());
int i=0;
spawn(5);
return 0;
}    

回答by Javad

int make_proc(int counter, int parent){
pid_t x=getpid();
std::cout << counter << " process "<< x << " : parent" << parent<< std::endl;


    if (counter==0) {
       return 1;
    }
    else {
        counter=counter-1;
        pid_t pid=fork();
       if (pid==0) return make_proc(counter, x);
       wait(NULL);

    }

}

--------------------

int main(int argc, char **argv)
{
    int x=getpid();
    make_proc(10, x);
    return 0;
}