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

Lab7

Uploaded by

Maaz Sayyed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Lab7

Uploaded by

Maaz Sayyed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment Number: 7

NAME: Sayyed Md Muaz Aslam ROLLNO: 60


CLASS: TY IT-A BATCH: B3
PRN No.: 12110133

Problem Statement:

a) : Implementation of Classical problem Producer Consumer using Threads and Semaphore

b)Implementation of Classical problem Producer Consumer using Threads and Mutex

Problem A:

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>

#define BUFFER_SIZE 5

sem_t empty, full, mutex;


int buffer[BUFFER_SIZE];
int in = 0, out = 0;

void *producer(void *arg) {


int item;
for (int i = 0; i < 5; i++) {
item = i + 1; // Produce an item
sem_wait(&empty);
sem_wait(&mutex);

// Add item to the buffer


buffer[in] = item;
printf("Producer produced item: %d\n", item);
in = (in + 1) % BUFFER_SIZE;

sem_post(&mutex);
sem_post(&full);
sleep(1);
}
pthread_exit(NULL);
}

void *consumer(void *arg) {


int item;
for (int i = 0; i < 5; i++) {
sem_wait(&full);
sem_wait(&mutex);

// Consume an item from the buffer


item = buffer[out];
printf("Consumer consumed item: %d\n", item);
out = (out + 1) % BUFFER_SIZE;
sem_post(&mutex);
sem_post(&empty);
sleep(1);
}
pthread_exit(NULL);
}

int main() {
pthread_t producers[1], consumers[1];

sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
sem_init(&mutex, 0, 1);

pthread_create(&producers[0], NULL, producer, NULL);


pthread_create(&consumers[0], NULL, consumer, NULL);

pthread_join(producers[0], NULL);
pthread_join(consumers[0], NULL);

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

return 0;
}

Output:

Problem B:

#include <stdio.h>
#include <pthread.h>
#include <unistd.h>

#define BUFFER_SIZE 5

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;


int buffer[BUFFER_SIZE];
int in = 0, out = 0;

void *producer(void *arg) {


int item;
for (int i = 0; i < 5; i++) {
item = i + 1; // Produce an item
pthread_mutex_lock(&mutex);

// Check if the buffer is full


while (((in + 1) % BUFFER_SIZE) == out) {
pthread_mutex_unlock(&mutex);
usleep(1000);
pthread_mutex_lock(&mutex);
}

// Add item to the buffer


buffer[in] = item;
printf("Producer produced item: %d\n", item);
in = (in + 1) % BUFFER_SIZE;

pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}

void *consumer(void *arg) {


int item;
for (int i = 0; i < 5; i++) {
pthread_mutex_lock(&mutex);

// Check if the buffer is empty


while (in == out) {
pthread_mutex_unlock(&mutex);
usleep(1000);
pthread_mutex_lock(&mutex);
}

// Consume an item from the buffer


item = buffer[out];
printf("Consumer consumed item: %d\n", item);
out = (out + 1) % BUFFER_SIZE;

pthread_mutex_unlock(&mutex);
sleep(1);
}
pthread_exit(NULL);
}

int main() {
pthread_t producers[1], consumers[1];

pthread_create(&producers[0], NULL, producer, NULL);


pthread_create(&consumers[0], NULL, consumer, NULL);

pthread_join(producers[0], NULL);
pthread_join(consumers[0], NULL);

return 0;
}
Output:

You might also like