Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <aio.h>
- #include <time.h>
- //////////////////////////////////////////////////
- /// ///
- /// Run using either one of these forms: ///
- /// ///
- /// ./async read filename ///
- /// ./async write filename ///
- /// ///
- /// write makes a copy of the file ///
- /// ///
- /// ///
- /// Erman Haskan (2010510036) ///
- /// 2013 - DEU ///
- /// ///
- //////////////////////////////////////////////////
- #define BUF_SIZE 1024
- int main(int argc , char *argv[])
- {
- if(argc!=3)
- {
- printf("Need exactly 2 arguments\n");
- exit(1);
- }
- int write;
- if(strncmp(argv[1],"read",4)==0) // READ OPERATION
- {
- write=0;
- }
- else if(strncmp(argv[1],"write",5)==0) // READ + WRITE OPERATION
- {
- write=1;
- }
- else
- {
- printf("Invalid arguments\n");
- exit(1);
- }
- char* fileName=argv[2];
- FILE * fp = fopen(fileName, "r");
- fseek(fp, 0L, SEEK_END);
- int num=ftell(fp)/BUF_SIZE; // NUMBER OF READ/WRITE CYCLES
- fclose(fp);
- char buf[BUF_SIZE];
- int fd,fd2,i;
- struct aiocb aiocb;
- struct aiocb aiocb2;
- if ((fd= open(fileName, O_RDWR | O_EXCL, S_IRUSR | S_IWUSR)) == -1)
- {
- printf(" Error at open(): %s\n", strerror(errno));
- exit(1);
- }
- memset(&aiocb, 0, sizeof(struct aiocb));
- aiocb.aio_fildes = fd;
- aiocb.aio_buf = buf;
- aiocb.aio_nbytes = BUF_SIZE;
- aiocb.aio_lio_opcode = LIO_READ;
- if(write)
- {
- char writeFile[100];
- sprintf (writeFile, "%s Copy", fileName);
- fd2 = open(writeFile, O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR);
- memset(&aiocb2, 0, sizeof(struct aiocb));
- aiocb2.aio_fildes = fd2;
- aiocb2.aio_nbytes = BUF_SIZE;
- aiocb2.aio_buf=buf;
- }
- /////////////////////////////////////////////////// READ/WRITE OPERATION START
- clock_t start=clock();
- for(i=0;i<num;i++)
- {
- aiocb.aio_offset = i*BUF_SIZE;
- if (aio_read(&aiocb) == -1)
- {
- printf("AIO READ ERROR");
- exit(2);
- }
- while (aio_error(&aiocb) == EINPROGRESS);
- aio_return(&aiocb);
- if(write)
- {
- aiocb2.aio_offset = i*BUF_SIZE;
- if (aio_write(&aiocb2) == -1)
- {
- printf("AIO WRITE ERROR");
- exit(2);
- }
- while (aio_error(&aiocb2) == EINPROGRESS);
- aio_return(&aiocb2);
- }
- }
- clock_t end=clock();
- ////////////////////////////////////////////////// READ/WRITE OPERATION END
- printf("%f\n",(float)(end-start) / CLOCKS_PER_SEC);
- close(fd);
- if(write)
- {
- close(fd2);
- printf ("WRITE DONE\n");
- }
- else
- {
- printf ("READ DONE\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement