fork() - 关闭服务器
我编写了一个服务器程序,可以使用 fork() 处理多个客户端。我有一个信号处理程序,在需要时进行错误检查,并且一切正常。我将其设置为如果客户端输入“退出”,服务器应停止接受连接,让打开的客户端完成通信,并在所有客户端关闭后关闭。为此,每当输入“退出”时,我都会将 int 标志设置为 0。由于每个子进程中的变量仅适用于该进程,并且不会影响其他子进程或父进程,因此我可以不跟踪标志何时设置为 0。在我的信号处理程序中,我检查
if( count == 0 && flag == 0)
//close server and exit program
计数是打开的客户端数量,在它们全部关闭后,显然将为零(这已经过错误检查以确保它是正确的) 。有谁知道我可以创建可以在一个客户端内设置的标志,并且对每个客户端都有相同的值的方法吗?如果这是有道理的..顺便说一句,我正在用 C 编码。
I have wrote a server program that can handle multiple client by using fork(). I have a signal handler, error checking where needed, and everything works correctly. I have it set up where if a client enter 'quit', the server should stop accepting connections, let the opened clients finish their communication, and close once all clients are closed. To do this, whenever 'quit' is entered, I have an int flag that is set to 0. Since the variables in each child process are only for that process, and do not affect the other child processes or the parent process, I can't keep track of when the flag is set to 0. In my signal handler I am checking
if( count == 0 && flag == 0)
//close server and exit program
count is the number of clients opened which after they are all closed will obviously be zero ( this has been error checked to make sure it is correct). Does anyone know of a way I can create flag that can be set inside one client, and have that same value for every client? If that makes sense.. I am coding in C by the way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要实现一个单一服务器供每个人通话,他将广播回所有客户端以更新它们的客户端计数,但本质上没有太多的复杂性来通话并跟踪所有其他客户端有点的挑战。
这也是在不同客户端之间路由和传输消息的地方。
希望这能带来一些启发。
You need to implement a single server for everyone to speak to, he will broadcast back to all the clients to update them with the client count, but in essence without a lot of complication to speak and keep track of all the other clients is a bit of a challenge.
This would also be a place to route and traffic messages between different clients.
Hopefully this shines some light.
接收
quit
命令的子进程需要发送一个信号(例如SIGUSR1
- 我不建议使用SIGTERM
来实现此目的,除非您将尽快关闭所有客户端)到其父级,并让父级在该信号处理程序的其内存空间中设置标志。 (父级只需将getpid()
的结果存储在某处即可让每个人都知道它的 pid。)The child process receiving the
quit
command needs to send a signal (such asSIGUSR1
-- I don't recommendSIGTERM
for this purpose unless you are going to be closing out all the clients as soon as possible) to its parent, and let the parent set the flag in its memory space in that signal handler. (The parent can let everyone know its pid by just storing the results ofgetpid()
somewhere.)有许多 IPC 机制可以做到这一点......特别是信号量。您可以创建一组两个信号量,并使用一个用于您的子计数,一个用于您的退出标志。当您启动程序时,客户端计数将初始化为零,退出标志将初始化为一。当每个子进程启动时,它应该为客户端计数加一……并在退出时减一。当客户端收到退出命令时,它将退出标志清零。当两个信号量都达到零时,主程序就会知道一切都完成了。
或者(如果可能的话)您可以启动线程而不是分叉进程,并使用两个全局 int 来表示 count 和 flag 以及全局互斥体保护他们。对于不需要进行大量线程间通信的简单事情,pthreads API 相当容易使用,并且创建线程比
fork
+exec
更快。There are many IPC mechanisms that can do this... in particular semaphores come to mind. You could create a set of two semaphores, and use one for your child count and one for your quit flag. When you start your program, the client count would be initialized to zero, and the quit flag would be initialized to one. When each child is started, it should add one to the client count... and subtract one when it exits. When a client receives the quit command, it would zero the quit flag. The main program will know when everything is done when both semaphores reach zero.
Or (if possible) you could just start threads instead of forking processes, and use two global
int
s forcount
andflag
and a global mutex to protect them. The pthreads API is fairly easy to use for simple things where not a lot of inter-thread communication needs to occur, and creating a thread is faster thanfork
+exec
.