Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | None | 0 0
  1. /*
  2. The Merge Sort to use for Operating Systems Assignment 1 2019
  3. written by Robert Sheehan
  4.  
  5. Modified by: put your name here
  6. UPI: put your login here
  7.  
  8. By submitting a program you are claiming that you and only you have made
  9. adjustments and additions to this code.
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <unistd.h>
  15. #include <string.h>
  16. #include <sys/resource.h>
  17. #include <stdbool.h>
  18. #include <pthread.h>
  19.  
  20. #define SIZE 2
  21.  
  22. int THREAD_MAX=0;
  23. pthread_mutex_t mutex;
  24. long num_cpus;
  25.  
  26. struct block {
  27. int size;
  28. int *first;
  29. };
  30.  
  31. // void print_block_data(struct block *blk) {
  32. // printf("size: %d address: %p\n", blk->size, blk->first);
  33. // }
  34.  
  35. /* Combine the two halves back together. */
  36. void merge(struct block *left, struct block *right) {
  37. int combined[left->size + right->size];
  38. int dest = 0, l = 0, r = 0;
  39. while (l < left->size && r < right->size) {
  40. if (left->first[l] < right->first[r])
  41. combined[dest++] = left->first[l++];
  42. else
  43. combined[dest++] = right->first[r++];
  44. }
  45. while (l < left->size)
  46. combined[dest++] = left->first[l++];
  47. while (r < right->size)
  48. combined[dest++] = right->first[r++];
  49. memmove(left->first, combined, (left->size + right->size) * sizeof(int));
  50. }
  51.  
  52. /* Merge sort the data. */
  53. void *merge_sort(void* args) {
  54. // print_block_data(my_data);
  55. struct block *my_data = (struct block *) args;
  56.  
  57. if (my_data->size > 1) {
  58. struct block left_block;
  59. struct block right_block;
  60. left_block.size = my_data->size / 2;
  61. left_block.first = my_data->first;
  62. right_block.size = left_block.size + (my_data->size % 2);
  63. right_block.first = my_data->first + left_block.size;
  64.  
  65.  
  66. pthread_t thread1;
  67. if (THREAD_MAX<num_cpus){
  68. pthread_attr_t attr;
  69. pthread_attr_init(&attr);
  70. pthread_attr_setstacksize(&attr,900000000);
  71.  
  72. pthread_mutex_lock(&mutex);
  73. //printf("%d\n",THREAD_MAX);
  74. THREAD_MAX++;
  75. pthread_mutex_unlock(&mutex);
  76.  
  77. pthread_create(&thread1,&attr, merge_sort,&left_block);
  78. merge_sort(&right_block);
  79. pthread_join(thread1,NULL);
  80. pthread_mutex_lock(&mutex);
  81. THREAD_MAX--;
  82. pthread_mutex_unlock(&mutex);
  83. }else{
  84. merge_sort(&left_block);
  85. merge_sort(&right_block);
  86. }
  87. merge(&left_block, &right_block);
  88. pthread_mutex_destroy(&mutex);
  89. }
  90. }
  91.  
  92. /* Check to see if the data is sorted. */
  93. bool is_sorted(int data[], int size) {
  94. bool sorted = true;
  95. for (int i = 0; i < size - 1; i++) {
  96. if (data[i] > data[i + 1])
  97. sorted = false;
  98. }
  99. return sorted;
  100. }
  101.  
  102. int main(int argc, char *argv[]) {
  103. struct rlimit limit;
  104. getrlimit(RLIMIT_STACK, &limit);
  105. limit.rlim_cur=900000000;
  106. limit.rlim_max=900000000;
  107. setrlimit(RLIMIT_STACK, &limit);
  108. long size;
  109.  
  110. pthread_mutex_init(&mutex, NULL);
  111. num_cpus = sysconf( _SC_NPROCESSORS_ONLN);
  112.  
  113. if (argc < 2) {
  114. size = SIZE;
  115. } else {
  116. size = atol(argv[1]);
  117. }
  118. struct block start_block;
  119. int data[size];
  120. start_block.size = size;
  121. start_block.first = data;
  122. for (int i = 0; i < size; i++) {
  123. data[i] = rand();
  124. }
  125.  
  126. printf("starting---\n");
  127. merge_sort(&start_block);
  128. printf("---ending\n");
  129. printf(is_sorted(data, size) ? "sorted\n" : "not sorted\n");
  130. printf("\nStack Limit = %ld and %ld max\n",limit.rlim_cur, limit.rlim_max);
  131. exit(EXIT_SUCCESS);
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement