当父母收到杀死孩子的信号时,如何向收割者函数添加额外的参数(c)?

发布于 2024-10-07 04:22:35 字数 822 浏览 1 评论 0原文

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

当用户连接时,将创建一个子进程来为用户服务。
当用户登录时,我将他的用户名存储到一个文本文件中,online.txt
但是当用户注销时,我需要从 online.txt 中删除用户(问题),然后父级信号一个reaper()并杀死了孩子。

我的问题是:

Q1:如何向 reaper 挤入额外信息(例如用户用于登录的用户名),以便它也可以从 online.txt 中删除用户?或者还有其他更好的方法吗?

Q2:reaper()中的sig从哪里获取值?我可以为收割机添加额外的参数吗?

Q3:我可以使用孩子的 pid 作为 login.txt 的某种主键吗?如果是这样,我如何在父进程调用的 reaper() 期间检索子进程的 pid?

收割者看起来像这样:

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

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

家长使用的信号看起来像这样:

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

先谢谢你了,我希望一次问 3 个问题不会太贪心。
对任何问题的任何见解将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-10-14 04:22:35

据我从你的问题中了解到,父进程将 reaper() 注册为 SIGCHLD 的处理程序。当它检测到登录时,它将用户名写入文件,并生成一个子进程。

注销时,会调用 reaper() 函数,因为子进程已检测到注销并退出,对吗?

如果是这样,为什么不让服务器维护一个将 PID 映射到用户名的数据结构。然后获取 waitpid 的返回值并确定需要从文件中删除哪个用户名。

所以,总结一下:

1)不。是的。

2)来自处理程序接收到的信号。不

。3)是的。来自waitpid()的返回值。

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

弥繁 2024-10-14 04:22:35

问题 1:为您的子进程配置信号处理程序来执行某些特定操作是否合适?然而,也许更好的解决方案是不使用文件,而是使用内存结构来存储用户登录的内容。这样,收割者就可以删除内存中的条目,甚至删除建议的信号处理程序。

问题 2:我不熟悉您的操作系统或体系结构,但我猜测 SIGCHLD 将被传递到 reaper( int sig ) 作为参数值。

问题 3:获取 pid 是特定于操作系统的。对于 POSIX 类型,它通常是来自 unistd 的 getpid()。但是我会质疑您是否真的想对文件执行此操作。

当您开始到处发出信号时,您的解决方案可能会变得容易受到竞争条件的影响……这会带来安全风险。

请各位用户不吝指正。在寻求智慧的过程中,一个人必须接受指导。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文