Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.41 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <semaphore.h>
  6.  
  7. #define QUEUESIZE 20
  8. #define LOOP 15
  9. #define MAX_THREADS 10
  10.  
  11. void *producer (void *args);
  12. void *consumer (void *args);
  13.  
  14. int lambda;
  15. int queuesi;
  16. typedef struct {
  17. int buf[QUEUESIZE];
  18. long head, tail;
  19. int full, empty;
  20. pthread_mutex_t *mut;
  21. sem_t *sem_notFull, *sem_notEmpty;
  22. } queue;
  23.  
  24. queue *queueInit (void);
  25. void queueDelete (queue *q);
  26. void queueAdd (queue *q, int in);
  27. void queueDel (queue *q, int *out);
  28. int factorial( int i);
  29. int poissonfunction(int i);
  30. int rando();
  31.  
  32. int main (int argc, char *argv[])
  33. {
  34. int max_pro,max_con,j,i;
  35. queue *fifo;
  36. pthread_t prod[MAX_THREADS],cons[MAX_THREADS];
  37.  
  38. max_con=1;
  39. max_pro=4;
  40. lambda=6;
  41. queuesi=4;
  42.  
  43. fifo = queueInit ();
  44.  
  45. if (fifo == NULL) {
  46. fprintf (stderr, "main: Queue Init failed.n");
  47. exit (1);
  48. }
  49.  
  50.  
  51. for(i=0; i<max_pro;i++)
  52. pthread_create (&prod[i], NULL, producer,fifo);
  53.  
  54. for(j=0;j<max_con;j++)
  55. pthread_create (&cons[j], NULL, consumer, fifo);
  56.  
  57. for(i=0; i<max_pro;i++)
  58. pthread_join (prod[i], NULL);
  59.  
  60. for(j=0;j<max_con;j++)
  61. pthread_join (cons[j], NULL);
  62.  
  63. queueDelete (fifo);
  64.  
  65. return 0;
  66. }
  67.  
  68. void *producer (void *q)
  69. {
  70. queue *fifo;
  71. int i,insert,sleep_time;
  72. fifo = (queue *)q;
  73.  
  74. for (i = 0; i < LOOP; i++) {
  75. //pthread_mutex_lock (fifo->mut);
  76. while (fifo->full) {
  77. printf ("producer: queue FULL.n");
  78. sem_wait (fifo->sem_notFull);
  79. }
  80. pthread_mutex_lock (fifo->mut);
  81. insert=rando();
  82. queueAdd (fifo, insert);
  83.  
  84. printf("producer item number%d item produced %dn",i,insert);
  85. pthread_mutex_unlock (fifo->mut);
  86. sem_post (fifo->sem_notEmpty);
  87. sleep_time=poissonfunction(i);
  88. usleep (sleep_time);
  89. }
  90.  
  91. return (NULL);
  92. }
  93.  
  94. void *consumer (void *q)
  95. {
  96. queue *fifo;
  97. int i, d;
  98.  
  99. fifo = (queue *)q;
  100.  
  101. for (i = 0; i < LOOP; i++) {
  102. //pthread_mutex_lock (fifo->mut);
  103. while (fifo->empty) {
  104. printf ("consumer: queue emptyn");
  105. sem_wait (fifo->sem_notEmpty);
  106. }
  107. pthread_mutex_lock (fifo->mut);
  108. queueDel (fifo, &d);
  109. pthread_mutex_unlock (fifo->mut);
  110. sem_post (fifo->sem_notFull);
  111. printf ("consumer: recieved %d.n", d);
  112. usleep(2000);
  113. }
  114.  
  115. return (NULL);
  116. }
  117.  
  118. queue *queueInit (void)
  119. {
  120. queue *q;
  121.  
  122. q = (queue *)malloc (sizeof (queue));
  123. if (q == NULL) return (NULL);
  124. q->empty = 1;
  125. q->full = 0;
  126. q->head = 0;
  127. q->tail = 0;
  128. q->mut = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
  129. pthread_mutex_init (q->mut, NULL);
  130. q->sem_notFull = (sem_t *) malloc (sizeof (sem_t));
  131. sem_init (q->sem_notFull, 0, 1);
  132. q->sem_notEmpty = (sem_t *) malloc (sizeof (sem_t));
  133. sem_init (q->sem_notEmpty, 0, 1);
  134.  
  135. return (q);
  136. }
  137.  
  138. void queueDelete (queue *q)
  139. {
  140. pthread_mutex_destroy (q->mut);
  141. free (q->mut);
  142. sem_destroy (q->sem_notFull);
  143. free (q->sem_notFull);
  144.  
  145. sem_destroy (q->sem_notEmpty);
  146. free (q->sem_notEmpty);
  147.  
  148. free (q);
  149. }
  150.  
  151. void queueAdd (queue *q, int in)
  152. {
  153. q->buf[q->tail] = in;
  154. q->tail++;
  155. if (q->tail == queuesi)
  156. q->tail = 0;
  157.  
  158. if (q->tail == q->head)
  159. q->full = 1;
  160. q->empty = 0;
  161.  
  162. return;
  163. }
  164.  
  165. void queueDel (queue *q, int *out)
  166. {
  167. *out = q->buf[q->head];
  168.  
  169. q->head++;
  170. if (q->head == queuesi)
  171. q->head = 0;
  172. if (q->head == q->tail)
  173. q->empty = 1;
  174. q->full = 0;
  175.  
  176. return;
  177. }
  178.  
  179. int factorial(int i){
  180. if(i==0)
  181. return i=1;
  182. else
  183. i=i*factorial(i-1);
  184. return (i);
  185. }
  186.  
  187. int poissonfunction (int i){
  188. int time,c,t;
  189. double p;
  190. t = 1000;
  191. c = lambda*t;
  192. p = (pow(c,i)*exp(-c))/factorial(i);
  193. time =(int) p*t;
  194. return (time);
  195. }
  196.  
  197. int rando(){
  198. int value;
  199. value=(int) random()/1000;
  200. return (value);
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement