Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. int Account[3];
  6. sem_t semaphore1;
  7. sem_t semaphore2;
  8. sem_t semaphore3;
  9.  
  10.  
  11. void *thread1(void *vargp){
  12. int i, internal_reg;
  13.  
  14. /*synch with thread2, thread3, and thread 4 */
  15.  
  16. for(i = 0; i < 10000; i++){
  17.  
  18. sem_wait(&semaphore1);
  19. internal_reg = Account[0];
  20. internal_reg = internal_reg - 200;
  21. Account[0] = internal_reg;
  22. sem_post(&semaphore1);
  23.  
  24. sem_wait(&semaphore1);
  25. internal_reg = Account[1];
  26. internal_reg = internal_reg + 200;
  27. Account[1] = internal_reg;
  28. sem_post(&semaphore1);
  29.  
  30. if((i+1)% 1000 == 0){
  31.  
  32. }
  33. }
  34. }
  35.  
  36.  
  37.  
  38. int main(){
  39.  
  40. Account[0] = 100000;
  41. Account[1] = 100000;
  42. Account[2] = 100000;
  43.  
  44. sem_init(&semaphore1, 0, 1);
  45. sem_init(&semaphore2, 0, 1);
  46. sem_init(&semaphore3, 0, 1);
  47.  
  48.  
  49. pthread_t tid[4];
  50. pthread_create(&tid[0], NULL, thread1, NULL);
  51. pthread_create(&tid[1], NULL, thread2, NULL);
  52. pthread_create(&tid[2], NULL, thread3, NULL);
  53. pthread_create(&tid[3], NULL, thread4, NULL);
  54. pthread_join(&tid[1], NULL);
  55.  
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement