Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <math.h>
- #include <stdlib.h>
- pthread_mutex_t m[6];
- int a, b, wynik;
- int typ;
- char typy[4] = {'+', '-', '/', '*'};
- void * dodawanie(void * arg)
- {
- while(1)
- {
- pthread_mutex_lock(&m[2]);
- typ = 0;
- wynik = a + b;
- pthread_mutex_unlock(&m[1]);
- }
- }
- void * odejmowanie(void * arg)
- {
- while(1)
- {
- pthread_mutex_lock(&m[3]);
- typ = 1;
- wynik = a - b;
- pthread_mutex_unlock(&m[1]);
- }
- }
- void * dzielenie(void * arg)
- {
- while(1)
- {
- pthread_mutex_lock(&m[4]);
- typ = 2;
- wynik = a / b;
- pthread_mutex_unlock(&m[1]);
- }
- }
- void * mnozenie(void * arg)
- {
- while(1)
- {
- pthread_mutex_lock(&m[5]);
- typ = 3;
- wynik = a * b;
- pthread_mutex_unlock(&m[1]);
- }
- }
- void * wyswietl(void * arg)
- {
- while(1)
- {
- pthread_mutex_lock(&m[1]);
- printf("%d %c %d = %d\n", a, typy[typ], b, wynik);
- if(typ < 3) pthread_mutex_unlock(&m[typ+3]);
- else pthread_mutex_unlock(&m[0]);
- }
- }
- int main(int argc, char ** argv)
- {
- int i;
- pthread_t threads[5];
- for(int i = 1; i < 6; i++)
- pthread_mutex_lock(&m[i]);
- pthread_create(&(threads[0]), NULL, dodawanie, NULL);
- pthread_create(&(threads[1]), NULL, odejmowanie, NULL);
- pthread_create(&(threads[2]), NULL, dzielenie, NULL);
- pthread_create(&(threads[3]), NULL, mnozenie, NULL);
- pthread_create(&(threads[4]), NULL, wyswietl, NULL);
- int n, first = 1;
- while(1)
- {
- pthread_mutex_lock(&m[0]);
- if(first){scanf("%d", &b); first = 0;}
- a = b;
- if((n = scanf("%d", &b)) < 1) break;
- pthread_mutex_unlock(&m[2]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement