如何fork()同时进行两个进程

时间:2014-03-29 19:47:46

标签: c++ process fork inter-process-communicat

我在程序上取得了一些进展,允许它使用rand()%20在0到20之间运行一段随机时间。但是,我无法弄清楚如何fork()关闭多个进程,最多2个。

如果我正确理解fork(),我在程序中创建了1个父进程和1个子进程。

中的rand1值
else if (pid==0) {
mypid = getpid();
cout << "Child pid is " << mypid << "\n";
execlp("./idle","./idle","1",rand1,0); }
execlp中的

将为生成的任何内容创建并运行./idle进程,对吗?例如,如果rand1 == 19,则每个进程(父进程和子进程)将在程序完成之前运行19秒,不包括代码中的sleep()计时器。

现在我的主要问题是分支两个进程,以便我不仅仅留下1个父亲和1个孩子,而是留下2个父母和2个孩子,但是当前正在运行该程序。

我迷失了,希望这篇文章的未来回复可以得到一些指导。

谢谢!

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
#include <time.h>

using namespace std;
using std::cout;

volatile sig_atomic_t keep_going = 2;

void catch_child (int sig)
{ int status, p;
  p = waitpid(-1,&status,WNOHANG);
  if (p==0) return;
  keep_going=0;
  cout<<"Child process caught: "<< p <<endl;
  return;
}

int  main()
{
  pid_t pid;
  pid_t mypid;

  int rand1;
  int i;

  srand(time(NULL)); //init seed
  rand1 = rand()%20; //generates random number 0 - 20   

  cout << "This is the random number (rand1): " << rand1 << "\n"; //output to see how long 

  signal (SIGCHLD, catch_child); //when child is sent, run function catch_child

  pid = fork(); 
  if (pid<0) {
    cout<<"Error\n";
    return 0;
  }
  else if (pid==0) {
    mypid = getpid();
    cout << "Child pid is " << mypid << "\n";
    execlp("./idle","./idle","1",rand1,0);
  } else {
    mypid=getpid();

    cout<<"Parent procss is " << mypid << " and child is " << pid << "\n";
    sleep(3);

    cout<<"Pausing child " << pid <<" for " << rand1 << "  seconds\n";


    kill(pid,SIGSTOP);

    for (int i=0; i < rand1; i++) sleep(3);

    cout<<"Resuming child "<<pid<<"\n";
    kill(pid,SIGCONT);

    cout<<"Parent process pid is "<<mypid<<" waiting for child to stop\n";
    while (keep_going==rand1) sleep(2);

    cout <<"Parent knows that Child now done\n";

  }
  return 0;

} //keep 

更新

这是一个似乎是半工作程序的示例运行。附上的代码和关于样本运行的图像使用下面的更新代码。

samplerun

#include <iostream>
#include <unistd.h>
#include <sys/wait.h>
#include <cstdlib>
#include <time.h>

using namespace std;
using std::cout;

volatile sig_atomic_t keep_going = 2;

void catch_child (int sig)
{ int status, p;
  p = waitpid(-1,&status,WNOHANG);
  if (p==0) return;
  keep_going=0;
  cout<<"Child process caught: "<< p <<endl;
  return;
}

int  main()
{
  pid_t pid;
  pid_t mypid;

  int rand1;
  int i;

  srand(time(NULL)); //init seed
  rand1 = rand()%20; //generates random number 0 - 20   

  cout << "This is the random number (rand1): " << rand1 << "\n"; //output to see how long 

  signal (SIGCHLD, catch_child); //when child is sent, run function catch_child

  ; 
  for(int i=0;i<2;i++){
  pid = fork();

  if (pid<0) {
    cout<<"Error\n";
    return 0;
  }
  else if (pid==0) {
    mypid = getpid();
    cout << "Child pid is " << mypid << "\n";
    execlp("./idle","./idle","1",rand1,0); //does rand1 make the processes run for rand1's value?
  } else {
    mypid=getpid();

    cout<<"Parent procss is " << mypid << " and child is " << pid << "\n";
    sleep(3);

    cout<<"Pausing child " << pid <<" for " << rand1 << "  seconds\n";
    kill(pid,SIGSTOP); //SIGSTOP acts as a PAUSE

    for (int i=0; i < rand1; i++) sleep(3);

    cout<<"Resuming child " << pid << "\n";
    kill(pid,SIGCONT);

    cout<<"Parent process pid is "<<mypid<<" waiting for child to stop\n";
    while (keep_going==rand1) sleep(2);

    cout <<"Parent knows that Child now done\n";

  }
}
  return 0;
} 

0 个答案:

没有答案