[go: up one dir, main page]

0% found this document useful (0 votes)
9 views5 pages

OS Classprob

OS important

Uploaded by

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

OS Classprob

OS important

Uploaded by

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

OS-Lab-Classical Problems on

Synchronization
23BAI1357 HARIHARAN GV

1. Write a C program to implement the Bounded Buffer Problem (Producer-


Consumer Problem) using semaphores and mutex locks. The program should
satisfy the following conditions:
a) A bounded buffer (fixed-size array) is shared between producers and
consumers.
b) Producers should be able to add items to the buffer only if there is space
available.
c) Consumers should be able to remove items only if the buffer is not empty.
d) Use semaphores to manage the synchronization between producer and
consumer threads.
e) Ensure mutual exclusion so that multiple threads do not modify the buffer
simultaneously.
2. Write a C program to implement the Reader-Writer problem using semaphores.
The program should satisfy the following conditions:
a) Multiple readers can read simultaneously.
b) Only one writer can write at a time.
c) When a writer is writing, no reader should be allowed.
d) Use semaphores to manage synchronization between readers and writers.
NOTE:
The reader should display the content he/she had read along with their name
e.g
content read is “hai how are you?”
output
Manimegalai hai how are you
3. Write a C program to implement the Dining Philosophers Problem using
monitors for synchronization. The program should meet the following
requirements:
a. Five philosophers sit at a round table, alternating between thinking and eating.
b. Each philosopher can only eat when both left and right chopsticks are
available.
c. Use monitors (mutex & condition variables) to control access to the shared
chopsticks.
d. Ensure no deadlock and no starvation by allowing fair access to chopsticks.
e. Implement the following functions inside a monitor class:
i. pickUp(int i): Philosopher i picks up chopsticks when available.
ii. putDown(int i): Philosopher i puts down chopsticks and allows neighbors to
eat.
iii. test(int i): Checks if a philosopher can eat.
f. Use threads to simulate

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

#define BUFFER_SIZE 5
#define NUM_READERS 3
#define NUM_WRITERS 2
#define NUM_PHILOSOPHERS 5

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


sem_t empty, full;
pthread_mutex_t mutex;

void *producer(void *arg) {


int item;
for (int i = 0; i < 5; i++) {
item = i;
sem_wait(&empty);
pthread_mutex_lock(&mutex);
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&full);
sleep(1);
}
}

void *consumer(void *arg) {


int item;
for (int i = 0; i < 5; i++) {
sem_wait(&full);
pthread_mutex_lock(&mutex);
item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
sleep(1);
}
}

sem_t rw_mutex, mutex_rw;


int read_count = 0;
char content[] = "hai how are you?";

void *reader(void *arg) {


sem_wait(&mutex_rw);
read_count++;
if (read_count == 1)
sem_wait(&rw_mutex);
sem_post(&mutex_rw);
printf("%s %s\n", (char *)arg, content);
sem_wait(&mutex_rw);
read_count--;
if (read_count == 0)
sem_post(&rw_mutex);
sem_post(&mutex_rw);
}

void *writer(void *arg) {


sem_wait(&rw_mutex);
printf("Writer %s writing...\n", (char *)arg);
sem_post(&rw_mutex);
}

pthread_mutex_t chopstick_mutex;
pthread_cond_t condition[NUM_PHILOSOPHERS];
int state[NUM_PHILOSOPHERS];

void test(int i) {
if (state[i] == 1 && state[(i + 4) % 5] != 2 && state[(i + 1) %
5] != 2) {
state[i] = 2;
pthread_cond_signal(&condition[i]);
}
}

void pickUp(int i) {
pthread_mutex_lock(&chopstick_mutex);
state[i] = 1;
test(i);
while (state[i] != 2)
pthread_cond_wait(&condition[i], &chopstick_mutex);
pthread_mutex_unlock(&chopstick_mutex);
}

void putDown(int i) {
pthread_mutex_lock(&chopstick_mutex);
state[i] = 0;
test((i + 4) % 5);
test((i + 1) % 5);
pthread_mutex_unlock(&chopstick_mutex);
}

void *philosopher(void *num) {


int i = *(int *)num;
while (1) {
pickUp(i);
sleep(1);
putDown(i);
}
}

int main() {
pthread_t prod, cons, readers[NUM_READERS], writers[NUM_WRITERS],
philosophers[NUM_PHILOSOPHERS];
int ids[NUM_PHILOSOPHERS] = {0, 1, 2, 3, 4};

sem_init(&empty, 0, BUFFER_SIZE);
sem_init(&full, 0, 0);
pthread_mutex_init(&mutex, NULL);
pthread_create(&prod, NULL, producer, NULL);
pthread_create(&cons, NULL, consumer, NULL);
pthread_join(prod, NULL);
pthread_join(cons, NULL);

sem_init(&rw_mutex, 0, 1);
sem_init(&mutex_rw, 0, 1);
for (int i = 0; i < NUM_READERS; i++)
pthread_create(&readers[i], NULL, reader, "Reader");
for (int i = 0; i < NUM_WRITERS; i++)
pthread_create(&writers[i], NULL, writer, "Writer");
for (int i = 0; i < NUM_READERS; i++)
pthread_join(readers[i], NULL);
for (int i = 0; i < NUM_WRITERS; i++)
pthread_join(writers[i], NULL);

pthread_mutex_init(&chopstick_mutex, NULL);
for (int i = 0; i < NUM_PHILOSOPHERS; i++)
pthread_cond_init(&condition[i], NULL);
for (int i = 0; i < NUM_PHILOSOPHERS; i++)
pthread_create(&philosophers[i], NULL, philosopher, &ids[i]);
for (int i = 0; i < NUM_PHILOSOPHERS; i++)
pthread_join(philosophers[i], NULL);
return 0;
}

You might also like