选择和轮询无法正常工作

时间:2013-08-11 20:59:11

标签: java c linux epoll

我在java中编写了一个自动流多路复用器,必须读取大量的FileDescriptors。要读取文件描述符(或具有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));
        }
    }

当我浏览时,我应该在终端中看到新线路,但它将永远被阻挡...... 如果我修改了超时,请查看终端中现在的行。

有时候代码变得疯狂并且无法打印:

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

这个结果很有意思,fileDescriptor是相同的,使用修改过的fd号返回本机轮询,但是在pollfd stuct中,revent字段是......如果发生了事件,它应该被修改。  我测试过,指针位置很好(结果显示),一个简单的C代码做同样的结果(我没有找到InputStream.available,就像C中的方法一样FD,所以我看不到流中有多少字节可用,但它永远等待)

我错了什么?

1 个答案:

答案 0 :(得分:1)

Java_hu_ddsi_java_Native_JUnix_select中,您需要添加对

的调用
FD_ZERO(&watch);

初始化您的fd_set

你也泄漏了timeout。在free返回后你需要select,或者更好的是,只需在堆栈中声明它

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

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

相关问题