Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Chris Osborn
- Class: CSE2431
- Assignment: Lab4
- Compilation Command: gcc PartA.c -o A -lpthread -lrt
- */
- #include <pthread.h>
- #include <semaphore.h>
- #include <stdio.h>
- #include <stdlib.h>
- // Thread routines
- void *Thread1();
- void *Thread2();
- void *Thread3();
- void *Thread4();
- pthread_t tid[4]; // array of thread IDs
- sem_t semaphore1; // sem1
- sem_t semaphore2; // sem2
- sem_t semaphore3; // sem3
- sem_t semaphore4; // sem4
- // Only shared variable
- int Account[3];
- int main()
- {
- int i;
- // Initialize accounts
- Account[0] = 100000;
- Account[1] = 100000;
- Account[2] = 100000;
- // Initialize semaphores to 1
- i = sem_init(&semaphore1, 0, 1);
- if(i < 0){exit;}
- i = sem_init(&semaphore2, 0, 1);
- if(i < 0){exit;}
- i = sem_init(&semaphore3, 0, 1);
- if(i < 0){exit;}
- i = sem_init(&semaphore4, 0, 1);
- if(i < 0){exit;}
- // Create threads 1, 2, & 3
- i = pthread_create(&tid[0], NULL, Thread1, NULL);
- if(i < 0){exit;}
- i = pthread_create(&tid[1], NULL, Thread2, NULL);
- if(i < 0){exit;}
- i = pthread_create(&tid[2], NULL, Thread3, NULL);
- if(i < 0){exit;}
- // Wait for Thread2 to finish, then exit
- i = pthread_join(tid[1], NULL);
- if(i < 0){exit;}
- exit;
- }
- void *Thread1()
- {
- int i, internal_reg;
- // Create Thread4
- i = pthread_create(&tid[3], NULL, Thread4, NULL);
- if(i < 0){exit;}
- /* here sync with Thread2, Thread3, and Thread4 (4 way) */
- sem_post(&semaphore2);
- sem_post(&semaphore3);
- sem_post(&semaphore4);
- sem_wait(&semaphore1);
- sem_wait(&semaphore1);
- sem_wait(&semaphore1);
- for (i = 0; i < 10000; i++)
- {
- sem_wait(&semaphore1);
- internal_reg = Account[0];
- internal_reg = internal_reg - 200;
- Account[0] = internal_reg;
- /* moving $200 from Account[0] to Account[1] */
- internal_reg = Account [1];
- internal_reg = internal_reg + 200;
- Account[1] = internal_reg;
- sem_post(&semaphore1);
- /* Prints contents of each account and their sum after each 1000 iterations*/
- if (i % 1000 == 0 && i != 0){
- printf("Thread1 #%d - Acc0: %d, Acc1: %d, Acc2: %d, Sum: %d\n", i, Account[0],Account[1],Account[2],Account[0]+Account[1]+Account[2]);
- }
- // Sleep every 3000 iterations
- if(i % 3000 == 0 && i != 0)
- {usleep(200000);}
- }
- sem_post(&semaphore4);
- pthread_exit(NULL);
- }
- void *Thread2()
- {
- int i, internal_reg;
- /* here sync with Thread1, Thread3, and Thread4 (4 way) */
- sem_post(&semaphore1);
- sem_post(&semaphore3);
- sem_post(&semaphore4);
- sem_wait(&semaphore2);
- sem_wait(&semaphore2);
- sem_wait(&semaphore2);
- for (i = 0; i < 10000; i++)
- {
- sem_wait(&semaphore1);
- internal_reg = Account[1];
- internal_reg = internal_reg - 200;
- Account[1] = internal_reg;
- /* moving $200 from Account[1] to Account[2] */
- internal_reg = Account [2];
- internal_reg = internal_reg + 200;
- Account[2] = internal_reg;
- sem_post(&semaphore1);
- /* Prints contents of each account and their sum after each 1000 iterations*/
- if (i % 1000 == 0 && i != 0){
- printf("Thread2 #%d - Acc0: %d, Acc1: %d, Acc2: %d, Sum: %d\n", i, Account[0],Account[1],Account[2],Account[0]+Account[1]+Account[2]);
- }
- // Sleep after 2000, 4000, 6000, and 9000 iterations
- if(i == 2000 || i == 4000 || i == 6000 || i == 9000)
- {usleep(200000);}
- }
- sem_post(&semaphore4);
- pthread_exit(NULL);
- }
- void *Thread3()
- {
- int i, internal_reg;
- /* here sync with Thread1, Thread2, and Thread4 (4 way) */
- sem_post(&semaphore1);
- sem_post(&semaphore2);
- sem_post(&semaphore4);
- sem_wait(&semaphore3);
- sem_wait(&semaphore3);
- sem_wait(&semaphore3);
- for (i = 0; i < 10000; i++)
- {
- sem_wait(&semaphore1);
- internal_reg = Account[2];
- internal_reg = internal_reg - 200;
- Account[2] = internal_reg;
- /* moving $200 from Account[2] to Account[0] */
- internal_reg = Account [0];
- internal_reg = internal_reg + 200;
- Account[0] = internal_reg;
- sem_post(&semaphore1);
- /* Prints contents of each account and their sum after each 1000 iterations*/
- if (i % 1000 == 0 && i != 0){
- printf("Thread3 #%d - Acc0: %d, Acc1: %d, Acc2: %d, Sum: %d\n", i, Account[0],Account[1],Account[2],Account[0]+Account[1]+Account[2]);
- }
- // Sleep after 2000 and 7500 iterations
- if(i == 2000 || i == 7500)
- {usleep(300000);}
- }
- sem_post(&semaphore4);
- pthread_exit(NULL);
- }
- void *Thread4()
- {
- int i, count = 0;
- /* here sync with Thread1, Thread2, and Thread3 (4 way) */
- sem_post(&semaphore1);
- sem_post(&semaphore2);
- sem_post(&semaphore3);
- sem_wait(&semaphore4);
- sem_wait(&semaphore4);
- sem_wait(&semaphore4);
- for(i = 0; i < 50000; i++)
- {
- if(Account[0]+Account[1]+Account[2] != 300000)
- {count++;}
- if(i % 10000 == 0 && i != 0)
- {usleep(50000);}
- }
- // 4 process 1 way sync to ensure Thread4 prints and exits last
- sem_wait(&semaphore4);
- sem_wait(&semaphore4);
- sem_wait(&semaphore4);
- printf("The accounts did not add up to $300,000 %d times out of 50,000.\n", count);
- exit;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement