Advertisement
zaxisss

Untitled

Dec 2nd, 2020 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <semaphore.h>
  3.  
  4. int main(){
  5. sem_t Sem;
  6. int Wartosc;
  7. int i;
  8.  
  9. Wartosc=10;
  10. sem_init(&Sem,0,Wartosc);
  11. sem_getvalue(&Sem,&Wartosc);
  12. printf("\nWartosc zmiennej semaforowej Sem=%d ", Wartosc);
  13.  
  14. for(i=Wartosc; i>0;i--){
  15. sem_wait(&Sem); sem_getvalue(&Sem, &Wartosc);
  16. printf("\nWartosc zmiennej semaforowej Sem=%d ", Wartosc);
  17. }
  18. printf("\n");
  19. sem_destroy(&Sem); //usuniecie semafora
  20. exit(0);
  21. }
  22.  
  23.  
  24.  
  25. #include <stdio.h>
  26. #include <semaphore.h>
  27.  
  28. int main(){
  29. sem_t Sem;
  30. int Wartosc;
  31. int i;
  32. Wartosc=10;
  33.  
  34. sem_init(&Sem, 0, Wartosc);
  35. sem_getvalue(&Sem, &Wartosc);
  36.  
  37. printf("\nWartosc zmiennej semaforowej Sem= %d ", Wartosc);
  38. for(i=Wartosc; i>0; i--){
  39. sem_post(&Sem); sem_getvalue(&Sem ,&Wartosc);
  40. printf("\nWartosc zmiennej semaforowej Sem=%d ", Wartosc);
  41. }
  42. printf("\n");
  43. sem_destroy(&Sem);
  44. exit(0);
  45. }
  46.  
  47.  
  48.  
  49. #include <stdio.h>
  50. #include <semaphore.h>
  51.  
  52. int main(){
  53. sem_t Sem;
  54. int Wartosc, result ,i;
  55. Wartosc = 10;
  56.  
  57. sem_init(&Sem, 0, Wartosc);
  58. sem_getvalue(&Sem, &Wartosc);
  59. printf("\nWartosc zmiennej semaforowej Sem=%d ",Wartosc);
  60. for(i=Wartosc; i>=-1; i--){
  61. result=sem_trywait(&Sem);
  62. sem_getvalue(&Sem, &Wartosc);
  63. if(result==0)
  64. printf("\nWartosc zmiennej semaforowej Sem= %d ", Wartosc);
  65. else
  66. printf("\nWartosc zmiennej semaforowej Sem=%d Watek zostanie zablkowany", Wartosc);
  67. }
  68. printf("\n"); sem_destroy(&Sem);
  69. exit(0);
  70. } //main
  71.  
  72.  
  73. #include <stdio.h>
  74. #include <semaphore.h>
  75.  
  76. void * Down();
  77. void * Up();
  78. static sem_t Sem;
  79. int main() {
  80. pthread_t Tid_Up, Tid_Down;
  81. sem_init(&Sem, 0, 5);
  82. pthread_create(&Tid_Down, NULL, Down, NULL);
  83. pthread_create(&Tid_Up, NULL, Up, NULL);
  84. pthread_join(Tid_Up, NULL);
  85. pthread_join(Tid_Down, NULL);
  86. printf("\n");
  87.  
  88. sem_destroy(&Sem);
  89. exit(0);
  90. }
  91.  
  92. void *Down(){
  93. int Wartosc;
  94. int i;
  95. int result;
  96. for(i=1; i<10;i++){
  97. sem_getvalue(&Sem,&Wartosc);
  98. printf("\nFUNKCJA DOWN: Sem=%d ", Wartosc);
  99. result=sem_trywait(&Sem);
  100. if(result==0){
  101. sem_wait(&Sem);
  102. }
  103. else {
  104. printf("wartosc zmiennej result %d --> watek ulegnie zawieszeniu", result);
  105. sem_wait(&Sem);
  106. }
  107. sleep(2);
  108. }
  109. }
  110.  
  111. void *Up(){
  112. int Wartosc;
  113. int i;
  114.  
  115. for(i=1;i<10;i++){
  116. sem_getvalue(&Sem, &Wartosc);
  117. printf("\nFUNKCJA Up:(przed operacja SEM_POS Sem=%d) ", Wartosc);
  118. sem_post(&Sem);
  119. sem_getvalue(&Sem,&Wartosc);
  120. printf("(po operacji SEM_POS: Sem=%d) ", Wartosc);
  121. sleep(4);
  122. }
  123. }
  124.  
  125.  
  126. #include <stdio.h>
  127. #include <semaphore.h>
  128.  
  129. void * Down();
  130. void * Up();
  131. static sem_t Sem;
  132. int main() {
  133. pthread_t Tid_Up, Tid_Down1, Tid_Down2;
  134. sem_init(&Sem, 0, 5);
  135. pthread_create(&Tid_Down1, NULL, Down, NULL);
  136. pthread_create(&Tid_Down2, NULL, Down, NULL);
  137. pthread_create(&Tid_Up, NULL, Up, NULL);
  138. pthread_join(Tid_Up, NULL);
  139. pthread_join(Tid_Down1, NULL);
  140. pthread_join(Tid_Down2, NULL);
  141. printf("\n");
  142.  
  143. sem_destroy(&Sem);
  144. exit(0);
  145. }
  146.  
  147. void *Down(){
  148. int Wartosc;
  149. int i;
  150. int result;
  151. for(i=1; i<10;i++){
  152. sem_getvalue(&Sem,&Wartosc);
  153. printf("\nFUNKCJA DOWN: Sem=%d ", Wartosc);
  154. result=sem_trywait(&Sem);
  155. if(result==0){
  156. sem_wait(&Sem);
  157. }
  158. else {
  159. printf("wartosc zmiennej result %d --> watek ulegnie zawieszeniu", result);
  160. sem_wait(&Sem);
  161. }
  162. sleep(2);
  163. }
  164. }
  165.  
  166. void *Up(){
  167. int Wartosc;
  168. int i;
  169.  
  170. for(i=1;i<10;i++){
  171. sem_getvalue(&Sem, &Wartosc);
  172. printf("\nFUNKCJA Up:(przed operacja SEM_POS Sem=%d) ", Wartosc);
  173. sem_post(&Sem);
  174. sem_getvalue(&Sem,&Wartosc);
  175. printf("(po operacji SEM_POS: Sem=%d) ", Wartosc);
  176. sleep(4);
  177. }
  178. }
  179.  
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement