Updated Week7

This commit is contained in:
hello 2025-02-18 01:32:55 +05:30
parent 68a43beb49
commit 0c7b5f622a
4 changed files with 214 additions and 0 deletions

0
OS/C/Week7/aq1.c Normal file
View File

BIN
OS/C/Week7/q1 Executable file

Binary file not shown.

103
OS/C/Week7/q1.c Normal file
View File

@ -0,0 +1,103 @@
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define BUFFER_SIZE 10
#define NUM_ITEMS 20 // items to be produced/consumed
int buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
sem_t empty; // empty slots semaphore
sem_t full; // available items semaphore
pthread_mutex_t mutex; // mutex for shared vars
void* producer(void* arg) {
int item;
for (int i = 0; i < NUM_ITEMS; i++) {
item = i; // item is set to index
// waiting for empty slot
sem_wait(&empty);
// critical section
pthread_mutex_lock(&mutex);
// item placement into buffer
buffer[in] = item;
printf("Produced: %d at index %d\n", item, in);
in = (in + 1) % BUFFER_SIZE;
// critical section exit
pthread_mutex_unlock(&mutex);
// empty slot available
sem_post(&full);
// production time sim using sleep
sleep(1);
}
pthread_exit(NULL);
}
void* consumer(void* arg) {
int item;
for (int i = 0; i < NUM_ITEMS; i++) {
// waiting for at least one item in the buffer
sem_wait(&full);
// critical section
pthread_mutex_lock(&mutex);
// item removal from buffer
item = buffer[out];
printf("Consumed: %d from index %d\n", item, out);
out = (out + 1) % BUFFER_SIZE;
// critical section exit
pthread_mutex_unlock(&mutex);
// empty slot
sem_post(&empty);
// consumption time sim using sleep
sleep(2);
}
pthread_exit(NULL);
}
int main(void) {
pthread_t producer_tid, consumer_tid;
// semaphore init
sem_init(&empty, 0, BUFFER_SIZE); // empty slots init
sem_init(&full, 0, 0); // available items init
// mutex init
pthread_mutex_init(&mutex, NULL);
// thread creator
if (pthread_create(&producer_tid, NULL, producer, NULL) != 0) {
perror("Failed to create producer thread");
exit(EXIT_FAILURE);
}
if (pthread_create(&consumer_tid, NULL, consumer, NULL) != 0) {
perror("Failed to create consumer thread");
exit(EXIT_FAILURE);
}
// thread completion wait
pthread_join(producer_tid, NULL);
pthread_join(consumer_tid, NULL);
// cleanup of semaphores and mutex
sem_destroy(&empty);
sem_destroy(&full);
pthread_mutex_destroy(&mutex);
return 0;
}

111
OS/C/Week7/q2.c Normal file
View File

@ -0,0 +1,111 @@
// Write a C program to solve the readers and writers Problem.
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#define NUM_READERS 3
#define NUM_WRITERS 2
// shared data structure - simple integer
int shared_data = 0;
// sync variables
pthread_mutex_t mutex; // mutex for read_count protection
sem_t wrt; // semaphore for writer access control
int read_count = 0; // tracks active readers
void* reader(void* arg) {
int id = *((int*)arg);
while (1) {
// read delay sim
sleep(1);
// critical section entry
pthread_mutex_lock(&mutex);
read_count++;
if (read_count == 1) {
// first reader blocks writers
sem_wait(&wrt);
}
pthread_mutex_unlock(&mutex);
// critical section
printf("Reader %d is reading shared_data = %d\n", id, shared_data);
sleep(2); // read time sim
// critical section exit
pthread_mutex_lock(&mutex);
read_count--;
if (read_count == 0) {
// last reader unblocks writers
sem_post(&wrt);
}
pthread_mutex_unlock(&mutex);
}
pthread_exit(NULL);
}
void* writer(void* arg) {
int id = *((int*)arg);
while (1) {
// write delay sim
sleep(3);
// critical section entry
sem_wait(&wrt); // exclusive access control
// critical section
shared_data += 1;
printf("Writer %d is writing: shared_data becomes %d\n", id, shared_data);
sleep(2); // write time sim
// critical section exit
sem_post(&wrt);
}
pthread_exit(NULL);
}
int main(void) {
pthread_t readers[NUM_READERS], writers[NUM_WRITERS];
int reader_ids[NUM_READERS], writer_ids[NUM_WRITERS];
int i;
// sync init
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt, 0, 1); // binary semaphore init
// reader thread creation
for (i = 0; i < NUM_READERS; i++) {
reader_ids[i] = i + 1;
if (pthread_create(&readers[i], NULL, reader, &reader_ids[i]) != 0) {
perror("Failed to create reader thread");
exit(EXIT_FAILURE);
}
}
// writer thread creation
for (i = 0; i < NUM_WRITERS; i++) {
writer_ids[i] = i + 1;
if (pthread_create(&writers[i], NULL, writer, &writer_ids[i]) != 0) {
perror("Failed to create writer thread");
exit(EXIT_FAILURE);
}
}
// thread joining
for (i = 0; i < NUM_READERS; i++) {
pthread_join(readers[i], NULL);
}
for (i = 0; i < NUM_WRITERS; i++) {
pthread_join(writers[i], NULL);
}
// cleanup
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
return 0;
}