Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to deal with this case: andorid epoll_wait return -1 and errno=4 used ndk

I am writing network communication program with Android ndk, using epoll. I found the method ‘epoll_wait’ woken not very accurate

while(1){
    struct epoll_event events[3];
    log_string("epoll_wait start");//here will print start time
    events_len = epoll_wait(_epoll_fd, events, 3, 20 * 1000);// wait 20 second,for test,I use pipe instead of socket,monitor a pipe EPOLLIN event
    if (events_len <= 0) {
        log_string("epoll_wait end events_len=%d,errno=%d", events_len, errno);//Normally,the events_len always is 0,and errno is 0
    }
}

The above code runs on the PC(like Ubuntun PC) is very normal,as expected.

If it runs on Android Phone(use Android Service , separate thread to run) is as expected at first.

After some time,epoll_wait becomes not very accurate,sometimes got -1 and errno=4,sometimes waited very long.

So I only know that phenomenon, but do not know why.

Can you tell why and tell me the best practices for use android epoll? thx

like image 261
gezhonglunta Avatar asked Nov 22 '25 17:11

gezhonglunta


1 Answers

4 is EINTR, which means your app got a signal. This isn't really an error, just restart epoll.

Regarding "waited very long", does your app hold at least a partial wakelock?

like image 111
George Y. Avatar answered Nov 25 '25 07:11

George Y.