Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- void *cons1_thread_fun(void *arg);
- void *cons2_thread_fun(void *arg);
- void *cons3_thread_fun(void *arg);
- pthread_mutex_t s;
- pthread_mutex_t s1;
- pthread_cond_t zw;
- pthread_cond_t zw1;
- #define L_KONS 3
- int data = 1, i, prod_unlock=0, cons_wait[L_KONS];
- int main()
- {
- int res;
- pthread_t a_thread[3];
- void *(*funs[3]) (void *arg);
- funs[0] = *cons1_thread_fun;
- funs[1] = *cons2_thread_fun;
- funs[2] = *cons3_thread_fun;
- for(i=0; i<L_KONS; i++)
- {
- cons_wait[i] = 1;
- }
- for (i=0; i<L_KONS; i++) {
- res = pthread_create(&a_thread[i], NULL, funs[i], (void*) i);
- if (res != 0) {
- perror("Thread consument creation failed");
- exit(EXIT_FAILURE);
- }
- }
- void *thread_result;
- res = pthread_mutex_init(&s, NULL);
- if (res != 0) {
- perror("Mutex s initialization failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_mutex_init(&s1, NULL);
- if (res != 0) {
- perror("Mutex s1 initialization failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_cond_init (&zw, NULL);
- if (res != 0) {
- perror("Conditional variable zw initialization failed");
- exit(EXIT_FAILURE);
- }
- res = pthread_cond_init (&zw1, NULL);
- if (res != 0) {
- perror("Conditional variable zw1 initialization failed");
- exit(EXIT_FAILURE);
- }
- while(1)
- {
- pthread_mutex_lock(&s);
- data++;
- printf("Produced data: %d\n", data);
- sleep(1);
- for(i=0; i< L_KONS; i++)
- cons_wait[i] = 0;
- pthread_cond_broadcast(&zw);
- pthread_mutex_unlock(&s);
- pthread_mutex_lock(&s1);
- while(prod_unlock < L_KONS)
- pthread_cond_wait(&zw1, &s1);
- prod_unlock = 0;
- pthread_mutex_unlock(&s1);
- }
- for (i=0; i < L_KONS; i++) {
- pthread_join(a_thread[i], NULL);
- }
- pthread_cond_destroy(&zw);
- pthread_cond_destroy(&zw1);
- pthread_mutex_destroy(&s);
- pthread_mutex_destroy(&s1);
- exit(EXIT_SUCCESS);
- }
- void *cons1_thread_fun(void *arg)
- {
- int nr_kons = 0;
- while(1)
- {
- pthread_mutex_lock(&s);
- while(cons_wait[nr_kons] == 1)
- pthread_cond_wait(&zw, &s);
- printf("Current value of data in thread 1: %d\n", data);
- //sleep(2);
- cons_wait[nr_kons] = 1;
- pthread_mutex_unlock(&s);
- pthread_mutex_lock(&s1);
- prod_unlock++;
- if(prod_unlock == L_KONS)
- pthread_cond_signal(&zw1);
- pthread_mutex_unlock(&s1);
- }
- pthread_exit(NULL);
- }
- void *cons2_thread_fun(void *arg)
- {
- int nr_kons = 1;
- while(1)
- {
- pthread_mutex_lock(&s);
- while(cons_wait[nr_kons] == 1)
- pthread_cond_wait(&zw, &s);
- printf("Current value of data in thread 2: %d\n", data);
- //sleep(3);
- cons_wait[nr_kons] = 1;
- pthread_mutex_unlock(&s);
- pthread_mutex_lock(&s1);
- prod_unlock++;
- if(prod_unlock == L_KONS)
- pthread_cond_signal(&zw1);
- pthread_mutex_unlock(&s1);
- }
- pthread_exit(NULL);
- }
- void *cons3_thread_fun(void *arg)
- {
- int nr_kons = 2;
- while(1)
- {
- pthread_mutex_lock(&s);
- while(cons_wait[nr_kons] == 1)
- pthread_cond_wait(&zw, &s);
- printf("Current value of data in thread 3: %d\n", data);
- //sleep(1);
- cons_wait[nr_kons] = 1;
- pthread_mutex_unlock(&s);
- pthread_mutex_lock(&s1);
- prod_unlock++;
- if(prod_unlock == L_KONS)
- pthread_cond_signal(&zw1);
- pthread_mutex_unlock(&s1);
- }
- pthread_exit(NULL);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement