问题描述
我目前有一个类似于此伪代码的程序。目的是通过为每个新连接创建一个子代来处理用户输入(简单的rpc程序),从而能够处理多个客户端,从而能够连接到服务器:
int main() {
char *cmd = " ";
while(status="running") {
accept_connection(); // this will block until a new connection is made
int rval;
int pid = fork(); // when a new connection is made,make a child process to handle user commands
if (pid == 0) { // child process
while (strcmp(command_received,"shutdown")) {
receive_message(msg,buffer);
/* processing the command here */
send_message(server_response);
}
exit(0);
}
int res = waitpid(pid,&rval,WNOHANG); // parent waits (non-blocking) for child process to end
if (WEXITSTATUS(rval == 0) {
status="closing";
}
}
printf("server shutting down...");
return 0;
}
我当前遇到的问题是,即使其中一个客户端输入了“ shutdown”命令,父进程也不会检测到它,并且永远不会进入该循环:
if (WEXITSTATUS(rval == 0) {
status="closing";
}
我面临的问题是父级无法立即检测到退出状态,因为它返回到循环的顶部并在accept_connection()处阻塞。
我可以使该设置正常工作吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)