Advertisement
DanFloyd

Producer&Consumer

May 21st, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.99 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <time.h>
  5. #include <pthread.h>
  6.  
  7. #define Q_SIZE 1024
  8. #define E_HEAP 11
  9.  
  10. #define ec_thread_creat(s) \
  11. if((s) != 0){printf("Couldn't create the thread!\n"); exit(err);}
  12.  
  13. #define ec_zero(s,m) \
  14. if((s) != 0){perror(m); exit(s);}
  15.  
  16. #define print_info \
  17. {printf("*** Type q, then press ret to exit!\n"); \
  18. sleep(3);}
  19.  
  20. struct node {
  21.     int info;
  22.     struct node *next;
  23. };
  24.  
  25. static struct node *head = NULL;
  26.  
  27. pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
  28. pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  29.  
  30. void* con(void* args);
  31. void* prod(void* args);
  32. static void cleanup_handler_con(void* arg);
  33. static void cleanup_handler_prod(void* arg);
  34.  
  35. int main() {
  36.    
  37.     pthread_t tid_con, tid_prod;
  38.     int err, flag;
  39.     char ctrl;
  40.  
  41.     print_info
  42.  
  43.     ec_thread_creat(err = pthread_create(&tid_prod, NULL, &prod, NULL));
  44.     printf("*** Producer created!\n");
  45.     ec_thread_creat(err = pthread_create(&tid_con, NULL, &con, NULL));
  46.     printf("*** Consumer created!\n");
  47.  
  48.     while(flag){
  49.         scanf("%c",&ctrl);
  50.         if(ctrl == 'q') flag = 0;
  51.         else print_info
  52.     }
  53.  
  54.     ec_zero((pthread_cancel(tid_prod)),"*** Couldn't delete the producer!\n");
  55.     ec_zero((pthread_cancel(tid_con)),"*** Couldn't delete the consumer!\n");
  56.  
  57.     return 0;
  58.  
  59. }
  60.  
  61. void* prod(void* args) {
  62.  
  63.     struct node *p, *i;
  64.     unsigned int seed;
  65.  
  66.     pthread_cleanup_push(cleanup_handler_prod,NULL);
  67.     while(1){
  68.         printf("--- I'm the producer\n");
  69.         if((p = malloc(sizeof(struct node)))==NULL) pthread_exit((void*) E_HEAP);
  70.         seed = time(0);
  71.         p->info = rand_r(&seed);
  72.         p->next = NULL;
  73.        
  74.         pthread_mutex_lock(&mutex);
  75.         printf("--- Producer locked\n");   
  76.         if(head != NULL){
  77.             i = head;
  78.             while(i->next != NULL) i = i->next;
  79.             i->next = p;
  80.         } else head = p;
  81.         sleep(2);
  82.         printf("--- Wake up consumer!\n");
  83.         pthread_cond_signal(&cond);
  84.         printf("--- Unlocking the producer\n");
  85.         pthread_mutex_unlock(&mutex);
  86.     }
  87.     pthread_cleanup_pop(0);
  88.  
  89.     return (void*) 0;
  90.  
  91. }
  92.  
  93. void* con(void* args) {
  94.  
  95.     struct node * v;
  96.  
  97.     pthread_cleanup_push(cleanup_handler_con,NULL);
  98.     while(1){
  99.         printf("--- I'm the consumer\n");
  100.         pthread_mutex_lock(&mutex);
  101.         printf("--- Consumer locked\n");
  102.         while(head == NULL){
  103.             printf("--- The consumer's sleeping ...ZzZzZ\n");
  104.             pthread_cond_wait(&cond,&mutex);
  105.         }
  106.         v = head->next;
  107.         printf("Number %d is leaving the queue...\n",head->info);
  108.         free(head);
  109.         head = v;
  110.         printf("--- Unlocking the consumer\n");
  111.         pthread_mutex_unlock(&mutex);
  112.     }
  113.     pthread_cleanup_pop(0);
  114.  
  115.     return (void*) 0;
  116. }
  117.  
  118. static void cleanup_handler_prod(void* arg) {
  119.  
  120.     printf("*** Deleting producer...\n");
  121.     pthread_mutex_unlock(&mutex);
  122.     while (head != NULL) sleep(1);
  123.  
  124. }
  125.  
  126. static void cleanup_handler_con(void* arg) {
  127.  
  128.     struct node *it;
  129.  
  130.     printf("*** Deleting consumer...\n");
  131.     if (head != NULL) {
  132.         it = head->next;
  133.         while (it != NULL) {
  134.             free(head);
  135.             head = it;
  136.             it = it->next;
  137.         }
  138.         free(head);
  139.         head = NULL;
  140.     }
  141.     pthread_mutex_unlock(&mutex);
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement