This is the mail archive of the
libc-help@sourceware.org
mailing list for the glibc project.
understanding unexpected aio_write() behaviour
- From: Leon Woestenberg <leon dot woestenberg at gmail dot com>
- To: libc-help at sourceware dot org
- Date: Thu, 21 May 2009 00:37:53 +0200
- Subject: understanding unexpected aio_write() behaviour
Hello,
I have got a problem to understand aio_write() behaviour of a small
test program, source below.
I am performing two concurrent async writes:
- first aio_write() is to bytes 0 to N-1.
- second aio_write() is to bytes N to 2*N-1.
I expect the file to be 2*N bytes of size after this program has run.
However I get variable sized output files, mostly N, sometimes 2*N,
sometimes <N, on subsequent test runs:
leon@witty:~/sandbox/asyncio$ rm -f test.bin && make && ./test && ls
-ald test.bin
cc -std=c99 -o test test.c -lrt
Initiating aio_write()
-rw-r--r-- 1 leon leon 2000000 2009-05-21 00:30 test.bin
leon@witty:~/sandbox/asyncio$ rm -f test.bin && make && ./test && ls
-ald test.bin
make: Nothing to be done for `all'.
Initiating aio_write()
-rw-r--r-- 1 leon leon 1000000 2009-05-21 00:30 test.bin
This is on Ubuntu 9.04 x86 (32-bit).
What am I overlooking here?
Regards,
Leon.
/* gcc -std=c99 -o main main.c -lrt */
#define _POSIX_C_SOURCE 200112L
#include <aio.h>
#include <assert.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define N 100000
int main(int argc, char *argv[])
{
struct aiocb aio_cb;
struct aiocb aio_cb2;
char *buffer = malloc(N);
assert(buffer);
int fd = open("test.bin", O_WRONLY | O_CREAT, 0666);
assert(fd >= 0);
/* fill in async request control block */
memset((void *)&aio_cb, 0, sizeof(struct aiocb));
aio_cb.aio_fildes = fd;
aio_cb.aio_offset = 0;
aio_cb.aio_buf = buffer;
/* length N */
aio_cb.aio_nbytes = N;
/* copy control block */
aio_cb2 = aio_cb;
/* second write is to offset N */
aio_cb2.aio_offset = N;
printf("Initiating aio_write()\n");
int rc;
rc = aio_write(&aio_cb);
assert(rc == 0);
rc = aio_write(&aio_cb2);
assert(rc == 0);
rc = aio_fsync(O_SYNC, &aio_cb);
assert(rc == 0);
rc = aio_fsync(O_SYNC, &aio_cb2);
assert(rc == 0);
rc = aio_return(&aio_cb);
assert(rc == 0);
rc = aio_return(&aio_cb2);
assert(rc == 0);
sleep(2);
close(fd);
free(buffer);
}
--
Leon