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.