Advertisement
Guest User

Untitled

a guest
Mar 29th, 2024
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <aio.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <signal.h>
  7. #include <string.h>
  8. #include <errno.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <signal.h>
  12. #include <stdint.h>
  13. #include <inttypes.h>
  14. int in_file, out_file, file_size, cluster_size, m = 1, n = 1, n_of_clusters, n_of_blocks, cur_block, write_counter;
  15. struct aio_operation {
  16. struct aiocb aio;
  17. char *buffer;
  18. int write_operation;
  19. void* next_operation;
  20. int block_no;
  21. };
  22. void aio_completion_handler(sigval_t sigval)
  23. {
  24. struct aio_operation *aio_op = (struct aio_operation *)sigval.sival_ptr;
  25. if (aio_op->write_operation)
  26. {
  27. printf("Successfully wrote block %i out of %i\n", aio_op->block_no, n_of_blocks);
  28. //free((char*)aio_op->buffer);
  29. if (cur_block <= n_of_blocks)
  30. {
  31. struct aiocb s;
  32. struct aio_operation op;
  33. char *charbuf = (char*)malloc(m * cluster_size * sizeof(char));
  34. s.aio_fildes = in_file;
  35. s.aio_offset = cur_block * cluster_size * m;
  36. s.aio_buf = charbuf;
  37. s.aio_nbytes = m * cluster_size;
  38. s.aio_reqprio = 0;
  39. s.aio_sigevent.sigev_notify = SIGEV_THREAD;
  40. s.aio_sigevent.sigev_signo = SIGRTMIN;
  41. s.aio_sigevent.sigev_notify_function = aio_completion_handler;
  42. s.aio_sigevent.sigev_notify_attributes = NULL;
  43. s.aio_sigevent.sigev_value.sival_ptr = &op;
  44. op.aio = s;
  45. op.buffer = charbuf;
  46. op.next_operation = NULL;
  47. op.write_operation = 0;
  48. op.block_no = cur_block;
  49. cur_block++;
  50. aio_read(&s);
  51. }
  52. else
  53. {
  54. if (write_counter == n_of_blocks)
  55. {
  56. close(in_file);
  57. close(out_file);
  58. printf("Copying successful\n");
  59. }
  60. }
  61. }
  62. else
  63. {
  64. printf("Successfully read block %i out of %i\n", aio_op->block_no, n_of_blocks);
  65. struct aiocb s;
  66. struct aio_operation op;
  67. s.aio_fildes = out_file;
  68. s.aio_offset = aio_op->aio.aio_offset;
  69. s.aio_buf = aio_op->aio.aio_buf;
  70. s.aio_nbytes = aio_op->aio.aio_nbytes;
  71. s.aio_reqprio = 0;
  72. s.aio_sigevent.sigev_notify = SIGEV_THREAD;
  73. s.aio_sigevent.sigev_signo = SIGRTMIN;
  74. s.aio_sigevent.sigev_notify_function = aio_completion_handler;
  75. s.aio_sigevent.sigev_notify_attributes = NULL;
  76. s.aio_sigevent.sigev_value.sival_ptr = &op;
  77. op.aio = s;
  78. op.buffer = (char*) aio_op->aio.aio_buf;
  79. op.next_operation = NULL;
  80. op.write_operation = 1;
  81. op.block_no = aio_op->block_no;
  82. aio_write(&s);
  83. write_counter++;
  84. }
  85. }
  86. int main()
  87. {
  88. struct stat buf;
  89. in_file = open("test.txt", O_RDONLY | O_NONBLOCK, 0666);
  90. if (in_file)
  91. {
  92. if (!fstat(in_file, &buf))
  93. {
  94. file_size = buf.st_size;
  95. cluster_size = buf.st_blksize;
  96. //cluster_size = 1;
  97. n_of_clusters = file_size / cluster_size;
  98. if (file_size % cluster_size)
  99. n_of_clusters++;
  100. n_of_blocks = n_of_clusters / m;
  101. if (n_of_clusters % m)
  102. n_of_blocks++;
  103. printf("Size is %i, block size is %i, n of clusters is %i, n of blocks is %i\n", file_size, cluster_size, n_of_clusters, n_of_blocks);
  104. out_file = open("test_copy.txt", O_CREAT | O_WRONLY | O_TRUNC | O_NONBLOCK, 0666);
  105. if (out_file)
  106. {
  107. cur_block = 0;
  108. write_counter = 0;
  109. for (int i = 0; i < n && cur_block != n_of_blocks; i++)
  110. {
  111. struct aiocb s;
  112. struct aio_operation op;
  113. char *charbuf = (char*)malloc(m * cluster_size * sizeof(char));
  114. s.aio_fildes = in_file;
  115. s.aio_offset = cur_block * cluster_size * m;
  116. s.aio_buf = charbuf;
  117. s.aio_nbytes = m * cluster_size;
  118. s.aio_reqprio = 0;
  119. s.aio_sigevent.sigev_notify = SIGEV_THREAD;
  120. s.aio_sigevent.sigev_signo = SIGRTMIN;
  121. s.aio_sigevent.sigev_notify_function = aio_completion_handler;
  122. s.aio_sigevent.sigev_notify_attributes = NULL;
  123. s.aio_sigevent.sigev_value.sival_ptr = &op;
  124. op.aio = s;
  125. op.buffer = charbuf;
  126. op.next_operation = NULL;
  127. op.write_operation = 0;
  128. op.block_no = cur_block;
  129. cur_block++;
  130. aio_read(&s);
  131. }
  132. }
  133. }
  134. }
  135. getchar();
  136.  
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement