|
| 1 | +// #include <stdio.h> |
| 2 | +// #include <pthread.h> // header file for multithreading |
| 3 | +// #include <semaphore.h> |
| 4 | +// #include <unistd.h> |
| 5 | + |
| 6 | +// // sem_t struct is alreading defined inside of the <semaphore.h> |
| 7 | + |
| 8 | +// sem_t mutex; |
| 9 | + |
| 10 | +// void* thread(void* arg) |
| 11 | +// { |
| 12 | +// //wait |
| 13 | +// sem_wait(&mutex); // lock or wait |
| 14 | +// printf("\nEntered...\n"); |
| 15 | + |
| 16 | +// //important |
| 17 | +// sleep(4); |
| 18 | + |
| 19 | +// //signal - which we had discussed in the previous video also |
| 20 | +// printf("\n Just Exiting\n"); |
| 21 | +// sem_post(&mutex); // this is used to release the signal. |
| 22 | +// } |
| 23 | + |
| 24 | +// int main() { |
| 25 | +// sem_init(&mutex, 0, 1); |
| 26 | +// pthread_t t1, t2, t3; // let us create three threads |
| 27 | +// pthread_create(&t1, NULL, thread, NULL); |
| 28 | +// sleep(2); |
| 29 | +// pthread_create(&t2, NULL, thread, NULL); |
| 30 | +// sleep(2); |
| 31 | +// pthread_create(&t3, NULL, thread, NULL); |
| 32 | +// pthread_join(t1, NULL); |
| 33 | +// pthread_join(t2, NULL); |
| 34 | +// pthread_join(t3, NULL); |
| 35 | +// sem_destroy(&mutex); // destroy the semaphore |
| 36 | + |
| 37 | +// return 0 ; |
| 38 | +// } |
| 39 | + |
| 40 | +/* |
| 41 | +* The Single Producer-Consumer Problem |
| 42 | +* 1. Shared Buffer |
| 43 | +* 2. Producer Thread |
| 44 | +* 3. Consumer Thread |
| 45 | +*/ |
| 46 | + |
| 47 | +#include <stdio.h> |
| 48 | +#include <stdlib.h> |
| 49 | +#include <pthread.h> // we will use this to create the producer and consumer threads |
| 50 | +#include <semaphore.h> |
| 51 | +#include <unistd.h> // this header file has the sleep() function definition. |
| 52 | + |
| 53 | +#define MAX_SIZE 10 |
| 54 | + |
| 55 | +sem_t mutex; |
| 56 | +int queue[MAX_SIZE]; |
| 57 | +int front = -1, rear = -1; |
| 58 | + |
| 59 | +int isFull() |
| 60 | +{ |
| 61 | + return (rear+1)%MAX_SIZE == front; |
| 62 | +} |
| 63 | + |
| 64 | +int isEmpty() |
| 65 | +{ |
| 66 | + return front == -1; |
| 67 | +} |
| 68 | + |
| 69 | +// function to enqueue(insert) an element |
| 70 | +void* enqueue(void* arg) { |
| 71 | + |
| 72 | + if(isFull()) { |
| 73 | + printf("Queue Overflow"); |
| 74 | + return 0; |
| 75 | + } |
| 76 | + |
| 77 | + if(front == -1) { |
| 78 | + front = 0; |
| 79 | + } |
| 80 | + |
| 81 | + rear = (rear+1)%MAX_SIZE; |
| 82 | + printf("Buffer can be shared\n"); |
| 83 | +} |
| 84 | + |
| 85 | +// function to dequeue(remove) an element |
| 86 | +void* dequeue(void* arg) { |
| 87 | + |
| 88 | + if(isEmpty()) { |
| 89 | + printf("Queue underflow\n"); |
| 90 | + printf("Buffer cannot be shared now"); |
| 91 | + exit(1); |
| 92 | + } |
| 93 | + |
| 94 | + if(front == rear) { |
| 95 | + front = rear = -1; |
| 96 | + }else { |
| 97 | + front = (front+1)%MAX_SIZE; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | + |
| 103 | +int main() { |
| 104 | + sem_init(&mutex, 0, 1); |
| 105 | + pthread_t thread_producer, thread_consumer; |
| 106 | + |
| 107 | + pthread_create(&thread_producer, NULL, enqueue, NULL); |
| 108 | + sleep(2); |
| 109 | + |
| 110 | + pthread_create(&thread_producer, NULL, enqueue, NULL); |
| 111 | + sleep(2); |
| 112 | + |
| 113 | + pthread_create(&thread_consumer, NULL, dequeue, NULL); |
| 114 | + sleep(2); |
| 115 | + |
| 116 | + pthread_create(&thread_consumer, NULL, dequeue, NULL); |
| 117 | + sleep(2); |
| 118 | + |
| 119 | + pthread_create(&thread_consumer, NULL, dequeue, NULL); |
| 120 | + pthread_join(thread_producer, NULL); |
| 121 | + pthread_join(thread_consumer, NULL); |
| 122 | + |
| 123 | + sem_destroy(&mutex); |
| 124 | + |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | + |
0 commit comments