I'm trying to handle SIGCHLD properly, but I don't manage to get the PID of the process inside the handler so I can change the value of one parameter inside an structure.
Here is the code:
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
Here is the code for the handler.Inside this file there is a global variable called dead_child_pid that I want to to to store the pid of the dead child to be used alter.
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;
}
When handler_SIGCHLD is called and at the end we I put mark_unused=1, the following code is accessed:
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;
}