Advertisement
veskopos

starcraft1.c

Apr 27th, 2015
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.00 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 minerals; //minerals in the mine
  21. int startminerals = 0; //minerals in the Command Center
  22. int soldiers = 0; //soldiers
  23. //--------------------------------------------
  24. // FUNCTION: Getminerals
  25. // Geting the minerals from the mine and allows only one worker to work.
  26. // PARAMETERS:
  27. // ID of the worker
  28. //--------------------------------------------
  29.  
  30. void* GetMinerals(void* arg){    //Geting the minerals of the mine
  31.     size_t thread_num = (size_t)arg; //get id of the thread
  32.     while(minerals>0){ //when there's a minerals in mine , loop works
  33.         if(soldiers>=20) break;
  34.         pthread_mutex_trylock(&lock); //lock mutex
  35.         printf("SCV %zd is mining\n", thread_num);
  36.         if(minerals>7){
  37.             minerals-=8; //geting the minerals
  38.             startminerals = startminerals + 8; //incrments the minerals in Command Center
  39.        
  40.         }
  41.  
  42.         else{ //if the minerals is less than 8, the worker takes the minerals which lefts.
  43.             startminerals = startminerals + minerals;
  44.             minerals-=minerals;
  45.         }
  46.         printf("SCV %zd is transporting minerals\n", thread_num);
  47.             printf( "SCV %zd delivered minerals to 'Command Center 1' %d\n", thread_num, startminerals);
  48.         printf("Remaining minerals: %d\n",minerals);       
  49.         pthread_mutex_unlock(&lock); //unlock the mutex
  50.    
  51.         sleep(2); //simulating when the worker works.          
  52.     }
  53. }
  54.  
  55. int main(int argc, char* argv[]){ //main
  56.  
  57.     if(argc<2){ //if there's no arguments
  58.         minerals = 5000;
  59.         NUM_THREADS = 5;
  60.     }
  61.     else{ //reading from argument line
  62.         minerals = atoi(argv[1]);
  63.         NUM_THREADS = atoi(argv[2]);
  64.     }
  65.  
  66.     pthread_t threads[NUM_THREADS]; //creating array from threads
  67.     pthread_mutex_init(&lock, NULL);  //inits the mutex
  68.    
  69.     size_t i; //counter
  70.    
  71.     for (i = 1; i <= NUM_THREADS; i++) {
  72.         int error = pthread_create(&threads[i], NULL, GetMinerals, (void*)i); //creating the threads and checks for errors.
  73.         if (error != 0) {
  74.             fprintf(stderr, "Error creating thread %zd: error: %d\n", i, error);
  75.         }
  76.   }
  77.     while(soldiers<20){
  78.         char c; //Reading symbol from keyboard
  79.         scanf("%c",&c);
  80.         if(c=='m'){
  81.             if(startminerals>50){ soldiers++; startminerals-=50;
  82.             printf("You wanna piece of me, boy? %d\n", soldiers);
  83.             }
  84.     else printf("Not enough minerals.\n");
  85.             }
  86.   }
  87.   for (i = 1; i < NUM_THREADS; i++) {
  88.         int error = pthread_join(threads[i], NULL); //joining the threads.
  89.         if (error != 0) {
  90.             fprintf(stderr, "Error joining thread %zd: error: %d\n", i, error);
  91.         }
  92.   }
  93.    
  94.                    
  95.     pthread_mutex_destroy(&lock); //destroys mutex
  96.   pthread_exit(NULL); //exit from the threads
  97.  
  98.  
  99.     return 0; //exit
  100. }
  101.  //creating array from threads
  102.     pthread_mutex_init(&lock, NULL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement