Advertisement
Guest User

Untitled

a guest
Dec 18th, 2014
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <stdlib.h>
  9. #include <aio.h>
  10. #include <time.h>
  11.  
  12.  
  13. //////////////////////////////////////////////////
  14. ///                                            ///
  15. ///  Run using either one of these forms:      ///
  16. ///                                            ///
  17. ///  ./async read filename                     ///
  18. ///  ./async write filename                    ///
  19. ///                                            ///
  20. ///  write makes a copy of the file            ///
  21. ///                                            ///
  22. ///                                            ///
  23. ///  Erman Haskan (2010510036)                 ///
  24. ///  2013 - DEU                                ///
  25. ///                                            ///
  26. //////////////////////////////////////////////////
  27.  
  28.  
  29. #define BUF_SIZE 1024
  30.  
  31. int main(int argc , char *argv[])
  32. {
  33.     if(argc!=3)
  34.     {
  35.         printf("Need exactly 2 arguments\n");
  36.         exit(1);
  37.     }
  38.    
  39.     int write;
  40.    
  41.     if(strncmp(argv[1],"read",4)==0) // READ OPERATION
  42.     {
  43.         write=0;
  44.     }
  45.     else if(strncmp(argv[1],"write",5)==0) // READ + WRITE OPERATION
  46.     {
  47.         write=1;
  48.     }
  49.     else
  50.     {
  51.         printf("Invalid arguments\n");
  52.         exit(1);
  53.     }
  54.    
  55.     char* fileName=argv[2];
  56.  
  57.     FILE * fp = fopen(fileName, "r");
  58.     fseek(fp, 0L, SEEK_END);
  59.     int num=ftell(fp)/BUF_SIZE; // NUMBER OF READ/WRITE CYCLES
  60.     fclose(fp);
  61.    
  62.     char buf[BUF_SIZE];
  63.     int fd,fd2,i;
  64.     struct aiocb aiocb;
  65.     struct aiocb aiocb2;
  66.  
  67.    
  68.     if ((fd= open(fileName, O_RDWR | O_EXCL, S_IRUSR | S_IWUSR)) == -1)
  69.     {
  70.         printf(" Error at open(): %s\n", strerror(errno));
  71.         exit(1);
  72.     }
  73.  
  74.     memset(&aiocb, 0, sizeof(struct aiocb));
  75.     aiocb.aio_fildes = fd;
  76.     aiocb.aio_buf = buf;
  77.     aiocb.aio_nbytes = BUF_SIZE;
  78.     aiocb.aio_lio_opcode = LIO_READ;
  79.    
  80.     if(write)
  81.     {
  82.         char writeFile[100];
  83.         sprintf (writeFile, "%s Copy", fileName);
  84.         fd2 = open(writeFile, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
  85.         memset(&aiocb2, 0, sizeof(struct aiocb));
  86.         aiocb2.aio_fildes = fd2;
  87.         aiocb2.aio_nbytes = BUF_SIZE;
  88.         aiocb2.aio_buf=buf;
  89.     }
  90.  
  91.    
  92.     /////////////////////////////////////////////////// READ/WRITE OPERATION START
  93.    
  94.     clock_t start=clock();
  95.    
  96.     for(i=0;i<num;i++)
  97.     {
  98.         aiocb.aio_offset = i*BUF_SIZE;
  99.         if (aio_read(&aiocb) == -1)
  100.         {
  101.             printf("AIO READ ERROR");
  102.             exit(2);
  103.         }
  104.         while (aio_error(&aiocb) == EINPROGRESS);
  105.         aio_return(&aiocb);
  106.        
  107.         if(write)
  108.         {
  109.             aiocb2.aio_offset = i*BUF_SIZE;
  110.             if (aio_write(&aiocb2) == -1)
  111.             {
  112.                 printf("AIO WRITE ERROR");
  113.                 exit(2);
  114.             }
  115.             while (aio_error(&aiocb2) == EINPROGRESS);
  116.             aio_return(&aiocb2);
  117.         }
  118.     }
  119.    
  120.     clock_t end=clock();
  121.    
  122.     ////////////////////////////////////////////////// READ/WRITE OPERATION END
  123.    
  124.    
  125.     printf("%f\n",(float)(end-start) / CLOCKS_PER_SEC);
  126.    
  127.     close(fd);
  128.    
  129.     if(write)
  130.     {
  131.         close(fd2);
  132.         printf ("WRITE DONE\n");
  133.     }
  134.     else
  135.     {
  136.         printf ("READ DONE\n");
  137.     }
  138.    
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement