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

2

0

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.

Retainer answered 9/12, 2010 at 15:20 Comment(2)
A parent is signalled to kill a child with a reaper... Sometimes I wonder what harmless and unknowing bystanders will think when they happen to look over your shoulder while you're typing a question like this? And what will then happen to you and how you will explain this when they call the police...Repose
I suppose I'll be banned from this forum in no time.Retainer
I
2

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().

Inofficious answered 9/12, 2010 at 15:29 Comment(2)
Thank you very much for your insight! The documentation for waitpid() was so long that I didn't finish reading it till the return values section. BTW, I use multi-processes because it's a concurrent server. The log off action is performed by another terminal.Retainer
Actually, you could see from the function declaration that the return type is pid_t. Even if it is a concurrent server, you can do it using multi-threading (yeuch) or, even better, single threaded. I prefer single threaded myself. I knew a company made a single threaded webserver, but they cheated by basically using their own multi-threading code in effect. Anyway it is possible to do the whole thing single threaded which has soem advantages, although multi-process can be good too. It depends on exactly how you want your chatroom to function.Inofficious
R
1

Question 1: Would configuring a signal handler for your child process to execute some particular action be appropriate? However perhaps a better solution would be to not use a file, rather an in memory construct to store what users are logged in. That way the reaper could just delete the entry out of memory, or even the proposed signal handler.

Question 2: I'm not familiar with your OS or architecture, but I would guess that SIGCHLD would be passed into reaper( int sig ) for the parameter value.

Question 3: Getting the pid is os specific. For POSIX types it's usually getpid(), from unistd.. However I would question if you really want to be doing that with a file.

Your solution can become vulnerable to race conditions when you start signaling all over the place... which lends itself to a security risk.

Fellow users, please feel free to correct me. In the search for wisdom one must accept instruction.

Remsen answered 9/12, 2010 at 15:36 Comment(2)
Thank you very much for your insight! Especially Q1! I shall do more research on that topic!Retainer
I think he wants the PID of the dead child, so he needs waitpid() not getpid() which will just return the PID of the current process. fork() will return the PID of the child in the first place. I don't see how he will have race conditions if only one process (the parent) is managing the file. You are right that storing the data in memory is best - I figured he had a specific reason to be using a file.Inofficious

© 2022 - 2024 — McMap. All rights reserved.