Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "domaci1.h"
  4. #include <math.h>
  5. #define UNLOCKED 0
  6. #define LOCKED 1
  7. #define TIMED_OUT 2
  8. typedef struct Node{
  9. int flag;
  10. struct Node * next;
  11. } Node;
  12.  
  13. Node* tail = NULL;
  14. int sucsses = NULL;
  15. int sucssesTO = NULL;
  16. double startID[80];
  17. double minTime = 10.0;
  18. double maxTime = 0.0;
  19.  
  20.  
  21. void init_MCSLock(){
  22.  
  23. }
  24.  
  25. int lock_n_threads_with_timeout(int id, int* local, int timeout)
  26. {
  27. double start = get_time();
  28. startID[id] = start;
  29. double wait = timeout/1000;
  30. //printf(" start =%d \n", start);
  31. //fflush(stdout);
  32.  
  33. Node* node = (Node*) malloc(sizeof(Node));
  34. node->flag = LOCKED;
  35. node->next = NULL;
  36. *local = node;
  37.  
  38. Node* old = (Node*) get_and_set((int*)&tail, (int)node);
  39.  
  40.  
  41.  
  42. if(old != NULL){
  43. old->next = node;
  44.  
  45. while(1){
  46. double end = get_time();
  47. double waitedTime = (double)end - start;
  48. //printf("end= %lf | waitedTime= %lf\n", end, waitedTime);
  49. //fflush(stdout);
  50. if((waitedTime) < 0.2){
  51. if(node->flag == LOCKED){
  52. //printf("Sleep ");
  53. //fflush(stdout);
  54. sleep(1);
  55. }else if(node->flag == UNLOCKED){
  56. //printf("UNLOCKED ");
  57. //fflush(stdout);
  58. //node->flag = UNLOCKED;
  59. return 1;
  60. }
  61. }else if((waitedTime) >= 0.2){
  62. //printf("TO ");
  63. //fflush(stdout);
  64. node->flag = TIMED_OUT;
  65. return 0;
  66. }
  67.  
  68. }
  69. } else{
  70. printf("FIRST ");
  71. fflush(stdout);
  72. // node->flag = UNLOCKED;
  73. return 1;
  74. }
  75. }
  76. void unlock_n_threads_with_timeout(int id, int* local)
  77. {
  78. Node* node = (Node*) *local;
  79.  
  80.  
  81. if(node->next == NULL){
  82. sucsses = (Node*) compare_and_set((int*)&tail, (int)node, (int)NULL);
  83. if(sucsses){
  84. printf("FIN ");
  85. fflush(stdout);
  86. }else{
  87. while(node->next == NULL){
  88. printf("sucess_Fail ");
  89. fflush(stdout);
  90. sleep(1);
  91. }
  92.  
  93. node->next->flag = UNLOCKED;
  94.  
  95. }
  96. } else{
  97. Node* nextNode = (Node*) node->next;
  98. while(1){
  99. //LOCKED->UNLOCKED
  100. if((Node*)compare_and_set((int*)&nextNode->flag,(int) LOCKED,(int) UNLOCKED)){
  101. //printf("LOCK->UNLOCK ");
  102. //fflush(stdout);
  103. break;
  104. }
  105. else {
  106. //TIME_OUT
  107. if((Node*)compare_and_set((int*)&nextNode->next,(int)LOCKED, (int) UNLOCKED )){ //TIME_OUT-ovao, da li je sledeci ok?
  108. printf("TO_LOCK->UNLOCK\n");
  109. fflush(stdout);
  110. break;
  111. }else if(nextNode->next == NULL){
  112. if((Node*)compare_and_set((int*)&tail,(int)nextNode,(int)NULL)){ //TIME_OUT-ovao, da li je zadnji?
  113. printf("FIN_TO\n");
  114. fflush(stdout);
  115. break;
  116. }else{
  117. while(nextNode->next == NULL){
  118. printf("sucessTO_Fail ");
  119. fflush(stdout);
  120. sleep(1);
  121. break;
  122. }
  123. }
  124. }else nextNode = nextNode->next;
  125.  
  126. }
  127.  
  128. }
  129.  
  130. /* if(nextNode->flag == TIMED_OUT){
  131. if(nextNode->next == NULL){
  132. sucssesTO = (Node*)compare_and_set(&tail, nextNode, NULL);
  133. if(sucssesTO){
  134. break;
  135. }else while (nextNode->next == NULL)
  136. sleep(1);
  137. }
  138. }else{
  139. // printf("LOCKED->UNLOCKED \n");
  140. //// fflush(stdout);
  141. nextNode->flag = UNLOCKED;
  142. break;
  143. //free(node);
  144. } */
  145.  
  146. }
  147.  
  148.  
  149. double finalTime = get_time() - startID[id];
  150. if(finalTime > maxTime) maxTime = finalTime;
  151. if(finalTime < minTime) minTime = finalTime;
  152.  
  153. }
  154.  
  155. int main()
  156. {
  157. start_timeout_mutex_n_threads_test(200);
  158. printf("MAX DELTA = %.5lf\n", maxTime);
  159. printf("MIN DELTA = %.5lf\n", minTime);
  160. return 0;
  161.  
  162. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement