Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pthread.h>
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdio.h>
- unsigned int truskawkaNaKrzaczku = 0;
- unsigned int zebrana = 0;
- unsigned int truskawkaWKoszyczku = 0;
- pthread_mutex_t mutex;
- void lock(void){
- pthread_mutex_lock(&mutex);
- }
- void unlock(void){
- pthread_mutex_unlock(&mutex);
- }
- void* zbieracz(void* arg){
- while (1)
- {
- lock();
- truskawkaNaKrzaczku = rand() % 10;
- zebrana = 1;
- unlock();
- printf("Zbieracz\n");
- usleep(800*1000);
- }
- }
- void* maciek(void* arg){
- while(1)
- {
- unsigned int zjadlem = 0;
- lock();
- if(truskawkaWKoszyczku >= 5){
- truskawkaWKoszyczku -= 5;
- zjadlem = 1;
- }
- unlock();
- printf("Maciek, zjadlem? %d\n", zjadlem);
- usleep(1500*1000);
- }
- }
- void* inspektor(void* arg){
- while(1)
- {
- unsigned int fajnaTruskawka = 0;
- lock();
- if(truskawkaNaKrzaczku > 6 && zebrana == 1){
- ++truskawkaWKoszyczku;
- fajnaTruskawka = 1;
- }
- zebrana = 0;
- unlock();
- printf("Inspektor, truskawka byla fajna? %d\n", fajnaTruskawka);
- usleep(300*1000);
- }
- }
- int main(void){
- pthread_t threadMaciek;
- pthread_t threadZbieracz;
- pthread_t threadInspektor;
- pthread_create(&threadMaciek, NULL, maciek, NULL);
- pthread_create(&threadZbieracz, NULL, zbieracz, NULL);
- pthread_create(&threadInspektor, NULL, inspektor, NULL);
- pthread_join(threadMaciek, NULL);
- pthread_join(threadZbieracz, NULL);
- pthread_join(threadInspektor, NULL);
- // maciek(&mutex);
- // zbieracz(&mutex);
- // inspektor(&mutex);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement