daily pastebin goal
62%
SHARE
TWEET

Untitled

a guest Apr 26th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* Tomas Mazvila GRP loginas */
  2. /* Failas: tommaz2_aio02.c */
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <aio.h>
  12. #define BUFFLEN 1048576
  13.  
  14. int kp_test_open(const char *name);
  15. int kp_test_close(int fd);
  16. int write_open(const char *name);
  17.  
  18. int kp_test_open(const char *name){
  19.         int dskr;
  20.         dskr = open( name, O_RDONLY );
  21.         if( dskr == -1 ){
  22.                 perror( name );
  23.                 exit(1);
  24.         }
  25.         printf( "dskr = %d\n", dskr );
  26.         return dskr;
  27. }
  28. int write_open(const char *name){
  29.         int dskr;
  30.         dskr = open(name, O_WRONLY |  O_CREAT, 644);
  31.         if( dskr == -1){
  32.                 perror(name);
  33.                 exit(1);
  34.         }
  35.         printf("dskr = %d\n", dskr);
  36.         return dskr;
  37. }
  38. int kp_test_close(int fd){
  39.         int rv;
  40.         rv = close( fd );
  41.         if( rv != 0 ) perror ( "close() failed" );
  42.         else puts( "closed" );
  43.         return rv;
  44. }
  45.  
  46. int do_stuff(const int d, const int d2, struct aiocb *aiorp, void *buf, const int count) {
  47.         int rv = 0;
  48.         const struct aiocb *aioptr[1];
  49.         memset((void *)aiorp, 0, sizeof(struct aiocb));
  50.        
  51.         aiorp->aio_fildes = d;
  52.         aiorp->aio_buf = buf;
  53.         aiorp->aio_nbytes = count;
  54.         aiorp->aio_offset = 0;
  55.         aioptr[0] = aiorp;
  56.        
  57.         rv = aio_read(aiorp);
  58.         if( rv != 0 ){
  59.                 perror( "aio_read failed" );
  60.                 abort();
  61.         }
  62.         int aio_ret_v = 0;
  63.         int total = 0;
  64.         aio_suspend(aioptr, 1, NULL);
  65.         aio_ret_v = aio_return(aiorp);
  66.         total += aio_ret_v;    
  67.         while( aio_ret_v == 0){
  68.                 aio_read(aiorp);                
  69.                 aio_suspend(aioptr, 1, NULL);
  70.                 aio_ret_v = aio_return(aiorp);
  71.                 total += aio_ret_v;
  72.                 printf(".\n");
  73.         }
  74.         aiorp -> aio_fildes = d2;
  75.         do {
  76.                 aio_write(aiorp);
  77.                 aio_suspend(aioptr, 1, NULL);
  78.                 aio_ret_v = aio_return(aiorp);
  79.         } while(aio_ret_v == 0);
  80.         printf( "AIO complete, %d bytes read.\n", total );
  81.         return rv;
  82. }
  83.  
  84.  
  85. int main( int argc, char * argv[] ){
  86.         int d, d2;
  87.         struct aiocb aior;
  88.         char buffer[BUFFLEN];
  89.         d = kp_test_open( "/dev/urandom" );
  90.         d2 = write_open("testwrite.txt");
  91.         do_stuff(d, d2, &aior, buffer, sizeof(buffer));
  92.         kp_test_close( d );
  93.         printf( "(C) 2018 Tomas Mazvila, %s\n", __FILE__ );
  94.         return 0;
  95. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top