【问题标题】:select and poll not work correctly选择和轮询无法正常工作
【发布时间】:2013-08-13 03:40:06
【问题描述】:

我在 java 中编写了一个自动流多路复用器,它必须读取大量的 FileDescriptor。要读取文件描述符(或具有 FileDescriptor 的套接字等),我需要一个精确的灵魂,如果我有一些描述符并且没有事件,请不要全部迭代。这是 linux 中的 som 方法:select,poll。我尝试使用这种方法但没有成功。本机代码是:

JNIEXPORT jint JNICALL Java_hu_ddsi_java_Native_JUnix_select(JNIEnv * env,jobject obj,jobjectArray fds,long timeout_s,long timeout_us)
{

    int len = env->GetArrayLength(fds);
    jclass cls = env->FindClass("java/io/FileDescriptor");
    jfieldID fid = env->GetFieldID(cls, "fd", "I");
    fd_set watch;
    int buf = 0;
    int max = 0;

    for(int i=0;i <  len;i++)
    {
        FD_SET( buf=(int) env->GetIntField(env->GetObjectArrayElement(fds, i),fid),&watch);
        if(buf>max)
            max = buf;
    }

    struct timeval *timeout = (timeval*) malloc(sizeof(struct timeval));
    timeout->tv_sec = timeout_s;
    timeout->tv_usec = timeout_us;

    return select(max+1, &watch, NULL,NULL,timeout);
}

JNIEXPORT jint JNICALL Java_hu_ddsi_java_Native_JUnix_poll(JNIEnv * env,jobject obj,jlongArray pollfds,int timeout_ms)
{
    unsigned int nfds = env->GetArrayLength(pollfds);

    void* pointerfor = malloc(nfds*(sizeof(void*)));

    for(int i=0;i <  nfds;i++)
        env->GetLongArrayRegion(pollfds,i,1,(jlong*) pointerfor+(sizeof(void*)*i));

    return poll( ((struct pollfd*)pointerfor), nfds,timeout_ms);
}

JNIEXPORT jlong JNICALL Java_hu_ddsi_java_Native_JUnix_pollfd(JNIEnv * env,jobject obj,jobject fd,jshort events,jshort revents)
{
    jclass cls = env->FindClass("java/io/FileDescriptor");
    jfieldID fid = env->GetFieldID(cls, "fd", "I");

    struct pollfd *pfd = (pollfd*) malloc(sizeof( pollfd));
        pfd->fd = env->GetIntField(fd,fid);
        pfd->events = events;
        pfd->revents = 0x0;

    return (jlong) pfd; 
}

JNIEXPORT jint JNICALL Java_hu_ddsi_java_Native_JUnix_pollfdfd(JNIEnv * env,jobject obj,jlong pfd_struct)
{
    return ((struct pollfd*)pfd_struct)->fd;
}

JNIEXPORT jshort JNICALL Java_hu_ddsi_java_Native_JUnix_pollfdevents(JNIEnv * env,jobject obj,jlong pfd_struct)
{
    return ((struct pollfd*)pfd_struct)->events;
}

JNIEXPORT jshort JNICALL Java_hu_ddsi_java_Native_JUnix_pollfdrevents(JNIEnv * env,jobject obj,jlong pfd_struct)
{
    return ((struct pollfd*)pfd_struct)->revents;
}

还有一个java中的测试代码:

    public static void main(String args[]) throws Throwable

        final FileInputStream is = new FileInputStream("/var/log/apache2/access.log");
        long st = JUnix.pollfd(is.getFD(),(short)( JUnix.POLLIN|JUnix.POLLPRI ));
        int len = 0;
        while(true)
        {

            System.out.println("ava:"+(len = is.available()));

            for(int i=0;i < len;i++)
                System.out.print((char) is.read());

//          JUnix.select(new FileDescriptor[]{is.getFD()},5,0);

            System.out.println("pollretval: "+JUnix.poll(new long[]{st},-1));
            System.out.println("revent: "+JUnix.pollfdrevents(st));
            System.out.println("pollfd: "+JUnix.pollfdfd(st));
        }
    }

当我浏览时,我应该在终端中看到新行,但它会永远被阻止...... 如果我修改超时,我会在终端中看到 now 行。

有时代码会变得疯狂并无限打印:

ava:0
pollretval: 1
revent: 0
pollfd: 10

这个结果很有趣,fileDescriptor 是相同的,原生 poll 返回了修改后的 fd 编号,但是在 pollfd stuct 中,revent 字段是......如果发生事件,它应该修改。 我测试过,指针的位置很好(如结果所示),并且一个简单的 C 代码执行相同的结果(我没有在 C 中找到 InputStream.available 类似 FD 的方法,所以我看不到流中有多少字节可用,但它永远等待)

我做错了什么?

【问题讨论】:

  • 如果我给出肯定的超时,它就会被释放,在等待的情况下无限地醒来。

标签: java c linux epoll


【解决方案1】:

Java_hu_ddsi_java_Native_JUnix_select你需要添加调用

FD_ZERO(&watch);

初始化您的fd_set

你还泄露了timeout。您需要在 select 返回之后 free 它,或者更好的是,在堆栈上声明它

struct timeval timeout;
timeout.tv_sec = timeout_s;
timeout.tv_usec = timeout_us;
return select(max+1, &watch, NULL,NULL,&timeout);

Java_hu_ddsi_java_Native_JUnix_poll 中有类似的pointerfor 泄漏。使用malloc 可能是合适的,因此您可能需要在返回之前free 指针。

【讨论】:

  • FD_ZERO() 没有解决问题,但感谢内存管理提示:)
  • linux 选择仅限于前 1024 个文件描述符。参见 FD_SETSIZE。如果你必须添加一个大于 1024 的描述符,事情就会出错(未定义的行为,经常崩溃)。更喜欢 poll()。
猜你喜欢
  • 1970-01-01
  • 2011-09-02
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2014-10-31
  • 2017-04-07
  • 2018-08-17
  • 2013-10-06
相关资源
最近更新 更多