“错误文件描述符”多次使用 socketpair 函数的文件描述符时出错
以下代码使用套接字对将消息从子进程发送到其父进程。
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MESSAGE "Hello"
int main()
{
char message[100];
int i, pidFork, sockets[2];
/*
*Trying to use a single socketpair function call
*
*if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
* perror("Creating socketpair");
* exit(-1);
*}
*/
for(i=0; i<2; i++) {
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
perror("Creating socketpair");
exit(-1);
}
printf("i: %d\n", i);
pidFork = fork();
if (pidFork == -1) {
perror("Creating child process");
exit(-1);
}
else if (pidFork != 0) { //Parent
close(sockets[0]);
if(read(sockets[1], message, strlen(MESSAGE)) == -1) {
perror("Reading data");
exit(-1);
}
printf("Data: %s\n\n", message);
close(sockets[1]);
}
else { //Child
close(sockets[1]);
if(write(sockets[0], MESSAGE, strlen(MESSAGE)) == -1) {
perror("Writing data");
exit(-1);
}
close(sockets[0]);
exit(0);
}
}
return 0;
}
首先,我尝试在进入 for 循环之前通过对 socketpair 函数的一次调用来获取套接字对的文件描述符,如注释行所示,但这仅适用于第一次迭代,从第二次迭代开始我得到一个“错误的文件描述符错误”,我使其工作的方式是将套接字对函数调用移动到 for 循环内,但是在使用管道时,第一种方法对我有用。
所以我想知道为什么会发生这种情况,我犯了错误吗? 或者这是两种IPC机制之间的区别吗?
谢谢
更新:管道和socketpair的文件描述符之间没有区别。我认为它正在使用管道,因为我的完整程序的另一部分出现错误。 接受的答案解决了我的问题。
The following code sends messages from child processes to their parents using socket pairs.
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MESSAGE "Hello"
int main()
{
char message[100];
int i, pidFork, sockets[2];
/*
*Trying to use a single socketpair function call
*
*if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
* perror("Creating socketpair");
* exit(-1);
*}
*/
for(i=0; i<2; i++) {
if(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == -1) {
perror("Creating socketpair");
exit(-1);
}
printf("i: %d\n", i);
pidFork = fork();
if (pidFork == -1) {
perror("Creating child process");
exit(-1);
}
else if (pidFork != 0) { //Parent
close(sockets[0]);
if(read(sockets[1], message, strlen(MESSAGE)) == -1) {
perror("Reading data");
exit(-1);
}
printf("Data: %s\n\n", message);
close(sockets[1]);
}
else { //Child
close(sockets[1]);
if(write(sockets[0], MESSAGE, strlen(MESSAGE)) == -1) {
perror("Writing data");
exit(-1);
}
close(sockets[0]);
exit(0);
}
}
return 0;
}
First I tried to get the file descriptors for a socket pair with a single call to the socketpair function before entering the for loop, such as the commented lines shows, but this only works for the first iteration, from the second on iteration I get a "Bad file descriptor error", the way I made it work was moving the socketpair function call inside the for loop, however the first approach worked for me when using pipes.
So I would like to know why this happens, Am I making a mistake?
Or Is this a difference between both IPC mechanisms?
Thanks
Update: There's no difference between file descriptors of pipe and socketpair. I thought that it was working with pipes because an error in another part of my full program.
The accepted answer solved my issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将关闭父级和子级中
socketpair
的两端。一般来说,每个子进程使用一对套接字或管道。
You're closing both ends of the
socketpair
in both parent and child.In general, use one socket pair or pipe per child process.