Advertisement
Guest User

test sem

a guest
Dec 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 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 trying to pull file from the GIT\n");
  23. sem_wait(&mutex);
  24. printf("Pulling Success\n");
  25. sem_wait(&pushB);
  26. printf("A:%d\n",i);
  27. printf("A is working on the code.\n");
  28. sleep(1);
  29. printf("A is trying to commit to the GIT system\n");
  30. sem_post(&pushA);
  31. printf("Commit Success\n");
  32. sem_post(&mutex);
  33. }
  34.  
  35. }
  36.  
  37. void *PersonB_DeadLock(void *temp) {
  38. int i;
  39. for(i = 0;i<2;i++)
  40. {
  41. printf("A trying to pull file from the GIT\n");
  42. sem_wait(&mutex);
  43. printf("Pulling Success\n");
  44. sem_wait(&pushA);
  45. printf("B:%d\n",i);
  46. printf("B is working on the code.\n");
  47. sleep(1);
  48. printf("B is trying to commit to the GIT system\n");
  49. sem_post(&pushB);
  50. printf("Commit Success\n");
  51. sem_post(&mutex);
  52. }
  53.  
  54.  
  55. printf("B Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
  56.  
  57. }
  58.  
  59. void *PersonA_UnDeadLock(void *temp) {
  60.  
  61. int i;
  62. for(i = 0;i<2;i++)
  63. {
  64. printf("A trying to pull file from the GIT\n");
  65. sem_wait(&pushB);
  66. printf("Pulling Success\n");
  67. sem_wait(&mutex);
  68. printf("A:%d\n",i);
  69. printf("A is working on the code.\n");
  70. sleep(1);
  71. printf("A is trying to commit to the GIT system\n");
  72. amountCommit = amountCommit +1;
  73. sem_post(&mutex);
  74. amountSuccess = amountSuccess + 1;
  75. printf("Commit Success\n");
  76. sem_post(&pushA);
  77. }
  78.  
  79.  
  80. printf("A Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
  81. }
  82.  
  83. void *PersonB_UnDeadLock(void *temp) {
  84.  
  85. int i;
  86. for(i = 0;i<2;i++)
  87. {
  88. printf("A trying to pull file from the GIT\n");
  89. sem_wait(&pushA);
  90. printf("Pulling Success\n");
  91. sem_wait(&mutex);
  92. printf("B:%d\n",i);
  93. printf("B is working on the code.\n");
  94. sleep(1);
  95. printf("B is trying to commit to the GIT system\n");
  96. amountCommit = amountCommit +1;
  97. sem_post(&mutex);
  98. printf("Commit Success\n");
  99. amountSuccess = amountSuccess +1;
  100. sem_post(&pushB);
  101. }
  102.  
  103.  
  104. printf("B Commit >= %d, %d, %d\n", amountCommit, amountSuccess, amountErrors);
  105. }
  106.  
  107. int main() {
  108. sem_init(&mutex, 0, 1);
  109. sem_init(&pushB, 0, 1);
  110. pthread_t t[2];
  111. pthread_create(&t[0], NULL, &PersonA_DeadLock, "PersonA");
  112. pthread_create(&t[1], NULL, &PersonB_DeadLock, "PersonB");
  113. pthread_join(t[1], NULL);
  114. pthread_join(t[0], NULL);
  115. sem_destroy(&mutex);
  116. sem_destroy(&pushB);
  117. sem_destroy(&pushA);
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement