0% found this document useful (0 votes)
16 views

Assignment No 4a

Uploaded by

vedikadhebe2712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Assignment No 4a

Uploaded by

vedikadhebe2712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

ASSIGNMENT NO.

4 - A
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define buffer_size 5 // Maximum number of items in the buffer


#define num_items 10 // Number of items to be produced/consumed

int buffer[buffer_size]; // Shared buffer array


int in = 0; // Index for producer to place item
int out = 0; // Index for consumer to take item

sem_t empty; // Semaphore to track empty slots in the buffer


sem_t full; // Semaphore to track filled slots in the buffer
pthread_mutex_t mutex; // Mutex lock for critical section (buffer access)

// Producer thread function


void* producer(void* param)
{
for (int i = 0; i < num_items; i++)
{
int item = rand() % 100; // Produce a random item (0-99)

sem_wait(&empty); // Decrement empty, wait if buffer is full

pthread_mutex_lock(&mutex); // Enter critical section


buffer[in] = item; // Place item in buffer at index 'in'
in = (in + 1) % buffer_size; // Move 'in' index circularly
printf("Produced: %d\n", item);

pthread_mutex_unlock(&mutex); // Leave critical section

sem_post(&full); // Increment full, signaling an item is produced


}
return NULL;
}

// Consumer thread function


void* consumer(void* param)
{
for (int i = 0; i < num_items; i++)
{
sem_wait(&full); // Decrement full, wait if buffer is empty

pthread_mutex_lock(&mutex); // Enter critical section


int item = buffer[out]; // Remove item from buffer at index 'out'
out = (out + 1) % buffer_size; // Move 'out' index circularly
printf("Consumed: %d\n", item);

pthread_mutex_unlock(&mutex); // Leave critical section

sem_post(&empty); // Increment empty, signaling an item is consumed


}
return NULL;
}

int main() {
pthread_t prod, cons; // Declare producer and consumer threads

// Initialize semaphores
sem_init(&empty, 0, buffer_size); // 'empty' is initialized to the buffer size (all slots are empty)
sem_init(&full, 0, 0); // 'full' is initialized to 0 (no items produced yet)

pthread_mutex_init(&mutex, NULL); // Initialize mutex

// Create producer and consumer threads


pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);

// Wait for producer and consumer to finish


pthread_join(prod, NULL);
pthread_join(cons, NULL);

// Destroy semaphores and mutex after threads are done


sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);

return 0;
}

Output:
vedika@vedika-virtual-machine:~ gcc prodcons.c -o prodcons
vedika@vedika-virtual-machine:~ ./prodcons
Produced: 23 at 0
Produced: 54 at 1
Produced: 78 at 2
Consumed: 23 from 0
Consumed: 54 from 1
Produced: 45 at 3
Consumed: 78 from 2
Produced: 19 at 4
Consumed: 45 from 3
Produced: 67 at 0
Produced: 89 at 1
Consumed: 19 from 4
Consumed: 67 from 0
Consumed: 89 from 1

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy