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

纵然是瞬间 提交于 2019-12-13 04:06:35

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/4399801/how-to-squeeze-in-additional-parameters-to-a-reaper-function-when-a-parent-is-si

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!