Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.74 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <semaphore.h>
  6. #include <stdbool.h>
  7.  
  8. typedef struct Item {
  9.     int data;
  10.     char *text_data;
  11. } Item;
  12.  
  13. pthread_mutex_t mutex;
  14.  
  15. int head = 0;
  16. int tail = 0;
  17. int queue_size = 1000;
  18. int current_size = 0;
  19. sem_t semaphore;
  20.  
  21. Item *circular_queue;
  22.  
  23. bool isEmpty() {
  24.     return current_size == 0;
  25. }
  26.  
  27. bool isFull() {
  28.     return current_size == queue_size;
  29. }
  30.  
  31. void push(int val, char *text) {
  32.     tail = (tail + 1) % queue_size;
  33.     circular_queue[tail].data = val;
  34.     circular_queue[tail].text_data = text;
  35.     current_size++;
  36. }
  37.  
  38. Item pop() {
  39.     Item current = circular_queue[head];
  40.     head = (head + 1) % queue_size;
  41.     current_size--;
  42.     return current;
  43. }
  44.  
  45.  
  46. void *prod(void *vargp) {
  47.     int ret, x;
  48.     char *text;
  49.     while ((ret = scanf("%d %ms", &x, &text)) == 2) {
  50.         pthread_mutex_lock(&mutex);
  51.         if (x < 0) {
  52.             exit(1);
  53.         } else {
  54.             push(x, text);
  55.         }
  56.         pthread_mutex_unlock(&mutex);
  57.         sem_post(&semaphore);
  58.     }
  59.     pthread_exit(0);
  60. }
  61.  
  62. void *cons(void *vargp) {
  63.     while (13) {
  64.         sem_wait(&semaphore);
  65.         pthread_mutex_lock(&mutex);
  66.         if (!isEmpty()) {
  67.             Item temp = pop();
  68.             printf("Thread %d:", *(int *) vargp);
  69.             for (int i = 0; i < temp.data; i++) {
  70.                 printf(" %s", temp.text_data);
  71.             }
  72.             printf("\n");
  73.             pthread_mutex_unlock(&mutex);
  74.         } else {
  75.             pthread_mutex_unlock(&mutex);
  76.             pthread_exit(0);
  77.         }
  78.     }
  79. }
  80.  
  81. int main(int argc, char *argv[]) {
  82.     int thread_count = 1;
  83.     if (argc == 2) {
  84.         thread_count = atoi(argv[1]);
  85.     }
  86.     if (thread_count < 1 || thread_count > sysconf(_SC_NPROCESSORS_ONLN)) {
  87.         exit(1);
  88.     }
  89.     circular_queue = (Item *) malloc(sizeof(Item) * queue_size);
  90.     pthread_t prod_id;
  91.     pthread_t *cons_ids;
  92.     cons_ids = (pthread_t *) malloc(thread_count * sizeof(pthread_t));
  93.     int *thread_args = (int *) malloc(thread_count * sizeof(int));
  94.     sem_init(&semaphore, 0, 0);
  95.     pthread_mutex_init(&mutex, NULL);
  96.     pthread_create(&prod_id, NULL, prod, NULL);
  97.     for (int i = 0; i < thread_count; i++) {
  98.         thread_args[i] = i + 1;
  99.         pthread_create(&(cons_ids[i]), NULL, cons, &(thread_args[i]));
  100.     }
  101.     pthread_join(prod_id, NULL);
  102.     for (int i = 0; i < thread_count; i++) {
  103.         sem_post(&semaphore);
  104.     }
  105.     for (int i = 0; i < thread_count; i++) {
  106.         pthread_join(cons_ids[i], NULL);
  107.     }
  108.     pthread_mutex_destroy(&mutex);
  109.     sem_destroy(&semaphore);
  110.     free(cons_ids);
  111.     free(thread_args);
  112.     free(circular_queue);
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement