Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <sys/resource.h>
  6. #include <stdbool.h>
  7. #include <pthread.h>
  8.  
  9. #define SIZE 2
  10.  
  11. struct block {
  12. int size;
  13. int *first;
  14. };
  15.  
  16. // void print_block_data(struct block *blk) {
  17. // printf("size: %d address: %p\n", blk->size, blk->first);
  18. // }
  19.  
  20. /* Combine the two halves back together. */
  21. void merge(struct block *left, struct block *right) {
  22. int combined[left->size + right->size];
  23. int dest = 0, l = 0, r = 0;
  24. while (l < left->size && r < right->size) {
  25. if (left->first[l] < right->first[r])
  26. combined[dest++] = left->first[l++];
  27. else
  28. combined[dest++] = right->first[r++];
  29. }
  30. while (l < left->size)
  31. combined[dest++] = left->first[l++];
  32. while (r < right->size)
  33. combined[dest++] = right->first[r++];
  34. memmove(left->first, combined, (left->size + right->size) * sizeof(int));
  35. }
  36. int count=0;
  37. /* Merge sort the data. */
  38. void *merge_sort(struct block *my_data) {
  39. int fd[2];
  40. pid_t ls_pid,sort_pid;
  41. // print_block_data(my_data);
  42. if (my_data->size > 1) {
  43. struct block left_block;
  44. struct block right_block;
  45. left_block.size = my_data->size / 2;
  46. left_block.first = my_data->first;
  47. right_block.size = left_block.size + (my_data->size % 2);
  48. right_block.first = my_data->first + left_block.size;
  49. //create pipe
  50. if (count == 0 ){
  51. count++;
  52. if (pipe(fd) == -1){
  53. fprintf(stderr,"pipe failed");
  54. return 1;
  55. }
  56. sort_pid = fork();
  57. if (sort_pid < 0){
  58. perror("/nChile 2 fork failed");
  59. return 1;
  60. }
  61. if (sort_pid == 0){
  62. //I AM PARENT
  63. merge_sort(&left_block);
  64.  
  65. }else{
  66. //I AM CHILd
  67. merge_sort(&right_block);
  68. write(fd[0],&left_block,sizeof(struct block));
  69. }
  70. read(fd[0],&left_block,sizeof(struct block));
  71. merge(&left_block, &right_block);
  72. }
  73.  
  74. merge_sort(&right_block);
  75. merge_sort(&left_block);
  76. merge(&left_block, &right_block);
  77. return NULL;
  78. }
  79. return NULL;
  80. }
  81.  
  82. /* Check to see if the data is sorted. */
  83. bool is_sorted(int data[], int size) {
  84. bool sorted = true;
  85. for (int i = 0; i < size - 1; i++) {
  86. if (data[i] > data[i + 1])
  87. sorted = false;
  88. }
  89. return sorted;
  90. }
  91.  
  92. int main(int argc, char *argv[]) {
  93. struct rlimit r_limit;
  94. getrlimit(RLIMIT_STACK, &r_limit);
  95. r_limit.rlim_cur = 1000000010000;
  96. setrlimit(RLIMIT_STACK, &r_limit);
  97.  
  98.  
  99. long size;
  100.  
  101. if (argc < 2) {
  102. size = SIZE;
  103. } else {
  104. size = atol(argv[1]);
  105. }
  106. struct block start_block;
  107. int data[size];
  108. start_block.size = size;
  109. start_block.first = data;
  110. for (int i = 0; i < size; i++) {
  111. data[i] = rand();
  112. }
  113. printf("starting---\n");
  114. merge_sort(&start_block);
  115. printf("---ending\n");
  116. printf(is_sorted(data, size) ? "sorted\n" : "not sorted\n");
  117. exit(EXIT_SUCCESS);
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement