Search code examples
cparent-childpidtcpserverprocess-reaper

How to squeeze in additional parameters to a reaper function when a parent is signalled to kill a child (c)?


I'm writing a TCP server that functions very much like a chatroom and came across this question.

When a user connects, a child process is created to serve the user.
When a user logs in, I store his username into a text file, online.txt
But when a user logs out, I need to remove the user from online.txt(PROBLEM), the parent then signals a reaper() and kills the child.

My questions are:

Q1: How I can squeeze in additional information to the reaper (such as the username that the user used to log in) so that it can also remove the user from online.txt? Or is there another better method to do so?

Q2: where does sig in reaper() gets its value from? Can I add additional parameters to the reaper?

Q3: Could I use the child's pid as a some sort of primary key for login.txt? If so, how can I retrieve the child's pid during reaper(), which is called by the parent?

The reaper looks like this:

void    reaper(int sig)//where does sig come from?
{
int status;

while (waitpid(-1, &status, WNOHANG) >= 0)
    ;
}

The signal used by the parent looks like this:

(void) signal(SIGCHLD, reaper);//how can I add more parameters?

Thank you in advance, I hope asking 3 questions at once isn't too greedy.
Any insight on ANY of the questions will be greatly appreciated.


Solution

  • As far as I can gather from your question, the parent process registers reaper() as a handler for SIGCHLD. When it detects a login, it writes the username to file, and spawns a child.

    On logoff the reaper() function is called because the child process has detected the log off and so exited, right?

    If so, why not just have the server maintain a data structure mapping PID to username. Then take the return value from waitpid and identify which username needs to be removed from the file.

    So, to summarise:

    1) No. Yes.

    2) From the signal received by the handler. No.

    3) Yes. From the return value of waitpid().