1,324
社区成员




#include <sys/types.h>
#include <aio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>
#define SIZE_TO_READ 100
int main()
{
char buffer[SIZE_TO_READ] = {0};;
struct aiocb cb;
int file = 0;
int numBytes = 0;
file = open("/dev/scullp", O_RDONLY, 0);
if (file == -1)
{
perror("Unable to open file!");
return 1;
}
memset(&cb, 0, sizeof(struct aiocb));
cb.aio_nbytes = SIZE_TO_READ;
cb.aio_fildes = file;
cb.aio_offset = 0;
cb.aio_buf = buffer;
if (aio_read(&cb) == -1)
{
perror("Unable to create request!" );
close(file);
}
printf("Request enqueued!\n");
while(aio_error(&cb) == EINPROGRESS)
{
printf("working...\n");
}
numBytes = aio_return(&cb);
if (numBytes != -1)
{
printf("success!read:%s\n",buffer);
}
else
{
perror("aio_return error\n");
}
close(file);
return 0;
}
static ssize_t scullp_aio_read(struct kiocb *iocb, char __user *buf, size_t count,
loff_t pos)
{
printk(KERN_INFO "scullp_aio_read\n");
return scullp_defer_op(0, iocb, buf, count, pos);
}
ssize_t scullp_read (struct file *filp, char __user *buf, size_t count,
loff_t *f_pos)
{
....
printk(KERN_INFO "scullp_read\n");
.....
}
#gcc aio_test.c -lrt
#./a.out
Request enqueued!
working...
working...
working...
working...
success!read:
#dmesg | tail
.....
[180293.021298] scullp_read