document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //----------------------------------------------------------------------------
  2. // Title - Implement producer-consumer algorithm using multi-threading concept
  3. //----------------------------------------------------------------------------
  4.  
  5. ------------------------
  6. MAIN PROGRAM a10.c
  7. ------------------------
  8.  
  9. #define BUFFER_SIZE 10
  10. #include<stdlib.h>
  11. #include<stdio.h>
  12. #include<pthread.h>
  13. #include<semaphore.h>
  14. #define TRUE 1
  15.  
  16. pthread_mutex_t mutex;         //the mutex lock
  17. sem_t full,empty;              //semaphores
  18. int buffer[BUFFER_SIZE];
  19. int counter;                   //buffer counter
  20.  
  21. pthread_t tid;                 //threadid
  22.  
  23. void *consumer(void *param);
  24. void *producer(void *param);   //the producer thread
  25. void insert_item(int);
  26. int remove_item();
  27.  
  28. void initialize()
  29. {
  30.     pthread_mutex_init(&mutex,NULL);             //create the mutex lock
  31.     sem_init(&full,1,0);                         //initialize full semaphore to 0
  32.     sem_init(&empty,1,BUFFER_SIZE);              //initialize empty semaphore to BUFFER_SIZE
  33.     counter=0;
  34.     }
  35.  
  36. /*Produce Thread*/
  37.  
  38. void *producer(void *param)
  39. {
  40.     int item;
  41.     int waittime;
  42.     waittime=rand()%5;
  43.     sleep(waittime);
  44.     item=rand()%10;
  45.     sem_wait(&empty);
  46.     pthread_mutex_lock(&mutex);
  47.     printf(" Producer produced : %d \\n",item);
  48.     insert_item(item);
  49.     pthread_mutex_unlock(&mutex);
  50.     sem_post(&full);
  51.     }  
  52.  
  53. /*Consumer thread*/
  54.  
  55. void *consumer(void *param)
  56. {
  57.     int item;
  58.     int waittime;                        //next item is consumed after waittime
  59.     waittime=rand()%3;
  60.     sleep(waittime);
  61.     sem_wait(&full);                     //wait if the buffer is full
  62.     pthread_mutex_lock(&mutex);          //acquire the buffer
  63.     item=remove_item();
  64.     printf(" Consumer consumed : %d \\n ",item);
  65.     pthread_mutex_unlock(&mutex);        // release the buffer
  66.     sem_post(&empty);                    //signal empty
  67.     }
  68.  
  69. //add item to buffer
  70.  
  71. void insert_item(int item)
  72. {
  73.     buffer[counter++]=item;
  74.     }
  75.  
  76. //remove an item from the buffer
  77.  
  78. int remove_item()
  79. {
  80.     return(buffer[--counter]);
  81.     }
  82.  
  83. int main()
  84. {
  85.     int n1;
  86.     int n2;
  87.     int i;
  88.     printf(" Enter No. of Producers : \\n");
  89.     scanf("%d",&n1);  
  90.     printf(" Enter No. of Consumers : \\n");
  91.     scanf("%d",&n2);
  92.    
  93.     initialize();
  94.  
  95. //create producers threads
  96.  
  97.     for(i=0;i<n1;i++)
  98.     pthread_create(&tid,NULL,producer,NULL);
  99.    
  100. //create consumer threads
  101.  
  102.     for(i=0;i<n2;i++)
  103.     pthread_create(&tid,NULL,consumer,NULL);
  104.     sleep(50);
  105.     exit(0);
  106.     }
  107.  
  108.  
  109. ------------------------
  110. END OF THE PROGRAM
  111. ------------------------
  112. /*
  113. ------------------------
  114.     OUTPUT
  115. ------------------------
  116.  
  117. gescoe@gescoe-Vostro-230:~/Desktop$ gcc a10.c -lpthread
  118.  
  119. gescoe@gescoe-Vostro-230:~/Desktop$ ./a.out
  120.  
  121. Enter No. of Producers :
  122. 3
  123. Enter No. of Consumers:
  124. 3
  125.  
  126. Producer Produced 3
  127. Consumer Consumed 3
  128. Producer Produced 6
  129. Consumer Consumed 6
  130. Producer Produced 9
  131. Consumer Consumed 9
  132.  
  133. ------------------------
  134. EXIT
  135. ------------------------
  136. */
');