Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem handling file I/O with libevent2

I worked with libevent2 for some time, but usually I used it to handle network I/O (using sockets). Now I need to read many different files so I also wanted to use it. I created this code:

int file = open(filename, O_RDONLY);
struct event *ev_file_read = event_new(ev_base, file, EV_READ | EV_PERSIST, read_file, NULL);

if(event_add(ev_file_read, NULL))
        error("adding file event");

Unfortunately it doesn't work. I get this message when trying to add event:

[warn] Epoll ADD(1) on fd 7 failed. Old events were 0; read change was 1 (add); write change was 0 (none): Operation not permitted adding file event: Operation not permitted

The file exists and has rights to read/write.

Anyone has any idea how to handle file IO using libevent? I thought also about bufferred events, but in API there's only function bufferevent_socket_new() which doesn't apply here.

Thanks in advance.

like image 417
harnen Avatar asked Mar 28 '11 09:03

harnen


People also ask

What are the I/O handling operations of a file?

C# File I/O Handling Operations [Examples] 1 File.Exists 2 File.ReadAlllines 3 File.ReadAllText 4 File.Copy 5 File.Delete

Can errors be created or appended to files opened for reading?

In particular, we have assumed that the error handling in file we opened for reading already existed, and that those opened for writing could be created or appended to. We’ve also assumed that there were no failures during reading or writing.

Can an I/O method throw a filenotfoundexception?

However, it may also throw a FileNotFoundException. Because of this reliance on the operating system, identical exception conditions (such as the directory not found error in our example) can result in an I/O method throwing any one of the entire class of I/O exceptions.

What are the error handling functions during file operations in C/C++?

Below are some Error handling functions during file operations in C/C++: In C/C++, the library function ferror () is used to check for the error in the stream.


2 Answers

I needed libevent to read many files regarding priorities. The problem was in epoll not in libevent. Epoll doesn't support regular Unix files.

To solve it I forced libevent not to use epoll:

    struct event_config *cfg = event_config_new();

event_config_avoid_method(cfg, "epoll");

ev_base = event_base_new_with_config(cfg);  
    event_config_free(cfg);

Next method on the preference list was poll, which fully support files just as I wanted to.

Thank you all for answers.

like image 190
harnen Avatar answered Oct 05 '22 03:10

harnen


Makes no sense to register regular file descriptors with libevent. File descriptors associated with regular files shall always select true for ready to read, ready to write, and error conditions.

like image 23
Maxim Egorushkin Avatar answered Oct 05 '22 01:10

Maxim Egorushkin