Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <sys/times.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11.  
  12.  
  13. void generate(char *filename, int size, int bytes) {
  14. int file = open(filename, O_CREAT | O_EXCL, 0666);
  15. if (file < 0) {
  16. file = open(filename, O_RDWR | O_APPEND);
  17. printf("File already exist, appending \n");
  18. }
  19. time_t t;
  20. srand((unsigned) time(&t));
  21.  
  22. for (int i = 0; i < size; i++) {
  23. char result[bytes];
  24. for (int j = 0; j < bytes; j++) {
  25. char randomletter = 'a' + (rand() % 26);
  26. result[j] = randomletter;
  27. }
  28. write(file, result, bytes);
  29. write(file, "\n", 1);
  30. }
  31.  
  32. close(file);
  33. }
  34.  
  35. void sort(char *filename, int size, int bytes, char *variant) {
  36. int i = 1;
  37. if (strcmp(variant, "sys") == 0) {
  38. int src = open(filename, O_RDWR);
  39. while (i < size) {
  40. char *first, *second;
  41. first = (char *) malloc(sizeof(char) * bytes);
  42. second = (char *) malloc(sizeof(char) * bytes);
  43. if (first == NULL || second == NULL) {
  44. printf("Could not allocate memory!");
  45. exit(1);
  46. }
  47. int pos;
  48. if (i == 1) {
  49. pos = 0;
  50. } else {
  51. pos = (i - 1) * (bytes + 1);
  52. }
  53. lseek(src, pos, SEEK_SET);
  54. int j = i;
  55. read(src, first, bytes);
  56. lseek(src, 1, SEEK_CUR);
  57. read(src, second, bytes);
  58. lseek(src, 1, SEEK_CUR);
  59. lseek(src, -(bytes + 1), SEEK_CUR);
  60. while (j > 0 && first[0] > second[0]) {
  61. write(src, first, bytes);
  62. lseek(src, -((bytes * 2) + 1), SEEK_CUR);
  63. write(src, second, bytes);
  64. if (j == 1) {
  65. break;
  66. }
  67. lseek(src, -((bytes * 2) + 1), SEEK_CUR);
  68. read(src, first, bytes);
  69. lseek(src, 1, SEEK_CUR);
  70. j--;
  71. }
  72. i++;
  73. }
  74. close(src);
  75. } else if (strcmp(variant, "lib") == 0) {
  76. FILE *src = fopen(filename, "r+");
  77. while (i < size) {
  78. char *first, *second;
  79. first = (char *) malloc(sizeof(char) * bytes);
  80. second = (char *) malloc(sizeof(char) * bytes);
  81. if (first == NULL || second == NULL) {
  82. printf("Could not allocate memory!");
  83. exit(1);
  84. }
  85. int pos;
  86. if (i == 1) {
  87. pos = 0;
  88. } else {
  89. pos = (i - 1) * (bytes + 1);
  90. }
  91. fseek(src, pos, 0);
  92. int j = i;
  93. fread(first, 1, bytes, src);
  94. fseek(src, 1, 1);
  95. fread(second, 1, bytes, src);
  96. fseek(src, 1, 1);
  97. fseek(src, -(bytes + 1), 1);
  98. while (j > 0 && first[0] > second[0]) {
  99. fwrite(first, 1, bytes, src);
  100. fseek(src, -((bytes * 2) + 1), 1);
  101. fwrite(second, 1, bytes, src);
  102. if (j == 1) {
  103. break;
  104. }
  105. fseek(src, -((bytes * 2) + 1), 1);
  106. fread(first, 1, bytes, src);
  107. fseek(src, 1, 1);
  108. j--;
  109. }
  110. i++;
  111. }
  112. fclose(src);
  113. }
  114. }
  115.  
  116. void copy(char *srcname, char *destname, int size, int bytes) {
  117. int src = open(srcname, O_RDONLY);
  118. int dest = open(destname, O_WRONLY | O_CREAT, 0666);
  119. for (int i = 0; i < size; i++) {
  120. char line[bytes];
  121. read(src, line, bytes);
  122. off_t off = 1;
  123. lseek(src, off, SEEK_CUR);
  124. write(dest, line, bytes);
  125. write(dest, "\n", 1);
  126. }
  127. close(src);
  128. close(dest);
  129. }
  130.  
  131. void printTimeSpent(struct timespec start, struct timespec end) {
  132. long msend = end.tv_nsec / 100000;
  133. int time = (end.tv_sec - start.tv_sec);
  134. printf("time spent: %i.%lu s \n", time, msend);
  135. }
  136.  
  137. int main(int argc, char **argv) {
  138. struct timespec realtimestart;
  139. struct timespec finish;
  140. struct tms tms_counter;
  141. double cpu_time_used;
  142. clock_t cpu_start, cpu_end;
  143.  
  144. cpu_start = clock();
  145. clock_gettime(CLOCK_REALTIME, &realtimestart);
  146.  
  147. //PROGRAM START
  148. if (argc < 2) {
  149. printf("No argument passed");
  150. return 0;
  151. }
  152. if (strcmp(argv[1], "generate") == 0) {
  153. int size, bytes;
  154. sscanf(argv[3], "%d", &size);
  155. sscanf(argv[4], "%d", &bytes);
  156. generate(argv[2], size, bytes);
  157. }
  158. if (strcmp(argv[1], "sort") == 0) {
  159. int size, bytes;
  160. sscanf(argv[3], "%d", &size);
  161. sscanf(argv[4], "%d", &bytes);
  162. sort(argv[2], size, bytes, argv[5]);
  163. }
  164. if (strcmp(argv[1], "copy") == 0) {
  165. int size, bytes;
  166. sscanf(argv[4], "%d", &size);
  167. sscanf(argv[5], "%d", &bytes);
  168. copy(argv[2], argv[3], size, bytes);
  169. }
  170.  
  171. //PROGRAM END
  172.  
  173. cpu_end = clock();
  174. clock_gettime(CLOCK_REALTIME, &finish);
  175. times(&tms_counter);
  176.  
  177. printTimeSpent(realtimestart, finish);
  178.  
  179. cpu_time_used = ((double) (cpu_end - cpu_start)) / CLOCKS_PER_SEC;
  180. printf("cpu time: %f s \n", cpu_time_used);
  181.  
  182. int processor_time_used = (int) tms_counter.tms_utime;
  183. printf("processor clock ticks: %u \n", processor_time_used);
  184.  
  185. return 0;
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement