问题:recvmsg(pfd[0], &message, MSG_WAITALL) 总是返回 -1 而不是被阻止?
我正在制作一个服务器,它在连接时生成一个子进程(使用 fork),并在有另一个连接进入时使用管道向该子进程发送另一个套接字。这个想法是让子进程管理 2- 中的两个连接玩家网络游戏模式。
父级和子级之间的 IPC 管道变量是 pfd[2]。
基本上,在子进程中,我执行 recvmsg(pfd[0], &message, MSG_WAITALL)
来等待从父进程传递第二个套接字。
然而,recvmsg 永远不会被阻塞,并且总是返回 -1。
我已经将 pfd[0] 设置为 BLOCKING,如下所示:
// set to blocking pipe
int oldfl;
oldfl = fcntl(pfd[0], F_GETFL);
if (oldfl == -1) {
perror("fcntl F_GETFL");
exit(1);
}
fcntl(pfd[0], F_SETFL, oldfl & ~O_NONBLOCK);
How can I make the child to be Block at recvmsg?
感谢一百万的任何提示。
I'm making a server which spawn a child upon connection (using fork), and use pipe to send another socket to this child when there is another connection comming in. The idea is to let the child process manage two connections in a 2-player network game mode.
IPC pipe variable between parent and child is pfd[2].
Basically, in the child process, I do recvmsg(pfd[0], &message, MSG_WAITALL)
to wait for the 2nd socket to be passed from the parent.
However, recvmsg is never blocked, and always gets returned -1.
I've already set pfd[0] to BLOCKINg as follows:
// set to blocking pipe
int oldfl;
oldfl = fcntl(pfd[0], F_GETFL);
if (oldfl == -1) {
perror("fcntl F_GETFL");
exit(1);
}
fcntl(pfd[0], F_SETFL, oldfl & ~O_NONBLOCK);
How can I make the child to be blocked at recvmsg?
Thanks a million for any hint.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
recvmsg()
不适用于管道,而仅适用于套接字。当recvmsg()返回-1时,您应该检查errno值,它可能是EBADF。您可以使用 unix 套接字 代替管道在进程之间传递文件描述符。
recvmsg()
does not work for pipes, rather for sockets only. Whenrecvmsg()
returns -1 you should checkerrno
value, it is probablyEBADF
.You can use unix sockets instead of pipe to pass file descriptors between processes.