c - 需要在 SIGCHLD 处理程序期间访问子进程的变量

标签 c signals parent-child

我在开头声明这是一项家庭作业,并且在编写 SIGCHLD 处理程序时我陷入了困境。我需要访问子进程中的变量。

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


#define TEXT_LEN 10
#define NUM_RESCUE_PLOWS 4
#define NUM_VICTIMS_TO_RESCUE 40


/*  PURPOSE:  To keep trap of the number of victims that have been rescued.
 */
int numRescuedVictims = 0;



/*  PURPOSE:  To keep track of the process id's of the rescuing snow plows.
 */
pid_t plowPid[NUM_RESCUE_PLOWS];


/*  PURPOSE:  To note that at least one child plow has finished.  Reports the
 *process id and the number of victims rescued for each child plow.
 *'sigNum' tells the signal number.  No return value
 */
//  You may want to define a SIGCHLD listener here
//  It should have a loop that wait()s for children and
//  and prints how many victims each has rescued.
void listenSIGCHLD(int sig)
{
  int status;
  pid_t pidWait;
  while((pidWait = wait(&status)) > 0)
    printf("Plow %d rescued %d victim(s)", plowId, numVictimsRescued);
}


/*  PURPOSE:  To handle being informed of a rescued victim.  'sigNum' tells
 *signal number.  No return value.
 */
//  You may want to define a SIGUSR1 listener here
//  It increments 'numRescuedVictims' and prints the total number rescued victims
void listenSIGUSR1(int sig)
{
  numRescuedVictims += &numVictimsRescued;
  printf("We've rescued %d victims!" numRescuedVictims);
}


/*  PURPOSE:  To make 'NUM_RESCUE_PLOWS' processes to run 'rescuingPlow' to
 *rescue stuck victims, and then tell them to quit after all
 *'NUM_VICTIMS_TO_RESCUE' victims have been rescued.  Ignores parameters.
 *Returns 'EXIT_SUCCESS' to OS.
 */
int main ()
{

  //  I.  Applicability validity check:


  //  II.  Rescue victims:

  //  II.A.  Install 'SIGUSR1' handler:
  signal(SIGUSR1, listenSIGUSR1);

  // Install 'SIGUSR1' handler
  //  Install your SIGCHLD handler here
  signal(SIGCHLD, listenSIGCHLD);

  //  II.B.  Tell NUM_RESCUE_PLOWS plows to start rescuing the victims:

  int i;
  int myPid= getpid();

  for  (i = 0;  i < NUM_RESCUE_PLOWS;  i++)
    {
      //  Do a fork() and save it in plowPid[i]
      plowPid[i] = fork();
      //  If plowPid[i] is less than 0 then do:
      if(plowPid[i] < 0)
    {
      fprintf(stderr,"Dude, your system is WAY to busy to play rescuer!\n");
      return(EXIT_FAILURE);
    }

      //  If plowPid[i] is equal to 0 then do:
      else if(plowPid[i] == 0)
    {
      char pidText[TEXT_LEN];
      char indexText[TEXT_LEN];
    }
      snprintf(pidText,TEXT_LEN,"%d",myPid);
      snprintf(indexText,TEXT_LEN,"%d",i);
      execl("./rescuingPlow","rescuingPlow",pidText,indexText,NULL);
      fprintf(stderr,"Dude, somebody stole my plow!!\n");
      return(EXIT_FAILURE);
    }


  //  II.C.  Wait until all victims have been rescued:

  while  (numRescuedVictims < NUM_VICTIMS_TO_RESCUE)
    {
      sleep(1);
      printf("Searching for victims . . .\n");
    }


  //  III.  Finished:

  //  Loop to send SIGTERM to all NUM_RESCUE_PLOWS plow processes
  for (int i = 0; i < NUM_RESCUE_PLOWS; i++)
    {
      kill(plowPid[i], SIGTERM);
    }
  int toSleep= NUM_RESCUE_PLOWS;

  //  sleep() can be interrupted by SIGCHLD.  Whenever it is interrupted
  //  it returns the number of seconds that still remain on its alarm
  //  clock.  Let's wait until it has slept its full amount incase it
  //  was prematured interrupted by SIGCHLD.
  do
    {
      toSleep= sleep(toSleep);
    }
  while  (toSleep > 0);

  printf("Ready for the NEXT snow storm!\n");
  return(EXIT_SUCCESS);
}

`

这就是最终生成的进程。虽然我还没有解决这个问题。

/*
 *  rescuingPlow.c
 *
 *  Compile with $ gcc rescuingPlow.c -o rescuingPlow
 */
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>


/*  PURPOSE:  To keep track of the number of victims that this process
 *  rescued.
 */
int numVictimsRescued   = 0;



/*  PURPOSE:  To return the number of victims rescued to the OS.  'sigNum'
 *  tells the signal number.  No return value.
 */
//  You may want to write a SIGTERM handling function
//  that returns to the OS 'numVictimsRescued'.



/*  PURPOSE:  To rescue victims at random intervals and inform parent process
 *  by sending it SIGUSR1 until receiving SIGTERM.  First parameter (after
 *  program name) tells parent's process id.  Second parameter tells this
 *  plow's index.
 */
int main    (int argc, char* argv[])
{

  //  I.  Applicability validity check:

  pid_t parentPID;
  int   plowId;

  if  (argc < 3)
  {
    fprintf(stderr,"USAGE: rescuingPlow <parentPID> <plowId>\n");
    return(EXIT_FAILURE);
  }

  parentPID = atoi(argv[1]);
  plowId    = atoi(argv[2]);


  //  II.  Rescuing victims until told to stop:

  //  II.A.  Install signal handler:

  //  Install your SIGTERM handler here
  srand(plowId);  //  Uniquely initialize random number generator so they act independently of each other


  //  II.B.  Rescue victims:

  //  Write an endless loop that:
  //  (1) Does 'sleep((rand() % 6) + 1);'
  //  (2) Increments 'numVictimsRescued'
  //  (3) Does 'printf("Plow %d rescued %d victim(s)!\n",plowId,numVictimsRescued);'
  //  (4) Send 'SIGUSR1' to 'parentPID'


  //  III.  Finished:

  return(EXIT_SUCCESS);
}

我不完全确定我要处理这个问题或如何处理这个问题。我非常有信心我能够解决大多数其他存在的问题。

最佳答案

如果不使用某些 IPC 机制,子进程只能将 8 位的值传递给父进程。

这 8 位由子进程作为调用 exit() 的参数发送,并由父进程通过将宏 WEXITSTATUS() 应用于以下值来接收成功调用 wait()waitpid() 返回的 status。请参阅 man 2 exitman 2 wait 了解详细信息。

如果我没记错的话,8 位是标准定义的最小值。某些实现可能允许更多位。

更新:

如何使用wait()的示例:

int child_exit_code = -1;
int status = -1;
pid_t pid = wait(&status);
if (-1 != pid)
  child_exit_code = WEXITSTATUS(status);

关于c - 需要在 SIGCHLD 处理程序期间访问子进程的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16392762/

相关文章:

C++继承问题错误C2084函数已经有一个主体

Nhibernate 不会从集合中删除 child

c - C89、C90 或 C99 中的所有函数都需要原型(prototype)吗?

c - 通过 CAN 总线(CANopen?)广播文件传输

c - 关于 sigprocmask()、SIG_BLOCK 和 SIG_SETMASK

python错误抑制信号18到win32

matlab - 从噪声信号/电压测量中提取神经元尖峰时间的最佳方法

java - 动态添加多个 TextViews 到 LinearLayout

c - `const` 限定类型可以出现在 C99 程序中的哪些位置

c - 如何在 C 语言中访问给定字节的特定位?