当前位置: 技术问答>linux和unix
aio实现问题
来源: 互联网 发布时间:2017-05-19
本文导语: #include #include #include #include #include #include #include #include #include #include #include #include #include #define TEST_FILE "/home/sq/aio_test_file" #define TEST_FILE_SIZE (127 * 1024) #define NUM_EVENTS 64 #define ALIGN_SIZE 512 #defi...
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define TEST_FILE "/home/sq/aio_test_file"
#define TEST_FILE_SIZE (127 * 1024)
#define NUM_EVENTS 64
#define ALIGN_SIZE 512
#define RD_WR_SIZE 1024
struct custom_iocb
{
struct iocb iocb;
int nth_request;
};
void aio_callback(io_context_t ctx, struct iocb *iocb, long res, long res2)
{
struct custom_iocb *iocbp = (struct custom_iocb *)iocb;
printf("nth_request: %d, request_type: %s, offset: %lld, length: %lu, res: %ld, res2: %ldn",
iocbp->nth_request, (iocb->aio_lio_opcode == IO_CMD_PREAD) ? "READ" : "WRITE",
iocb->u.c.offset, iocb->u.c.nbytes, res, res2);
printf("%sn", (char*)iocb->u.c.buf);
}
int main(int argc, char *argv[])
{
int fd_EventObj;
int fd_File;
io_context_t pContext;
struct timespec tTimeSpec;
struct io_event atIOEvent[NUM_EVENTS];
struct custom_iocb atCustomIOCB[NUM_EVENTS];
struct iocb *apIOCB[NUM_EVENTS];
struct custom_iocb *apCustomIOCB;
int i, j, r;
void *buf;
fd_EventObj = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (fd_EventObj == -1)
{
perror("eventfd");
return 2;
}
//O_DIRECT这个很重要
fd_File = open(TEST_FILE, O_RDWR | O_CREAT | O_DIRECT, 0644);
if (fd_File == -1)
{
perror("open");
return 3;
}
pContext = 0;
if (io_setup(NUM_EVENTS, &pContext))
{
perror("io_setup");
return 4;
}
for (i = 0, apCustomIOCB = atCustomIOCB; i iocb;
io_prep_pread(&apCustomIOCB->iocb, fd_File, buf, RD_WR_SIZE, i * RD_WR_SIZE);
io_set_eventfd(&apCustomIOCB->iocb, fd_EventObj);
io_set_callback(&apCustomIOCB->iocb, aio_callback);
apCustomIOCB->nth_request = i + 1;
}
if (io_submit(pContext, NUM_EVENTS, apIOCB) != NUM_EVENTS)
{
perror("io_submit");
return 6;
}
tTimeSpec.tv_sec = 0;
tTimeSpec.tv_nsec = 0;
r = io_getevents(pContext, 1, NUM_EVENTS, atIOEvent, &tTimeSpec);
if (r > 0)
{
for (j = 0; j nth_request, (iocb->aio_lio_opcode == IO_CMD_PREAD) ? "READ" : "WRITE",
iocb->u.c.offset, iocb->u.c.nbytes, res, res2);
printf("%sn", (char*)iocb->u.c.buf);
}
int main(int argc, char *argv[])
{
int fd_EventObj;
int fd_File;
io_context_t pContext;
struct timespec tTimeSpec;
struct io_event atIOEvent[NUM_EVENTS];
struct custom_iocb atCustomIOCB[NUM_EVENTS];
struct iocb *apIOCB[NUM_EVENTS];
struct custom_iocb *apCustomIOCB;
int i, j, r;
void *buf;
fd_EventObj = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
if (fd_EventObj == -1)
{
perror("eventfd");
return 2;
}
//O_DIRECT这个很重要
fd_File = open(TEST_FILE, O_RDWR | O_CREAT | O_DIRECT, 0644);
if (fd_File == -1)
{
perror("open");
return 3;
}
pContext = 0;
if (io_setup(NUM_EVENTS, &pContext))
{
perror("io_setup");
return 4;
}
for (i = 0, apCustomIOCB = atCustomIOCB; i iocb;
io_prep_pread(&apCustomIOCB->iocb, fd_File, buf, RD_WR_SIZE, i * RD_WR_SIZE);
io_set_eventfd(&apCustomIOCB->iocb, fd_EventObj);
io_set_callback(&apCustomIOCB->iocb, aio_callback);
apCustomIOCB->nth_request = i + 1;
}
if (io_submit(pContext, NUM_EVENTS, apIOCB) != NUM_EVENTS)
{
perror("io_submit");
return 6;
}
tTimeSpec.tv_sec = 0;
tTimeSpec.tv_nsec = 0;
r = io_getevents(pContext, 1, NUM_EVENTS, atIOEvent, &tTimeSpec);
if (r > 0)
{
for (j = 0; j u.c.buf);
二、
#define NUM_EVENTS 64
//这个宏意味着缓存大小
for (i = 0, apCustomIOCB = atCustomIOCB; i iocb, fd_File, buf, RD_WR_SIZE, i * RD_WR_SIZE);
|
我也遇到了相同问题,同问