Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <pthread.h>
- int belka = 100;
- int deska = 0;
- int licznik=2000000;
- pthread_mutex_t sygnalizacja = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t mutBelka = PTHREAD_MUTEX_INITIALIZER;
- pthread_mutex_t mutDeska = PTHREAD_MUTEX_INITIALIZER;
- void* wykonaj1(void *a)
- {
- int i;
- pthread_mutex_lock(&sygnalizacja);
- for(i=0;i<1000000;i++)
- {
- licznik = licznik -1;
- }
- pthread_mutex_unlock(&sygnalizacja);
- return NULL;
- }
- void error(char *msg)
- {
- fprintf(stderr,"%s:%s\n",msg,strerror(errno));
- exit(1);
- }
- void* drwal()
- {
- while(1)
- {
- pthread_mutex_lock(&mutBelka);
- belka+=10;
- pthread_mutex_unlock(&mutBelka);
- //usleep(2000000);
- printf("Wyprodukowałem %i belek.\n", belka);
- usleep(2000000);
- }
- }
- void* stolarz(int liczba)
- {
- while(1)
- {
- pthread_mutex_lock(&mutBelka);
- belka-=liczba;
- pthread_mutex_unlock(&mutBelka);
- pthread_mutex_lock(&mutDeska);
- deska++;
- pthread_mutex_unlock(&mutDeska);
- printf("Zrobiłem deskę z %i belek.\n", liczba);
- usleep(1500*1000);
- }
- }
- int main(){
- pthread_t t0;
- pthread_t t1;
- pthread_t d;
- pthread_t s1, s2;
- pthread_t s3;
- pthread_create(&d,NULL,drwal,NULL);
- //pthread_create(&s1,NULL,stolarz(4),NULL);
- pthread_create(&s2,NULL,stolarz(2),NULL);
- pthread_create(&s3,NULL,stolarz(7),NULL);
- void* wynik;
- pthread_join(d,&wynik);
- //pthread_join(s1,&wynik);
- pthread_join(s2,&wynik);
- pthread_join(s3,&wynik);
- if(pthread_create(&t0,NULL,wykonaj1,NULL)==-1)
- error("Nie mozna utworzyc watku t0");
- if(pthread_create(&t1,NULL,wykonaj1,NULL)==-1)
- error("Nie mozna utworzyc watku t1");
- void* result;
- if(pthread_join(t0,&result)==-1)
- error("Blad oczekiwania na zakonczenie watku t0");
- if(pthread_join(t1,&result)==-1)
- error("Blad oczekiwania na zakonczenie watku t0");
- printf("licznk na koniec ma wartosc = %i \n", licznik);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement