简体   繁体   English

select()/ FD _ *()导致文件描述符错误

[英]select()/FD_*() cause Bad file descriptor

I just like to test a bug about select listen more than 1024 files descriptor on Linux(Ubuntu 13.04/Debian 6). 我只想测试一个有关在Linux(Ubuntu 13.04 / Debian 6)上选择监听超过1024个文件描述符的错误。 And I override FD_SETSIZE and __FD_SETSIZE macros. 而且我覆盖了FD_SETSIZE和__FD_SETSIZE宏。

And then , perror() report have some errors while closing files descriptor at the end of program. 然后,perror()报告在程序结束时关闭文件描述符时出现一些错误。 On my PC like the following :(the quantity of errors depends on the quantity of listens). 在我的PC上,如下所示:(错误的数量取决于侦听的数量)。

closing file at 0
close: Bad file descriptor
closing file at 1
close: Bad file descriptor
closing file at 2
close: Bad file descriptor
closing file at 3
close: Bad file descriptor
closing file at 4
close: Bad file descriptor
closing file at 5
close: Bad file descriptor
closing file at 6
close: Bad file descriptor
closing file at 7
close: Bad file descriptor

the Code is following : 代码如下:

#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/time.h>
#include <sys/resource.h>

#define __FD_SETSIZE 8192
#define FD_SETSIZE 8192

#define NR_SELECT 2048 

char filename[10];

void init()
{
    struct rlimit* rlim;
    int n;
    rlim = (struct rlimit*)malloc(sizeof(struct rlimit));
    n=getrlimit(RLIMIT_NOFILE,rlim);
    if (n == -1) {
        perror("getrlimit");
        exit(1);
    }
    rlim->rlim_max=8192;
    rlim->rlim_cur=8192;

    n=setrlimit(RLIMIT_NOFILE,rlim);
    if (n == -1) {
        perror("setrlimit");
        exit(1);
    }

}

int main(void)
{
    int fd[4096];
    int i;
    fd_set fdset;
    struct timeval tv;
    int retval;

    init();

    /*clear and init a fd set*/
    FD_ZERO(&fdset);

    for (i=0;i<NR_SELECT;i++) {
        sprintf(filename,"./tst%d",i);
        fd[i]=open(filename,O_CREAT|O_RDWR,0666);
        if (fd[i] == -1) {
            fprintf(stderr,"opening at %dfile \n",i);
            perror("open while opening at \n");
            exit(1);
        }
    }
    for (i=0;i<NR_SELECT;i++) {
        FD_SET(fd[i],&fdset);
        if (!FD_ISSET(fd[i],&fdset)) {
            fprintf(stderr,"checking fd[%d] in fdset",i);
            perror("FD_ISSET");
            exit(1);
        }
    }

    tv.tv_sec=5;
    tv.tv_usec=0;

    retval = select(4096,&fdset,NULL,NULL,&tv);
    if (retval == -1)
        perror("select()");
    else if (retval) {
        printf("data available\n");
    }

    for (i=0;i<NR_SELECT;i++) {
        int n;
        n = close(fd[i]);
        if (n== -1) {
            fprintf(stderr,"closing file at %d\n",i);
            perror("close");
        }
    }
    return 0;
}

While compiling will some warning about redefine Marcos. 在编译时会出现一些有关重新定义Marcos的警告。

You need to define FD_SETSIZE before including whatever header that on your system happens to pull in sys/select.h . 您需要先定义FD_SETSIZE然后再包含系统上碰巧引入sys/select.h任何标头。 Otherwise that header will define it and your redefinition will either do nothing or break the FD_* macros. 否则,该标头将对其进行定义,并且您的重定义将不执行任何操作或破坏FD_ *宏。

Also. 也。 Don't use select . 不要使用select Use poll , epoll , kqueue or any other modern interface that's designed to better deal with more than 20 file descriptors. 使用pollepollkqueue或任何其他旨在更好地处理20多个文件描述符的现代接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM