[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] Different behavior of my code
Dear List,
I am currently taking my first steps in experimenting with libevent. To
do that, I have written the small example below that should copy a file.
I am currently trying to restrict myself to the core functions, and I
know that the buffer handling is too primitive.
I am trying the code on 3 different platforms:
a) On OpenBSD 4.9, the code seems to work and the file is copied. I am
not sure what version OpenBSD is using and what they changed.
b) On MacOS Lion, using libevent 2.0.10, the program copies the file,
but seems to never receive the 0 bytes EOF read, thus blocking after the
last write. Output ends with:
...
read callback
Read: 6, 2 - 81953
write callback
Written: 7, 4 - 81953 (81953) of 81953
Sub read.
c) On Linux (Ubuntu LTS), using libevent 1.4.13, the read operation is
never triggered and the main loop exits immediately. The output printed is:
Starting.
Opened read file handle 6.
Opened write file handle 7.
Exited with 1
With these 3 different behaviors, I am pretty sure that I have not
understood some fundamental detail of libevent. I do not think the
different versions are to blame, and it should be possible to create
some code that runs on all versions, as I am using very basic functions
that seem to be the same in all versions.
Thanks for your help,
Bernd
---> c_libevent_test.c <---
#include <event.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#define READ_FILE_NAME "in.dat"
#define WRITE_FILE_NAME "out.dat"
#define BUFFER_SIZE 100000
struct event fd_read;
struct event fd_write;
int buffer[BUFFER_SIZE];
int full;
int written;
void onRead(int fd, short mode, void* data)
{
printf("read callback\n");
full = read(fd, buffer, BUFFER_SIZE);
printf("Read: %d, %d - %d\n", fd, mode, full);
if (full > 0) {
written = 0;
event_add(&fd_write, NULL);
}
}
void onWrite(int fd, short mode, void *data)
{
printf("write callback\n");
int wr = write(fd, buffer+written, full-written);
written += wr;
printf("Written: %d, %d - %d (%d) of %d\n", fd, mode,
wr, written, full);
if (wr >= full) {
written = 0;
full = 0;
printf("Sub read.\n");
event_add(&fd_read, NULL);
} else {
event_add(&fd_write, NULL);
}
}
int main(int argc, char** argv)
{
full = 0;
event_init();
printf("Starting.\n");
int fd;
fd = open(READ_FILE_NAME, O_RDONLY);
printf("Opened read file handle %d.\n", fd);
event_set(&fd_read, fd, EV_READ, onRead, 0);
fd = open(WRITE_FILE_NAME, O_WRONLY | O_CREAT, 0600);
printf("Opened write file handle %d.\n", fd);
event_set(&fd_write, fd, EV_WRITE, onWrite, 0);
event_add(&fd_read, NULL);
printf("Exited with %d\n", event_dispatch());
return 0;
}
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.