Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Dainius Saltenis IFF-6/13 daisal*/
- /* Failas: daisal_aio01.c */
- #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 1048576
- int daisal_test_open(const char *name, int flags);
- int daisal_test_close(int fd);
- int daisal_test_aio_read( const int d, struct aiocb *aiorp, void *buf, const int count, int write_dskr );
- int daisal_test_open(const char *name, int flags){
- int dskr;
- dskr = open( name, flags);
- if( dskr == -1 ){
- perror( name );
- exit(1);
- }
- printf( "dskr = %d\n", dskr );
- return dskr;
- }
- int daisal_test_close(int fd){
- int rv;
- rv = close( fd );
- if( rv != 0 ) perror ( "close() failed" );
- else puts( "closed" );
- return rv;
- }
- int daisal_test_aio_read( const int d, struct aiocb *aiorp, void *buf, const int count, const int write_dskr ){
- int rv = 0;
- int read_count = 0;
- memset( (void *)aiorp, 0, sizeof( struct aiocb ) );
- const struct aiocb *aioptr[1];
- while (read_count < count)
- {
- aiorp->aio_fildes = d;
- aiorp->aio_buf = buf;
- aiorp->aio_nbytes = count - read_count;
- aiorp->aio_offset = read_count;
- rv = aio_read( aiorp );
- printf( "read %d\n", rv );
- if( rv != 0 ){
- perror( "aio_read failed" );
- abort();
- }
- aioptr[0] = aiorp;
- rv = aio_suspend( aioptr, 1, NULL );
- if( rv != 0 ){
- perror( "aio_suspend failed" );
- abort();
- }
- read_count += aio_return( aiorp );
- if (read_count > count)
- pwrite(write_dskr, buf, sizeof(buf) - (read_count - count), read_count);
- else
- pwrite(write_dskr, buf, sizeof(buf), read_count);
- }
- return rv;
- }
- int main( int argc, char * argv[] ){
- int d, dw;
- int flags_d = O_RDONLY;
- int flags_dw = O_WRONLY | O_CREAT | O_TRUNC;
- struct aiocb aior;
- char buffer[BUFFLEN];
- printf( "(C) 2018 Dainius Saltenis, %s\n", __FILE__ );
- d = daisal_test_open( "/dev/urandom", flags_d );
- dw = daisal_test_open( argv[1] , flags_dw);
- daisal_test_aio_read ( d, &aior, buffer, BUFFLEN, dw );
- daisal_test_close( d );
- daisal_test_close( dw );
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement