Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. /* Dainius Saltenis IFF-6/13 daisal*/
  2. /* Failas: daisal_aio01.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 daisal_test_open(const char *name, int flags);
  15. int daisal_test_close(int fd);
  16. int daisal_test_aio_read( const int d, struct aiocb *aiorp, void *buf, const int count, int write_dskr );
  17.  
  18. int daisal_test_open(const char *name, int flags){
  19. int dskr;
  20. dskr = open( name, flags);
  21. if( dskr == -1 ){
  22. perror( name );
  23. exit(1);
  24. }
  25. printf( "dskr = %d\n", dskr );
  26. return dskr;
  27. }
  28.  
  29. int daisal_test_close(int fd){
  30. int rv;
  31. rv = close( fd );
  32. if( rv != 0 ) perror ( "close() failed" );
  33. else puts( "closed" );
  34. return rv;
  35. }
  36.  
  37. int daisal_test_aio_read( const int d, struct aiocb *aiorp, void *buf, const int count, const int write_dskr ){
  38. int rv = 0;
  39. int read_count = 0;
  40. memset( (void *)aiorp, 0, sizeof( struct aiocb ) );
  41. const struct aiocb *aioptr[1];
  42. while (read_count < count)
  43. {
  44. aiorp->aio_fildes = d;
  45. aiorp->aio_buf = buf;
  46. aiorp->aio_nbytes = count - read_count;
  47. aiorp->aio_offset = read_count;
  48. rv = aio_read( aiorp );
  49. printf( "read %d\n", rv );
  50. if( rv != 0 ){
  51. perror( "aio_read failed" );
  52. abort();
  53. }
  54. aioptr[0] = aiorp;
  55. rv = aio_suspend( aioptr, 1, NULL );
  56. if( rv != 0 ){
  57. perror( "aio_suspend failed" );
  58. abort();
  59. }
  60. read_count += aio_return( aiorp );
  61. if (read_count > count)
  62. pwrite(write_dskr, buf, sizeof(buf) - (read_count - count), read_count);
  63. else
  64. pwrite(write_dskr, buf, sizeof(buf), read_count);
  65. }
  66.  
  67. return rv;
  68. }
  69.  
  70.  
  71. int main( int argc, char * argv[] ){
  72. int d, dw;
  73. int flags_d = O_RDONLY;
  74. int flags_dw = O_WRONLY | O_CREAT | O_TRUNC;
  75. struct aiocb aior;
  76. char buffer[BUFFLEN];
  77.  
  78. printf( "(C) 2018 Dainius Saltenis, %s\n", __FILE__ );
  79. d = daisal_test_open( "/dev/urandom", flags_d );
  80. dw = daisal_test_open( argv[1] , flags_dw);
  81. daisal_test_aio_read ( d, &aior, buffer, BUFFLEN, dw );
  82. daisal_test_close( d );
  83. daisal_test_close( dw );
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement