Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <semaphore.h>
- #include <unistd.h>
- #include <time.h>
- /*
- This project was made with the aim of implementing semaphores to solve a simple 2 producer-consumer problem, where the first problem
- is between mCounter and mMonitor threads as one is trying to access a shared resource count to change it, the other trying to read it then
- erase its contents. The other problem is between mMonitor and mCollector where a thread wants to write a buffer the item read from
- the shared resource count, while the other is trying to read it. The solution made below was made using semaphores (one empty and one full semaphore
- for each problem). To simulate random messages arriving to the system, rand() function is used. However, since rand isn't thread safe, it was not
- used during the critical section of each thread but rather in the main function loop assigning identifiers and creating threads.
- */
- #define SIZE 1000
- #define SIZEBUFF 5
- #define t1 10
- struct thread_args {
- int id;
- int random;
- };
- struct Queue
- {
- int front, rear, size;
- int* array;
- int capacity;
- };
- pthread_t mCounter[SIZE];
- pthread_t mMonitor, mCollector;
- sem_t s_full,s_empty,s_full1,s_empty1;
- int count=0;
- struct Queue* buffer;
- struct Queue* createQueue(int capacity); // UTILITY FUNCTION THAT INITIALIZES THE BUFFER QUEUE ACCORDING TO SIZEBUFF
- void enqueue(struct Queue* queue, int item); // UTILITY FUNCTION THAT ENQUEUES A CERTAIN ELEMENT IN THE QUEUE, THE BUFFER IN THIS CASE
- int dequeue(struct Queue* queue); // UTILITY FUNCTION THAT DEQUEUES THE FIRST ELEMENT IN THE QUEUE, THE BUFFER IN THIS CASE
- void* collectorCSection(void* arg); // CORE FUNCTION THAT MANAGES THE MCOLLECTOR CRITICAL SECTION
- void* counterCSection(void* arg); // CORE FUNCTION THAT MANAGES THE MCOUNTER CRITICAL SECTION
- void* monitorCSection(void* arg); // CORE FUNCTION THAT MANAGES THE MMONITOR CRITICAL SECTION
- int main()
- {
- int i;
- buffer = createQueue(SIZEBUFF);
- sem_init(&s_full,0,0); // INITIALIZING THE FULL SEMAPHORE TO 0 SINCE THE COUNT IS NOT YET INCREMENTED
- sem_init(&s_empty,0,1); // INITIALIZING THE EMPTY SEMAPHORE TO 1 SINCE THE COUNT IS NOT YET INCREMENTED
- sem_init(&s_full1,0,0); // INITIALIZING THE FULL SEMAPHORE TO 0, SINCE THERE ARE NO ELEMENTS IN THE BUFFER YET
- sem_init(&s_empty1,0,SIZE); // INITIALIZING THE EMPTY SEMAPHORE TO THE SIZE OF THE COUNTER THREADS ARRAY
- srand(time(NULL));
- for(i = 0; i < SIZE; i++)
- {
- struct thread_args *args = malloc (sizeof (struct thread_args));
- int random = rand()%20;
- args->id = i;
- args->random = random;
- pthread_create(&mCounter[i] , NULL , counterCSection , args); // CREATING MCOUNTER THREADS AND ASSIGNING THEM TO THEIR CRITICAL SECTION
- }
- pthread_create(&mMonitor,NULL,monitorCSection,NULL); // CREATING MMONITOR THREAD AND ASSIGNING IT TO ITS CRITICAL SECTION
- pthread_create(&mCollector,NULL,collectorCSection,NULL); // CREATING MCOLLECTOR THREAD AND ASSIGNING IT TO ITS CRITICAL SECTION
- for(i = 0; i < SIZE; i++)
- {
- pthread_join(mCounter[i],NULL); // JOIN MCOUNTER THREADS
- }
- pthread_join(mMonitor,NULL); // JOIN MMONITOR THREAD
- pthread_join(mCollector,NULL); // JOIN MCOLLECTOR THREAD
- return 0;
- }
- void* counterCSection(void* arg)
- {
- printf("\nSleeptime: %d",((struct thread_args *) arg)->random);
- sleep(((struct thread_args *) arg)->random); // SLEEP THE THREAD ACCORDING TO THE PSEUDO-RANDOM NUMBER GENERATED FOR IT IN MAIN()
- printf("\nCounter Thread %d: received a message.",((struct thread_args *) arg)->id+1);
- printf("\nCounter Thread %d: waiting to write.",((struct thread_args *) arg)->id+1);
- sem_wait(&s_empty1); // DECREMENT THE EMPTY SEMAPHORE
- count++; // INCREMENT THE COUNT INDICATING THE NUMBER OF THREADS WORKING THEIR CRITICAL SECTION AT THE MOMENT
- printf("\nCounter Thread %d: incrementing counter. Value = %d.",((struct thread_args *) arg)->id+1,count);
- sem_post(&s_full1); // INCREMENT THE FULL SEMAPHORE
- }
- void* monitorCSection(void* arg)
- {
- int i=0;
- int temp;
- while(1)
- {
- sleep(2); // SLEEP THE THREAD FOR 2 SECONDS TO SIMULATE THE THREAD WORKING AT 2 SEC INTERVALS
- printf("\nMonitor Thread: waiting to read.");
- sem_wait(&s_full1); // DECREMENT THE FULL SEMAPHORE
- temp = count; // STORE THE CURRENT COUNT OF WORKING THREADS
- printf("\nMonitor Thread: reading counter value = %d.",temp);
- sem_post(&s_empty1); // INCREMENT THE EMPTY SEMAPHORE
- sem_wait(&s_empty); // DECREMENT THE EMPTY SEMAPHORE FOR THE SECOND PROBLEM
- count=0; // SET THE COUNT OF WORKING TREADS TO 0
- enqueue(buffer,temp); // PLACE THE STORED COUNT IN THE BUFFER QUEUE
- sem_post(&s_full); // INCREMENT THE FULL SEMAPHORE FOR THE SECOND PROBLEM
- i++;
- }
- }
- void* collectorCSection(void* arg)
- {
- int i=0;
- while(1)
- {
- sleep(2); // SLEEP THE THREAD FOR 2 SECONDS TO SIMULATE THE THREAD WORKING AT 2 SEC INTERVALS
- sem_wait(&s_full); // DECREMENT THE FULL SEMAPHORE FOR THE 2ND PROBLEM
- dequeue(buffer); // GET A VALUE FROM THE BUFFER QUEUE
- sem_post(&s_empty); // INCREMENT THE EMPTY SEMAPHORE FOR THE 2ND PROBLEM
- i++;
- }
- }
- struct Queue* createQueue(int capacity)
- {
- struct Queue* queue = (struct Queue*) malloc(sizeof(struct Queue));
- queue->capacity = capacity;
- queue->front = queue->size = 0;
- queue->rear = capacity - 1;
- queue->array = (int*) malloc(queue->capacity * sizeof(int));
- return queue;
- }
- void enqueue(struct Queue* queue, int item)
- {
- if (queue->size == queue->capacity)
- {
- printf("\nMonitor Thread: Buffer full.");
- return;
- }
- queue->rear = (queue->rear + 1)%queue->capacity;
- queue->array[queue->rear] = item;
- queue->size = queue->size + 1;
- printf("\nMonitor Thread: Writing to buffer at index = %d.",buffer->size);
- }
- int dequeue(struct Queue* queue)
- {
- if (queue->size == 0)
- {
- printf("\nCollector Thread: Buffer is empty.");
- return -1;
- }
- int item = queue->array[queue->front];
- queue->front = (queue->front + 1)%queue->capacity;
- queue->size = queue->size - 1;
- printf("\nCollector Thread: Reading from the buffer value = %d.",item);
- return item;
- }
Advertisement
Add Comment
Please, Sign In to add comment