Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define NUM_WORKERS 5
- #define MAX_MINERALS 4096
- #define MAX_POPULATION 100
- #define VICTORY 20
- #define WARRIOR_PRICE 25
- #define WORKER_PRICE 25
- unsigned int minerals = 0;
- unsigned short warriors = 0;
- unsigned short workers = NUM_WORKERS;
- unsigned short status = 1;
- pthread_mutex_t base_mutex;
- /*
- void *searching(unsigned short N);
- void transporting(unsigned short N);
- void unloading(unsigned short N);
- */
- //void *worker(void *N);
- void *worker (void *N)
- {
- int i;
- size_t n = (size_t) N;
- for (;;)
- {
- if (status == 0)
- {
- break;
- }
- if (warriors >= VICTORY)
- {
- break;
- }
- // Searching
- printf("Worker %zu is searching\n", n);
- sleep(2);
- // Transporting
- printf("Worker %zu is transporting\n", n);
- sleep(2);
- // Unloading
- if (minerals + (workers-NUM_WORKERS)*WORKER_PRICE + warriors*WARRIOR_PRICE >= MAX_MINERALS)
- {
- status = 0;
- if (N == 0)
- printf("There is no more minerals on the map!\n");
- break;
- }
- pthread_mutex_lock(&base_mutex);
- minerals +=4;
- printf("Worker %zu unloaded resources to Base station\n", n);
- pthread_mutex_unlock(&base_mutex);
- }
- }
- int main()
- {
- size_t i;
- pthread_t threads[NUM_WORKERS];
- pthread_mutex_init(&base_mutex, NULL);
- for (i = 0; i < NUM_WORKERS; i++)
- {
- int error = pthread_create(&threads[i], NULL, worker, (void*)i);
- if (error != 0)
- {
- fprintf(stderr, "Error creating thread %zd: error: %d\n", i, error);
- }
- }
- for (;;)
- {
- if(warriors>=VICTORY)
- {
- break;
- }
- char c;
- scanf("%c", &c);
- if(workers+warriors<MAX_POPULATION)
- {
- if(c == 'w')
- {
- if (minerals >= WARRIOR_PRICE)
- {
- pthread_mutex_lock(&base_mutex);
- printf("Warrior is being trained\n");
- minerals -= WARRIOR_PRICE;
- pthread_mutex_unlock(&base_mutex);
- sleep(2);
- warriors++;
- printf("Warrior is ready for duty\n");
- }
- else
- printf("You do not have enough minerals!\n");
- }
- else if(c == 'c')
- {
- if (minerals>=WORKER_PRICE)
- {
- pthread_mutex_lock(&base_mutex);
- printf("Worker is being trained\n");
- minerals -= WORKER_PRICE;
- pthread_mutex_unlock(&base_mutex);
- sleep(1);
- workers++;
- printf("Worker has been spowned\n");
- int error = pthread_create(&threads[workers-1], NULL, worker, (void*)workers-1);
- if (error != 0)
- {
- fprintf(stderr, "Error creating thread %d: error: %d\n", workers-1, error);
- }
- }
- else
- printf("You do not have enough minerals!\n");
- }
- else if (c != '\n')
- {
- printf("Wrong command!\n");
- }
- }
- else
- {
- break;
- }
- }
- for (i = 0; i < workers; i++)
- {
- int error = pthread_join(threads[i], NULL);
- if (error != 0)
- {
- fprintf(stderr, "Error joining thread %zd: error: %d\n", i, error);
- }
- }
- if (warriors>=VICTORY)
- {
- printf("\t===Win===\n");
- printf("All minerals on the map: %d\n", MAX_MINERALS);
- printf("Uncollected minerals: %d\n", MAX_MINERALS-(minerals+warriors*WARRIOR_PRICE+(workers-NUM_WORKERS)*WORKER_PRICE));
- printf("Collected minerals: %d\n", minerals+warriors*WARRIOR_PRICE+(workers-NUM_WORKERS)*WORKER_PRICE);
- }
- else if(workers+warriors>=MAX_POPULATION)
- {
- printf("\t===GAME OVER!===\n");
- printf("You reached population limit!\n");
- printf("All minerals on the map: %d\n", MAX_MINERALS);
- printf("Uncollected minerals: %d\n", MAX_MINERALS-(minerals+warriors*WARRIOR_PRICE+(workers-NUM_WORKERS)*WORKER_PRICE));
- printf("Collected minerals: %d\n", minerals+warriors*WARRIOR_PRICE+(workers-NUM_WORKERS)*WORKER_PRICE);
- }
- /*
- if (minerals >= MAX_MINERALS)
- {
- printf("\t===GAME OVER!===\n");
- printf("There is no more minerals on the map!\n");
- }
- */
- pthread_mutex_destroy(&base_mutex);
- pthread_exit(NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement