A simple example of epoll/AIO/eventfd in combination _ sunfly _ Sina Blog

Source: Internet
Author: User

A simple example of epoll/AIO/eventfd in combination _ sunfly _ Sina Blog

A simple example of epoll/AIO/eventfd
(00:37:41)
Reprinted token
Tags:
Epoll
Libaio
Eventfd
Miscellaneous

# DEFINE _ gnu_source
# DEFINE _ stdc_format_macros
# Include <stdio. h>
# Include <errno. h>
# Include <libaio. h>
# Include <sys/eventfd. h>
# Include <sys/epoll. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <unistd. h>
# Include <stdint. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# Include <inttypes. h>
# Define test_file "aio_test_file"
# Define test_file_size (127*1024)
# Define num_events 128
# Define align_size 512
# Define rd_wr_size 1024
Struct custom_iocb
{
Struct 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: % LD \ n ",
Iocbp-> nth_request, (iocb-> aio_lio_opcode = io_assist_pread )? "Read": "write ",
Iocb-> U. C. offset, iocb-> U. C. nbytes, res, RES2 );
}
Int main (INT argc, char * argv [])
{
Int EFD, FD, epfd;
Io_context_t CTX;
Struct timespec TMS;
Struct io_event events [num_events];
Struct custom_iocb iocbs [num_events];
Struct iocb * iocbps [num_events];
Struct custom_iocb * iocbp;
Int I, j, R;
Void * Buf;
Struct epoll_event epevent;
EFD = eventfd (0, efd_nonblock | efd_cloexec );
If (EFD =-1 ){
Perror ("eventfd ");
Return 2;
}
FD = open (test_file, o_rdwr | o_creat | o_direct, 0644 );
If (FD =-1 ){
Perror ("open ");
Return 3;
}
Ftruncate (FD, test_file_size );

CTX = 0;
If (io_setup (8192, & CTX )){
Perror ("io_setup ");
Return 4;
}
If (posix_memalign (& Buf, align_size, rd_wr_size )){
Perror ("posix_memalign ");
Return 5;
}
Printf ("Buf: % P \ n", Buf );
For (I = 0, iowhite = iocbs; I <num_events; ++ I, ++ iowhite ){
Iocbps [I] = & iocbp-> iocb;
Io_prep_pread (& iocbp-> iocb, FD, Buf, rd_wr_size, I * rd_wr_size );
Io_set_eventfd (> iocb, EFD );
Io_set_callback (> iocb, aio_callback );
Iocbp-> nth_request = I + 1;
}
If (io_submit (CTX, num_events, iocbps )! = Num_events ){
Perror ("io_submit ");
Return 6;
}
Epfd = epoll_create (1 );
If (epfd =-1 ){
Perror ("epoll_create ");
Return 7;
}
Epevent. Events = epollin | epollet;
Epevent. Data. PTR = NULL;
If (epoll_ctl (epfd, epoll_ctl_add, EFD, & epevent )){
Perror ("epoll_ctl ");
Return 8;
}
I = 0;
While (I <num_events ){
Uint64_t finished_aio;
If (epoll_wait (epfd, & epevent, 1,-1 )! = 1 ){
Perror ("epoll_wait ");
Return 9;
}
If (read (EFD, & finished_aio, sizeof (finished_aio ))! = Sizeof (finished_aio )){
Perror ("read ");
Return 10;
}
Printf ("finished Io number: %" priu64 "\ n", finished_aio );

While (finished_aio> 0 ){
TMS. TV _sec = 0;
TMS. TV _nsec = 0;
R = io_getevents (CTX, 1, num_events, events, & TMS );
If (r> 0 ){
For (j = 0; j <r; ++ J ){
(Io_callback_t) (events [J]. Data) (CTX, events [J]. OBJ, events [J]. Res, events [J]. RES2 );
}
I + = R;
Finished_aio-= R;
}
}
}

Close (epfd );
Free (BUF );
Io_destroy (CTX );
Close (FD );
Close (EFD );
Remove (test_file );
Return 0;
}
Note:
1. Run the following command on centos 6.2 (libaio-devel 0.3.107-10 ):
2. the RES field in struct io_event indicates the number of bytes read or a negative error code. In the latter case,-res indicates the corresponding
Errno. If the RES2 field is 0, the request is successful. Otherwise, the request fails.
3. iocb must be valid during AIO request execution
4. above Program By extending the iocb structure to save additional information (nth_request), and using iocb. Data
To save the callback function address. If the callback function is fixed, you can use iocb. Data to save additional information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.