我目前正在尝试运行一台服务器,以使用命名管道作为IPC同时处理多个"客户端"(本地进程)请求。客户端可以在上面进行写入,但服务器上的select()函数似乎无法正常工作,它一直返回0。
这是服务器的主要代码:
int main (int argc, char const *argv[]){
fd_set set; //fds to monitor
Request * r;
struct timeval tv; //timeout
Connection *c; //where the fd will be saved
fd_set foo; //debugging purpose
//opens the NamedPipe and saves de ReadOnly fd on c
if( (c = openConnection()) == NULL) {
return ERROR_OPEN_REQUEST_QUEUE;
}
//sets up the fds to monitor FD_ZERO and FD_SET
setConnection(c, &set);
FD_ZERO(&foo);
tv.tv_sec = 2;
tv.tv_usec = 0;
while(1){
int fdCount = select(2, &set, &foo, &foo, &tv);
//it seems select directly modifies this value
tv.tv_sec = 2;
//saw it on another post
setConnection(c, &set);
if( fdCount > 0){
r = getRequest(c);
if( r != NULL ){
TODO processRequest(r);
}
} else {
printf("No requests to processn");
}
}
return 0;
}
服务器和客户端都使用openConnection从NamedPipe获取fd。openConnections调用此函数并创建一个连接对象:
int * openNamedPipe(char * name) {
char origin[] = "/tmp/";
char myfifo[80];
int * fd;
fd = malloc(sizeof(int)*2);
strcpy(myfifo,origin);
strcat(myfifo,name);
mkfifo(myfifo, 0777);
fd[0] = open(myfifo, O_RDONLY|O_NONBLOCK);
fcntl(fd[0], F_SETFL, fcntl(fd[0], F_GETFL) &~O_NONBLOCK);
fd[1] = open(myfifo, O_WRONLY);
return fd;
}
我的问题如下:
- 对于每个客户端,在同一管道上多次调用mkfifo()是否有问题
- 与打开/关闭管道相同
我正在用猫手动检查fifo,我可以从外壳中读取内容。因此,如果客户端能够写入,那么服务器应该能够使用ReadOnly fd进行读取。
添加setConnection功能以防万一:
void setConnection(Connection * connection, fd_set* set){
FD_ZERO(set);
FD_SET(connection -> np -> fd, set);
return;
}
select
的第一个参数应该是编号最高的文件描述符+1。您正在传递值2,因此您的选择只关心fds 0和1(如果它们是在传递的集合中设置的)。你的烟斗真的用那些吗?如果没有,您需要在代码中测试最高的代码,然后通过该代码进行选择。
另一件事是,如果你不想看那些事件,你应该只传递NULL而不是foo
。