我正在尝试正确处理 SIGCHLD,但我无法在处理程序内获取进程的 PID,因此我可以更改结构内一个参数的值。
这是代码:
typedef struct
{
int active_state;
int pid;
}Worker;
typedef struct
{
Worker worker;
int used_state;//this can be USED or UNUSED
}WorkersTable;
WorkersTable *table; //I put this in a shared memory map and works correctly
这是处理程序的代码。在此文件中,有一个名为dead_child_pid的全局变量,我想存储要使用的死孩子的 pid。
void handler_SIGCHLD(int signal)
{
pid_t child_pid;
int e;
do
{
child_pid=wait3(&e,WNOHANG,NULL);
}while(child_pid>(pid_t)0);
mark_unused=1;
}
当调用 handler_SIGCHLD 并且最后我们设置 mark_unused=1 时,将访问以下代码:
if(mark_unused)
{
/*put the current position at the table as unused*/
int counter_find_pid=0;
while(counter_find_pid<MAX_WORKERS&&table[contador_find_pid].used_state==USED&&table[counter_find_pid].worker.pid!=dead_child_pid)
{
counter_find_pid++;
}
table[counter_find_pid].used_state=UNUSED;
}