Advertisement
Guest User

Untitled

a guest
May 30th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.39 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4.  
  5. pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
  6. pthread_cond_t empty=PTHREAD_COND_INITIALIZER;
  7. pthread_cond_t full=PTHREAD_COND_INITIALIZER;
  8.  
  9. typedef struct elem {
  10.     int value;
  11.     struct elem *next;
  12. } Elem;
  13.  
  14. typedef struct blockingQueue {
  15.     unsigned sizeBuffer, statusBuffer;
  16.     Elem *head, *last;
  17. } BlockingQueue;
  18.  
  19. BlockingQueue BQ;
  20. Elem queue;
  21. int P, C, N;
  22. unsigned B=10;
  23.  
  24. void newBlockingQueue ()
  25. {
  26.     BQ.sizeBuffer=B;
  27.     BQ.statusBuffer=0;
  28.     BQ.head=NULL;
  29.     BQ.last=NULL;
  30.  
  31.     queue.value=0;
  32.     queue.next=NULL;
  33.  
  34.     return;
  35. }
  36.  
  37. void putBlockingQueue (int newValue)
  38. {
  39.     Elem *novo;
  40.     Elem *iter;
  41.  
  42.     novo=(Elem *)malloc(sizeof(Elem));
  43.     novo->value=newValue;
  44.     novo->next=NULL;
  45.  
  46.     if (BQ.statusBuffer==0) {
  47.         queue.next=novo;
  48.         BQ.head=novo;
  49.         BQ.last=novo;
  50.         BQ.statusBuffer++;
  51.     }
  52.     else {
  53.         iter=queue.next;
  54.         while (iter->next!=NULL) {
  55.             iter=iter->next;
  56.         }
  57.         iter->next=novo;
  58.         BQ.last=novo;
  59.         BQ.statusBuffer++;
  60.     }
  61. }
  62.  
  63. void *producer (void *threadidp)
  64. {
  65.  
  66. }
  67.  
  68. void takeBlockingQueue ()
  69. {
  70.  
  71. }
  72.  
  73. void *consumer (void *threadidc)
  74. {
  75.  
  76. }
  77.  
  78. int main ()
  79. {
  80.     freopen ("inQ4.in", "r", stdin);
  81.  
  82.     int i;
  83.     Elem *iter;
  84.  
  85.     newBlockingQueue();
  86.  
  87.     for (i=0; i<10; i++) {
  88.         putBlockingQueue(i);
  89.     }
  90.     iter=queue.next;
  91.     for (iter; iter!=NULL; iter=iter->next) {
  92.         printf ("%d ", iter->value);
  93.     }
  94.  
  95.     printf ("OK\n");
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement