Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------
- // NAME: Veselin Dechev
- // CLASS: XIA
- // NUMBER: 2
- // PROBLEM: #3
- // FILE NAME: Starcraft.c (unix file name)
- // FILE PURPOSE:
- // Synchronising threads with mutex.
- // Example with workers and one mine like THE GAME Starcraft.
- // ...
- //--------------------------------------------
- #include <stdio.h>
- #include <pthread.h>
- #include <stdlib.h>
- int NUM_THREADS; //Number of threads
- pthread_mutex_t lock; //Mutex
- int minerals; //minerals in the mine
- int startminerals = 0; //minerals in the Command Center
- int soldiers = 0; //soldiers
- //--------------------------------------------
- // FUNCTION: Getminerals
- // Geting the minerals from the mine and allows only one worker to work.
- // PARAMETERS:
- // ID of the worker
- //--------------------------------------------
- void* GetMinerals(void* arg){ //Geting the minerals of the mine
- size_t thread_num = (size_t)arg; //get id of the thread
- while(minerals>0){ //when there's a minerals in mine , loop works
- if(soldiers>=20) break;
- pthread_mutex_trylock(&lock); //lock mutex
- printf("SCV %zd is mining\n", thread_num);
- if(minerals>7){
- minerals-=8; //geting the minerals
- startminerals = startminerals + 8; //incrments the minerals in Command Center
- }
- else{ //if the minerals is less than 8, the worker takes the minerals which lefts.
- startminerals = startminerals + minerals;
- minerals-=minerals;
- }
- printf("SCV %zd is transporting minerals\n", thread_num);
- printf( "SCV %zd delivered minerals to 'Command Center 1' %d\n", thread_num, startminerals);
- printf("Remaining minerals: %d\n",minerals);
- pthread_mutex_unlock(&lock); //unlock the mutex
- sleep(2); //simulating when the worker works.
- }
- }
- int main(int argc, char* argv[]){ //main
- if(argc<2){ //if there's no arguments
- minerals = 5000;
- NUM_THREADS = 5;
- }
- else{ //reading from argument line
- minerals = atoi(argv[1]);
- NUM_THREADS = atoi(argv[2]);
- }
- pthread_t threads[NUM_THREADS]; //creating array from threads
- pthread_mutex_init(&lock, NULL); //inits the mutex
- size_t i; //counter
- for (i = 1; i <= NUM_THREADS; i++) {
- int error = pthread_create(&threads[i], NULL, GetMinerals, (void*)i); //creating the threads and checks for errors.
- if (error != 0) {
- fprintf(stderr, "Error creating thread %zd: error: %d\n", i, error);
- }
- }
- while(soldiers<20){
- char c; //Reading symbol from keyboard
- scanf("%c",&c);
- if(c=='m'){
- if(startminerals>50){ soldiers++; startminerals-=50;
- printf("You wanna piece of me, boy? %d\n", soldiers);
- }
- else printf("Not enough minerals.\n");
- }
- }
- for (i = 1; i < NUM_THREADS; i++) {
- int error = pthread_join(threads[i], NULL); //joining the threads.
- if (error != 0) {
- fprintf(stderr, "Error joining thread %zd: error: %d\n", i, error);
- }
- }
- pthread_mutex_destroy(&lock); //destroys mutex
- pthread_exit(NULL); //exit from the threads
- return 0; //exit
- }
- //creating array from threads
- pthread_mutex_init(&lock, NULL
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement