C ++管理进程

时间:2012-02-28 18:48:25

标签: c++ linux process signals shared-memory

我有一个创建10个子流程的流程,它们共享一个内存段。 10个子流程可以同时运行,但我希望父亲在这段时间内睡觉。当孩子们完成后,他们会去睡觉。当所有的孩子都完成后,父亲做了一些计算并将孩子们唤醒了另一轮(他再次入睡,直到所有孩子都完成了计算)。我以为我可以在一个以check_children_finished()为条件的while循环中暂停()。孩子们知道通过kill()向父母发出信号并且他们有他的pid(作为参数发送给他们)并且实际上父亲也有子pid(存储在共享内存中)这里有一些代码 -


家长代码:


#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>

#include <sys/ipc.h> 
#include <sys/shm.h> 
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>

using namespace std;

static void sig_usr(int);
static volatile sig_atomic_t *sharedArray;
static int segmentId; 
int number_of_children = 10;

bool check_children_finished(){
  for (int i=1; i<number_of_children; i++)
    if (sharedArray[i*2] == -1)
      return false;
  return true;
}

void sig_usr (int signo)
{
  /*
  // This is signals handler (when a signal fires this is what runs)
  // we expect SIGUSR1 from the child process
  if(signo == SIGUSR1)
    cout << "(parent: " << (int)getpid() << ") --- caught SIGUSR1" << endl;
  if (signo == SIGUSR2){
    cout << "(parent: " << (int)getpid() << ") --- caught SIGUSR2" << endl;
  }else
    perror("unexpected signal fired");
  //  return;
  */
}


int main()
{

  // size of the shared memory array
  int arrSize = 100;
  const int shareSize = sizeof(sig_atomic_t) * (arrSize);
  /* Allocate shared memory segment */  
  segmentId = shmget(IPC_PRIVATE, shareSize, S_IRUSR | S_IWUSR); 
  sharedArray = (sig_atomic_t *) shmat(segmentId, NULL, 0);

  // feel shared memory with -1
  for (int i=0; i<arrSize; i++)
    sharedArray[i] = -1;

  // binding SIGUSR1 & SIGUSR2 to sig_usr method
  // we need to override SIGUSR2 because of group signaling
  signal(SIGUSR1, sig_usr);
  //  signal(SIGUSR2,sig_usr);

  fprintf(stderr, "\n (parnet) myPid=%d ; segId=%d\n",(int)getpid(), segmentId);
  sharedArray[0] = 123;
  int kids = 0; // this is the number of child processes
  // we send to the child (as shell parameters) the parent pid, shared segment address , and index to the shared memory(this is where he will write his pid and in index+1 the heuristic value)
  char* kidsParams[5];

  // takes care of param[0]=command to run
  string exec_line = "./child";
  kidsParams[0] = new char[exec_line.size()+1];
  memcpy(kidsParams[0], exec_line.c_str(), exec_line.size());

  // takes care of param[1]=parent pid
  kidsParams[1] = new char[100]; // = malloc(100*sizeof(char));
  sprintf( kidsParams[1],"%d",(int)getpid());

  // takes care of param[2]=shared mem segment address
  kidsParams[2] = new char[100];
  sprintf( kidsParams[2],"%d",segmentId);

  // takes care of param[3]=the child private index in shared mem
  kidsParams[3] = new char[100];

  kidsParams[4] = NULL;  // needed as end of array
  int index = 0;
  for(; kids<number_of_children; kids++) {
    sprintf( kidsParams[3],"%d",index);
    index+=2;
    pid_t childpid = fork();
    if(childpid==0){
      execv(kidsParams[0],kidsParams);
    }
   }
  cout << "(parent) --- just finished creating " << number_of_children << " kids" << endl;
  cout << "(parent) entering to while {...} pause" << endl;
  for (int i=0; i<number_of_children; i++)
    cout << "[" << i << "] = " << sharedArray[i];
  cout << endl;
  // going to sleep --- here I want while loop with conditioning that all children finished
  while ( ! check_children_finished() ) {
    cout << "(parent) now will signal the group" << endl;
    // killpg sends signal to the group (all the children). note that the group has the same pid as the father
    killpg(getpid(),SIGUSR2);
    cout << "(parent) just finished signaling the group" << endl;
    pause();
    for (int i=0; i<number_of_children; i++)
       cout << "[" << i << "] = " << sharedArray[i];
    cout << endl;
  }
  cout << "(parent) exited the while{...} paused" << endl;


  // removes shared memory
  //  shmdt (sharedArray);  
  //  shmctl (segmentId, IPC_RMID, NULL);  
  // note that all children must also shmctl (...IPC_RMID...);

}

这是一个子代码:


(same includes...)
using namespace std;

// declare the function proptotype (needed in signal function)
static void sig_usr(int);

// handles the signal (what happens when the signal fires) --- here I want to solve the search problem
void sig_usr (int signo)
{
  /*
  if(signo == SIGUSR1){
    cout << "(child: " << (int)getpid() << ") --- caught SIGUSR1" << endl;
  }else if(signo == SIGUSR2){
    cout << "(child: " << (int)getpid() << ") --- caught SIGUSR2" << endl;
  }else
    perror("eerrrr");
  //  return;
  */

}

int main(int argc, char** argv){
  // binding the signal to the handler
  signal(SIGUSR2,sig_usr);
  int segmentId;  
  volatile sig_atomic_t *sharedArray ;
  int myIndex;
  int myData =  5; 
  int parentPid;
  // read parameters
  parentPid = atoi(argv[1]);
  segmentId = atoi(argv[2]);
  myIndex = atoi(argv[3]);

  // declare a pointer to the shared memory
  sharedArray = (sig_atomic_t *) shmat(segmentId, NULL, 0);
  sharedArray[myIndex] =(int)getpid();
  sharedArray[myIndex+1] = myData;
  //  fprintf(stderr, "My Group Pid(child): %d\n",(int)getpgrp());
  cout << "(child: " << (int)getpid() << ") --- going to sleep" << endl;
  pause();
  cout << "(child: " << (int)getpid() << ") --- I woke up" << endl;
  //calc data

  //fprintf(stderr, "My Pid(child): %d\n",(int)getpid());
  //fprintf(stderr, "I got %d (child)\n",sharedArray[0] );


  // this signals the father
  kill(parentPid,SIGUSR1);
  cout << "fired SIGUSR1"<< endl;
}

以下是典型输出:


 (parnet) myPid=3104 ; segId=22872080
(parent) --- just finished creating 10 kids
(parent) entering to while {...} pause
[0] = 123[1] = -1[2] = -1[3] = -1[4] = -1[5] = -1[6] = -1[7] = -1[8] = -1[9] = -1
(parent) now will signal the group
User defined signal 2

有时我会得到类似的东西:


 (parnet) myPid=3126 ; segId=22937618
(child: 3129) --- going to sleep
(child: 3127) --- going to sleep
(parent) --- just finished creating 10 kids
(parent) entering to while {...} pause
[0] = 3127[1] = 5[2] = 3128[3] = 5[4] = 3129[5] = 5[6] = -1[7] = -1[8] = -1[9] = -1
(parent) now will signal the group
User defined signal 2
(child: 3127) --- I woke up
fired SIGUSR1
(child: 3128) --- going to sleep
(child: 3132) --- going to sleep
(child: 3129) --- I woke up
fired SIGUSR1

有人可以提出解决方案吗? 谢谢! - Liron

3 个答案:

答案 0 :(得分:4)

我建议在监控进程和其他工作进程之间使用管道,并使用简单的文本协议进行控制通信。

(因为管道上的文本协议比信号更可靠 - 可以“丢失”或“合并”)

所以,在分配工人之前,我会打电话(例如10次,或者两次* 10次,如果你想要他们两种方式)pipe(2)来建立控制沟通。然后,您可以使用ppoll(2)(或仅poll)来复用管道。

但您是否考虑使用现有框架,例如Open-MPI(执行 MPI =“消息传递接口”)。这并不意味着放弃使用共享内存段(仅使用MPI进行控制和同步问题)。或者也许使用OpenMPp-threads

MPI非常适用于高性能数值计算,许多超级计算机都使用它(使用专用硬件进行非常快速的消息传递)。

(当然,超级计算机是集群,因此您实际上并没有在数千个核心之间共享内存;您将通过在代码中仅使用MPI来利用它们。)

(您可能还会调查使用GPGPU至OpenCL

答案 1 :(得分:1)

同步进程的常用方法是使用信号量。

SysV使用semctl(),semop()和semget()提供了更复杂的信号量操作。 POSIX信号量(更简单)使用sem_open(),sem_close(),sem_post()和sem_wait()。

无论你使用哪个,在启动任何子进程之前在主进程中创建信号量,并在退出主进程后收到所有子进程后删除信号量。

每个子节点应该在信号量上执行sem_open()(不带O_CREAT),然后执行sem_wait(),它们将阻止你正确初始化信号量。当sem_wait()返回时,执行触及共享内存的代码,然后调用sem_post()。这应该为您的共享内存段提供独占的共享访问权限。

使用信号似乎是一个坏主意。我当然从来没有听说过那样做。

根据我自己的经验,我有一个单独的进程创建了所有SysV信号量,并将信号量id复制到共享内存段中的合适位置。这是所有过程所做的,然后退出。信号量是持久的 - 然后我会从命令行启动其他进程来打开信号量(没有O_CREAT)并执行sem_wait()。所以我没有使用fork / exec来启动子进程。与命令行分开的进程似乎更简单。

我不确定进程子进程是否继承信号量id,如继承文件描述符

答案 2 :(得分:0)

确实使用信号量会使事情变得更简单并最终起作用。请注意,知道必须使用-pthread标志进行编译(例如g ++ -Wall -pthread chikd.c -o parent)。 我确实有一个小问题,有没有人知道我能做些更好的事情然后睡觉(1)? (父代码,第109行)。

家长代码:

#include <csignal>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/ipc.h> 
#include <sys/shm.h> 
#include <sys/stat.h>
#include <unistd.h>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include <semaphore.h>
#include <fcntl.h>

using namespace std;

char SEM_NAME[] = "lir";

static void sig_usr(int);
static sig_atomic_t *sharedArray; // static volatile sig_atomic_t *sharedArray;
static int segmentId; 

int number_of_children = 10;
int child_init_ctrl = 0;
int child_finish_ctrl = 1;
int number_of_children_ctrl = 2;
int indexes_start_ctrl = 3;
// each child has its own : PID,DATA,ADDRESS_TO_HIS_INIT_STATE
int data_start = indexes_start_ctrl + number_of_children*3;

int getMyStartIndex(){
  return -1;
}

void sig_usr (int signo) {}

int main() {

  // create&init *new* semaphore
  sem_t *mutex;
  mutex = sem_open (SEM_NAME,O_CREAT,0644,1);
  if(mutex == SEM_FAILED) {
    perror("unable to create semaphore");
    sem_unlink(SEM_NAME);
    exit(-1);
  }

  // size of the shared memory array
  int arrSize = 100;
  const int shareSize = sizeof(sig_atomic_t) * (arrSize);
  /* Allocate shared memory segment */  
  segmentId = shmget(IPC_PRIVATE, shareSize, S_IRUSR | S_IWUSR); 
  sharedArray = (sig_atomic_t *) shmat(segmentId, NULL, 0);

  // fill shared memory with -1
  for (int i=0; i<arrSize; i++)
    sharedArray[i] = -1;

  // binding SIGUSR1 & SIGUSR2 to sig_usr method
  // we need to override SIGUSR2 because of group signaling
  signal(SIGUSR1, sig_usr);
  signal(SIGUSR2,sig_usr);

  sem_wait(mutex);
  sharedArray[child_init_ctrl] = 0;
  sharedArray[child_finish_ctrl] = 0;
  sharedArray[number_of_children_ctrl] = number_of_children;
  sem_post(mutex);

  // we send to the child (as shell parameters) the parent pid, shared segment address , and index to the shared memory(this is where he will write his pid and in index+1 the     heuristic value)
  char* kidsParams[6];

  // takes care of param[0]=command to run
  string exec_line = "./child";
  kidsParams[0] = new char[exec_line.size()+1];
  memcpy(kidsParams[0], exec_line.c_str(), exec_line.size());

  // takes care of param[1]=parent pid
  kidsParams[1] = new char[100]; // = malloc(100*sizeof(char));
  sprintf( kidsParams[1],"%d",(int)getpid());

  // takes care of param[2]=shared mem segment address
  kidsParams[2] = new char[100];
  sprintf( kidsParams[2],"%d",segmentId);

  // takes care of param[3]=the child private index in shared mem
  kidsParams[3] = new char[100];

  // takes care of param[4]=the child's first index of start state
  kidsParams[4] = new char[100]; // = malloc(100*sizeof(char));
  sprintf( kidsParams[4],"%d",getMyStartIndex());

  kidsParams[5] = NULL;  // needed as end of array

  // creates the child processes (and update child private index)
  int index = 3;
  for(int kids=0; kids<number_of_children; kids++) {
    sprintf( kidsParams[3],"%d",index);
    index+=3;
    pid_t childpid = fork();
    if(childpid==0){
      execv(kidsParams[0],kidsParams);
    }
  }

  while ( sharedArray[child_init_ctrl] != number_of_children ) {
    // waiting on the CPU !!! or going to sleep. is there a better solution?
    sleep(1);
  }

  // killpg sends signal to the group (all the children). note that the group has the same     pid as the father
  killpg (getpid(),SIGUSR2);

  // going to sleep --- here I want while loop with conditioning that all children finished
  bool all_child_finished_flag = false;
  while ( all_child_finished_flag == false ) {
    sem_wait(mutex);
    if (sharedArray[child_finish_ctrl] == number_of_children)
      all_child_finished_flag = true;
    sem_post(mutex);
    if (all_child_finished_flag == false)
      pause();
  }   


  // removes shared memory and delete semaphore
  shmdt (sharedArray);  
  shmctl (segmentId, IPC_RMID, NULL);  
  // note that all children must also shmctl (...IPC_RMID...);
  // removes the mutex
  sem_close(mutex);
  sem_unlink(SEM_NAME);

}

子代码:

// declare the function proptotype (needed in signal function)
static void sig_usr(int);
int child_init_ctrl = 0;
int child_finish_ctrl = 1;
int number_of_children_ctrl = 2;
int number_of_children=0;

// handles the signal (what happens when the signal fires)
void sig_usr (int signo) {}

char SEM_NAME[] = "lir";

int main(int argc, char** argv) {
    (same includes...)

      // create&init *existing* semaphore
      sem_t *mutex;
      mutex = sem_open (SEM_NAME, 0, 0644, 0);
      if(mutex == SEM_FAILED) {
        perror("reader:unable to execute semaphore");
        sem_close(mutex);
        exit(-1);
      }

      // binding the signal to the handler
      signal(SIGUSR2,sig_usr);

      volatile sig_atomic_t *sharedArray ;
      int myData=0;
      // read parameters
      int parentPid = atoi(argv[1]);
      int segmentId = atoi(argv[2]);
      int myIndex = atoi(argv[3]);
      int myStartState = atoi(argv[4]);

      // declare a pointer to the shared memory
      sharedArray = (sig_atomic_t *) shmat(segmentId, NULL, 0);
      sharedArray[myIndex] =(int)getpid();
      sharedArray[myIndex+1] = myData;
      number_of_children = sharedArray[number_of_children_ctrl];

      sem_wait(mutex);
      sharedArray[child_init_ctrl]++;
      sem_post(mutex);

      pause();

      // do some "calculations"
      sharedArray[myIndex+1] = myIndex;
      for (int i=0; i<10; i++)
        sharedArray[myIndex+1] += i;

      // this signals the father
      bool should_i_fire = false;
      sem_wait(mutex);
      sharedArray[child_finish_ctrl]++;
      if (sharedArray[child_finish_ctrl] == number_of_children)
        should_i_fire = true;
      sem_post(mutex);
      if (should_i_fire == true)
        kill(parentPid,SIGUSR1);
    }

最佳, - Liron