开发者

Problem: recvmsg(pfd[0], &message, MSG_WAITALL) always returns -1 instead of being blocked?

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 alrea开发者_如何学Pythondy 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.


recvmsg() does not work for pipes, rather for sockets only. When recvmsg() returns -1 you should check errno value, it is probably EBADF.

You can use unix sockets instead of pipe to pass file descriptors between processes.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜