Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <aio.h>
- #define BUFFLEN 1024*1024
- int rm_open(const char *name){
- int dskr = open( name, O_RDONLY );
- if( dskr == -1 ){
- perror( name );
- exit(1);
- }
- printf( "dskr = %d\n", dskr );
- return dskr;
- }
- void rm_close(int fd){
- int rv = close( fd );
- if( rv != 0 ) perror ( "close() failed" );
- else puts( "closed" );
- }
- int rm_aio_read_start( const int d, struct aiocb *aiorp, void *buf, int count, int offset){
- memset( (void *)aiorp, 0, sizeof( struct aiocb ) );
- aiorp->aio_fildes = d;
- aiorp->aio_buf = buf;
- aiorp->aio_nbytes = count;
- aiorp->aio_offset = offset;
- printf("%d\n", aiorp->aio_offset);
- int rv = 0;
- rv = aio_read( aiorp );
- if( rv != 0 ){
- perror( "aio_read failed" );
- abort();
- }
- return rv;
- }
- void rm_dummy( const void *data, int n ){
- int i, cnt = 0;
- for( i = 0; i < n; i++ )
- if( ((char*)data)[i] == '\0' ) cnt++;
- printf( "Number of '0' in data: %d\n", cnt );
- }
- void rm_aio_read_waitcomplete( struct aiocb *aiorp ){
- const struct aiocb *aioptr[1];
- aioptr[0] = aiorp;
- int rv = aio_suspend( aioptr, 1, NULL );
- if( rv != 0 ){
- perror( "aio_suspend failed" );
- abort();
- }
- rv = aio_return( aiorp );
- printf( "AIO complete, %d bytes read.\n", rv );
- }
- int main(){
- int offset = 0;
- struct aiocb aior;
- char buffer[BUFFLEN];
- int d = rm_open( "/dev/urandom" );
- int i = 0;
- while(i < 10){
- rm_aio_read_start( d, &aior, buffer, sizeof(buffer), offset );
- offset += sizeof( struct aiocb);
- rm_dummy( buffer, sizeof(buffer) );
- rm_aio_read_waitcomplete( &aior );
- i++;
- }
- rm_close( d );
- rm_dummy( buffer, sizeof(buffer) );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement