如何在其他参数收割机功能挤当父发出信号杀死一个孩子(三)? [英] How to squeeze in additional parameters to a reaper function when a parent is signalled to kill a child (c)?

查看:155
本文介绍了如何在其他参数收割机功能挤当父发出信号杀死一个孩子(三)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个TCP服务器,其功能非常像一个聊天室并在这个问题就来了。

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

当用户连接,一个子进程中创建服务于用户。结果
当用户登录时,我自己的用户名存储到一个文本文件, online.txt 结果
但是,当用户注销时,我需要删除从用户online.txt (的问题),父则信号收割机()并杀死了孩子。

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.

我的问题是:

Q1:如何我可以在其他信息挤收割者(如用户名所使用的用户登录),因此它也可以删除 online.txt ?或者有另一种更好的方法,这样做?

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: SIG 其中确实在收割机()从获取它的价值呢?我可以到收割者添加额外的参数呢?

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

Q3:我可以用孩子的PID为某种针对的login.txt主键的?如果是这样,我怎么能在找回孩子的PID 收割机(),这是由父母叫什么名字?

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?

感谢你在前进,我希望问一次3个问题是不是太贪婪。结果
对任何问题的任何有识之士将大大AP preciated。

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.

推荐答案

据我可以从你的问题收集,父进程注册收割机()为SIGCHLD的处理程序。当它检测到一个登录,它会将用户名文件,并生成一个孩子。

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?

如果是这样,为什么不直接在服务器维护一个数据结构映射PID号的用户名。然后采取从waitpid函数的返回值,并确定该用户名需要从文件中删除。

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.

因此​​,要总结:

1)号是。

2)从由处理程序接收的信号。没有。

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

3)是。从waitpid函数的返回值()。

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

这篇关于如何在其他参数收割机功能挤当父发出信号杀死一个孩子(三)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆