Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 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],&right_block,sizeof(struct block));
  69. }
  70.  
  71. merge(&left_block, &right_block);
  72. read(fd[0],&left_block,sizeof(struct block));
  73. return NULL;
  74. }
  75.  
  76. merge_sort(&right_block);
  77. merge_sort(&left_block);
  78. merge(&left_block, &right_block);
  79.  
  80. }
  81. return NULL;
  82. }
  83.  
  84. /* Check to see if the data is sorted. */
  85. bool is_sorted(int data[], int size) {
  86. bool sorted = true;
  87. for (int i = 0; i < size - 1; i++) {
  88. if (data[i] > data[i + 1])
  89. sorted = false;
  90. }
  91. return sorted;
  92. }
  93.  
  94. int main(int argc, char *argv[]) {
  95. struct rlimit r_limit;
  96. getrlimit(RLIMIT_STACK, &r_limit);
  97. r_limit.rlim_cur = 1000000010000;
  98. setrlimit(RLIMIT_STACK, &r_limit);
  99.  
  100.  
  101. long size;
  102.  
  103. if (argc < 2) {
  104. size = SIZE;
  105. } else {
  106. size = atol(argv[1]);
  107. }
  108. struct block start_block;
  109. int data[size];
  110. start_block.size = size;
  111. start_block.first = data;
  112. for (int i = 0; i < size; i++) {
  113. data[i] = rand();
  114. }
  115. printf("starting---\n");
  116. merge_sort(&start_block);
  117. printf("---ending\n");
  118. printf(is_sorted(data, size) ? "sorted\n" : "not sorted\n");
  119. exit(EXIT_SUCCESS);
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement