mariana12

Untitled

Dec 16th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<pthread.h>
  4. #include<semaphore.h>
  5. #include<unistd.h>
  6.  
  7. int oPush = 0, oPushA = 0, oPushB = 0, oPull =0; //0 mean
  8. int amountSuccess = 0;
  9. int amountErrors = 0;
  10. int amountCommit = 0;
  11.  
  12. pthread_mutex_t working = PTHREAD_MUTEX_INITIALIZER;
  13. pthread_mutex_t deadlockMutex = PTHREAD_MUTEX_INITIALIZER;
  14. pthread_cond_t pulling = PTHREAD_COND_INITIALIZER;
  15. sem_t pushA,pushB,mutex;
  16.  
  17. void *PersonA_DeadLock(void *temp) {
  18.  
  19. int i;
  20. for(i = 0;i<2;i++)
  21. {
  22. //printf("A\n");
  23. printf("A trying to pull file from the GIT\n");
  24. sem_wait(&mutex);
  25. //printf("A1\n");
  26. //printf("A trying to pull file from the GIT\n");
  27. printf("Pulling Success\n");
  28. sem_wait(&pushB);
  29. //printf("A2\n");
  30. printf("A:%d\n",i);
  31. printf("A is working on the code.\n");
  32. sleep(1);
  33. printf("A is trying to commit to the GIT system\n");
  34. //printf("A\n");
  35. sem_post(&pushA);
  36. //printf("A3\n");
  37. //printf("A is trying to commit to the GIT system\n");
  38. printf("Commit Success\n");
  39. sem_post(&mutex);
  40. }
  41.  
  42. }
  43.  
  44. void *PersonB_DeadLock(void *temp) {
  45. int i;
  46. for(i = 0;i<2;i++)
  47. {
  48. //printf("B\n");
  49. printf("A trying to pull file from the GIT\n");
  50. sem_wait(&mutex);
  51. //printf("B1\n");
  52. //printf("B trying to pull file from the GIT\n");
  53. printf("Pulling Success\n");
  54. sem_wait(&pushA);
  55. //printf("B2\n");
  56. printf("B:%d\n",i);
  57. printf("B is working on the code.\n");
  58. sleep(1);
  59. printf("B is trying to commit to the GIT system\n");
  60. //printf("B\n");
  61. sem_post(&pushB);
  62. //printf("B3\n");
  63. //printf("B is trying to commit to the GIT system\n");
  64. printf("Commit Success\n");
  65. sem_post(&mutex);
  66. }
  67.  
  68.  
  69. printf("B Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
  70.  
  71. }
  72.  
  73. void *PersonA_UnDeadLock(void *temp) {
  74.  
  75. int i;
  76. for(i = 0;i<2;i++)
  77. {
  78. //printf("A\n");
  79. printf("A trying to pull file from the GIT\n");
  80. sem_wait(&pushB);
  81. //printf("A1\n");
  82. //printf("A trying to pull file from the GIT\n");
  83. printf("Pulling Success\n");
  84. sem_wait(&mutex);
  85. //printf("A2\n");
  86. printf("A:%d\n",i);
  87. printf("A is working on the code.\n");
  88. sleep(1);
  89. printf("A is trying to commit to the GIT system\n");
  90. //printf("A\n");
  91. sem_post(&mutex);
  92. //printf("A3\n");
  93. //printf("A is trying to commit to the GIT system\n");
  94. printf("Commit Success\n");
  95. sem_post(&pushA);
  96. }
  97.  
  98.  
  99. printf("A Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
  100. }
  101.  
  102. void *PersonB_UnDeadLock(void *temp) {
  103.  
  104. int i;
  105. for(i = 0;i<2;i++)
  106. {
  107. //printf("B\n");
  108. printf("A trying to pull file from the GIT\n");
  109. sem_wait(&pushA);
  110. //printf("B1\n");
  111. //printf("B trying to pull file from the GIT\n");
  112. printf("Pulling Success\n");
  113. sem_wait(&mutex);
  114. //printf("B2\n");
  115. printf("B:%d\n",i);
  116. printf("B is working on the code.\n");
  117. sleep(1);
  118. printf("B is trying to commit to the GIT system\n");
  119. //printf("B\n");
  120. sem_post(&mutex);
  121. //printf("B3\n");
  122. //printf("B is trying to commit to the GIT system\n");
  123. printf("Commit Success\n");
  124. sem_post(&pushB);
  125. }
  126.  
  127.  
  128. printf("B Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
  129. }
  130.  
  131. int main() {
  132. sem_init(&mutex, 0, 1);
  133. sem_init(&pushB, 0, 1);
  134. pthread_t t[2];
  135. pthread_create(&t[0], NULL, &PersonA_DeadLock, "PersonA");
  136. pthread_create(&t[1], NULL, &PersonB_DeadLock, "PersonB");
  137. pthread_join(t[1], NULL);
  138. pthread_join(t[0], NULL);
  139. sem_destroy(&mutex);
  140. sem_destroy(&pushB);
  141. sem_destroy(&pushA);
  142. return 0;
  143. }
Add Comment
Please, Sign In to add comment