select() C lib函数总是returns0
select () C lib function always returns 0
我目前正在尝试 运行 服务器使用命名管道作为 IPC 同时处理多个 "clients'"(本地进程)请求。客户端可以在上面写,但是服务器上的 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 process\n");
}
}
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() 是否有问题?
- 同样适用于 opening/closing 管道
我正在用 cat 手动检查 fifo,我能够从 shell 中读取内容。因此,如果客户端能够写入,服务器应该能够使用 ReadOnly fd 进行读取。
添加 setConnection 函数以防万一:
void setConnection(Connection * connection, fd_set* set){
FD_ZERO(set);
FD_SET(connection -> np -> fd, set);
return;
}
select
的第一个参数应该是编号最高的文件描述符 +1。您正在传递值 2,因此您的 select 只关心 fds 0 和 1(如果它们在传递的集合中设置)。你的管道真的使用那些吗?如果不是,则需要测试最高的代码并将其传递给 select.
另一件事,如果您不想观看这些事件,似乎您应该只传递 NULL 而不是 foo
。
我目前正在尝试 运行 服务器使用命名管道作为 IPC 同时处理多个 "clients'"(本地进程)请求。客户端可以在上面写,但是服务器上的 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 process\n");
}
}
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() 是否有问题?
- 同样适用于 opening/closing 管道
我正在用 cat 手动检查 fifo,我能够从 shell 中读取内容。因此,如果客户端能够写入,服务器应该能够使用 ReadOnly fd 进行读取。
添加 setConnection 函数以防万一:
void setConnection(Connection * connection, fd_set* set){
FD_ZERO(set);
FD_SET(connection -> np -> fd, set);
return;
}
select
的第一个参数应该是编号最高的文件描述符 +1。您正在传递值 2,因此您的 select 只关心 fds 0 和 1(如果它们在传递的集合中设置)。你的管道真的使用那些吗?如果不是,则需要测试最高的代码并将其传递给 select.
另一件事,如果您不想观看这些事件,似乎您应该只传递 NULL 而不是 foo
。