Advertisement
veskopos

Starcraft.c

Apr 17th, 2015
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. //--------------------------------------------
  2. // NAME: Veselin Dechev
  3. // CLASS: XIA
  4. // NUMBER: 2
  5. // PROBLEM: #3
  6. // FILE NAME: Starcraft.c (unix file name)
  7. // FILE PURPOSE:
  8. // Synchronising threads with mutex.
  9. // Example with workers and one mine like THE GAME Starcraft.
  10. // ...
  11. //--------------------------------------------
  12.  
  13. #include <stdio.h>
  14. #include <pthread.h>
  15. #include <stdlib.h>
  16.  
  17. int NUM_THREADS; //Number of threads
  18.  
  19. pthread_mutex_t lock; //Mutex
  20. int gold; //Gold in the mine
  21. int* workers_gold; //gold of every worker
  22. int startminerals = 0;
  23. //--------------------------------------------
  24. // FUNCTION: GetGold
  25. // Geting the gold from the mine and allows only one worker to work.
  26. // PARAMETERS:
  27. // ID of the worker
  28. //--------------------------------------------
  29.  
  30. void* GetGold(void* arg){    //Geting the gold of the mine
  31.     size_t thread_num = (size_t)arg; //get id of the thread
  32.     while(gold>0){ //when there's a gold in mine , loop works
  33.         pthread_mutex_trylock(&lock); //lock mutex
  34.         printf("SCV %zd is mining\n", thread_num);
  35.         if(gold>7){
  36.             gold-=8; //geting the gold
  37.             startminerals = startminerals + 8; //incrments the gold in every worker's inventory
  38.        
  39.         }
  40.  
  41.         else{ //if the gold is less than 8 , the worker takes the gold wich lefts.
  42.             startminerals = startminerals + gold;
  43.             gold-=gold;
  44.         }
  45.         printf("SCV %zd is transporting minerals\n", thread_num);
  46.             printf( "SCV %zd delivered minerals to 'Command Center 1' %d\n", thread_num, startminerals);
  47.         printf("Remaining gold: %d\n",gold);
  48.         pthread_mutex_unlock(&lock); //unlock the mutex
  49.         sleep(1); //simulating when the worker works.
  50.     }
  51. }
  52.  
  53. int main(int argc, char* argv[]){ //main
  54.  
  55.     if(argc<2){ //if there's no arguments
  56.         gold = 5000;
  57.         NUM_THREADS = 5;
  58.     }
  59.     else{ //reading from argument line
  60.         gold = atoi(argv[1]);
  61.         NUM_THREADS = atoi(argv[2]);
  62.     }
  63.  
  64.     pthread_t threads[NUM_THREADS]; //creating array from threads
  65.     pthread_mutex_init(&lock, NULL);  //inits the mutex
  66.    
  67.     size_t i; //counter
  68.    
  69.     workers_gold = (int*)malloc(NUM_THREADS*sizeof(int)); // allocate memorry for every worker's inventory
  70.     for (i = 0; i< NUM_THREADS; i++){
  71.                 workers_gold[i]=0; //every worker's inventory is set to 0;
  72.     }
  73.    
  74.     for (i = 1; i <= NUM_THREADS; i++) {
  75.         int error = pthread_create(&threads[i], NULL, GetGold, (void*)i); //creating the threads and checks for errors.
  76.         if (error != 0) {
  77.             fprintf(stderr, "Error creating thread %zd: error: %d\n", i, error);
  78.         }
  79.   }
  80.  
  81.   for (i = 0; i < NUM_THREADS; i++) {
  82.         int error = pthread_join(threads[i], NULL); //joining the threads.
  83.         if (error != 0) {
  84.             fprintf(stderr, "Error joining thread %zd: error: %d\n", i, error);
  85.         }
  86.   }
  87.    
  88.     pthread_mutex_destroy(&lock); //destroys mutex
  89.   pthread_exit(NULL); //exit from the threads
  90.  
  91.     return 0; //epic crash!!!
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement