SIGCHLD を適切に処理しようとしていますが、ハンドラー内のプロセスの PID を取得できないため、構造内の 1 つのパラメーターの値を変更できます。
コードは次のとおりです。
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;
}