Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- int Account[3];
- sem_t semaphore1;
- sem_t semaphore2;
- sem_t semaphore3;
- void *thread1(void *vargp){
- int i, internal_reg;
- /*synch with thread2, thread3, and thread 4 */
- for(i = 0; i < 10000; i++){
- sem_wait(&semaphore1);
- internal_reg = Account[0];
- internal_reg = internal_reg - 200;
- Account[0] = internal_reg;
- sem_post(&semaphore1);
- sem_wait(&semaphore1);
- internal_reg = Account[1];
- internal_reg = internal_reg + 200;
- Account[1] = internal_reg;
- sem_post(&semaphore1);
- if((i+1)% 1000 == 0){
- }
- }
- }
- int main(){
- Account[0] = 100000;
- Account[1] = 100000;
- Account[2] = 100000;
- sem_init(&semaphore1, 0, 1);
- sem_init(&semaphore2, 0, 1);
- sem_init(&semaphore3, 0, 1);
- pthread_t tid[4];
- pthread_create(&tid[0], NULL, thread1, NULL);
- pthread_create(&tid[1], NULL, thread2, NULL);
- pthread_create(&tid[2], NULL, thread3, NULL);
- pthread_create(&tid[3], NULL, thread4, NULL);
- pthread_join(&tid[1], NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement