- 论坛徽章:
- 0
|
本帖最后由 rick_cheung 于 2011-01-14 14:36 编辑
我想使用Linux异步io 做一个写文件的测试,没有成功。请教各位原因何在。谢谢!
程序如下:
#include <stdio.h>
#include <aio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
struct aiocb my_aiocb;
void aio_completion_handler( sigval_t sigval);
void setup_io(int fd, char *buf)
{
//struct aiocb my_aiocb;
int ret;
bzero( (char *)&my_aiocb, sizeof(struct aiocb) );
my_aiocb.aio_fildes = fd;
my_aiocb.aio_buf = buf;
my_aiocb.aio_nbytes = 4;
my_aiocb.aio_sigevent.sigev_notify = SIGEV_THREAD;
my_aiocb.aio_sigevent.sigev_notify_function = aio_completion_handler;
my_aiocb.aio_sigevent.sigev_notify_attributes = NULL;
my_aiocb.aio_sigevent.sigev_value.sival_ptr = &my_aiocb;
ret = aio_write( &my_aiocb );
printf("send the aio request\n");
}
void aio_completion_handler( sigval_t sigval)
{
struct aiocb *req;
int ret;
req = (struct aiocb *)sigval.sival_ptr;
if( aio_error(req) == 0)
{
ret = aio_return(req);
}
printf("io done\n");
return;
}
int main()
{
int fd;
char *buf;
buf = "123";
fd = open("/home/heihei.txt", O_CREAT | O_RDWR | O_APPEND , 77777);
printf("fd = %d\n", fd);
setup_io(fd, buf);
}
输出如下:
fd = 3
send the aio request
并且heihei.txt 已经生成。但是文件里面没有任何内容,说明没有写成功,原因何在呢? |
|