added optimised OS codes

This commit is contained in:
sherlock 2025-04-03 19:02:05 +05:30
parent 0fc0033265
commit fbcc758f43
2 changed files with 39 additions and 45 deletions

BIN
OS/C/Week7/q2 Executable file

Binary file not shown.

View file

@ -6,106 +6,100 @@
#include <semaphore.h>
#include <unistd.h>
#define NUM_READERS 3
#define NUM_WRITERS 2
#define NR 3 // NUM_READERS
#define NW 2 // NUM_WRITERS
// shared data structure - simple integer
int shared_data = 0;
int sd = 0; // shared_data
// 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
pthread_mutex_t m; // mutex for rc protection
sem_t w; // semaphore for writer access control
int rc = 0; // read_count: tracks active readers
void* reader(void* arg) {
void* r(void* arg) { // reader
int id = *((int*)arg);
while (1) {
// read delay sim
sleep(1);
// critical section entry
pthread_mutex_lock(&mutex);
read_count++;
if (read_count == 1) {
pthread_mutex_lock(&m);
rc++;
if (rc == 1) {
// first reader blocks writers
sem_wait(&wrt);
sem_wait(&w);
}
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&m);
// critical section
printf("Reader %d is reading shared_data = %d\n", id, shared_data);
printf("Reader %d is reading sd = %d\n", id, sd);
sleep(2); // read time sim
// critical section exit
pthread_mutex_lock(&mutex);
read_count--;
if (read_count == 0) {
pthread_mutex_lock(&m);
rc--;
if (rc == 0) {
// last reader unblocks writers
sem_post(&wrt);
sem_post(&w);
}
pthread_mutex_unlock(&mutex);
pthread_mutex_unlock(&m);
}
pthread_exit(NULL);
}
void* writer(void* arg) {
void* w_func(void* arg) { // writer
int id = *((int*)arg);
while (1) {
// write delay sim
sleep(3);
// critical section entry
sem_wait(&wrt); // exclusive access control
sem_wait(&w); // exclusive access control
// critical section
shared_data += 1;
printf("Writer %d is writing: shared_data becomes %d\n", id, shared_data);
sd++; // shared_data modification
printf("Writer %d is writing: sd becomes %d\n", id, sd);
sleep(2); // write time sim
// critical section exit
sem_post(&wrt);
sem_post(&w);
}
pthread_exit(NULL);
}
int main(void) {
pthread_t readers[NUM_READERS], writers[NUM_WRITERS];
int reader_ids[NUM_READERS], writer_ids[NUM_WRITERS];
pthread_t rts[NR], wts[NW]; // reader/writer threads
int rids[NR], wids[NW]; // reader/writer ids
int i;
// sync init
pthread_mutex_init(&mutex, NULL);
sem_init(&wrt, 0, 1); // binary semaphore init
pthread_mutex_init(&m, NULL);
sem_init(&w, 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);
}
for (i = 0; i < NR; i++) {
rids[i] = i + 1;
pthread_create(&rts[i], NULL, r, &rids[i]);
}
// 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);
}
for (i = 0; i < NW; i++) {
wids[i] = i + 1;
pthread_create(&wts[i], NULL, w_func, &wids[i]);
}
// thread joining
for (i = 0; i < NUM_READERS; i++) {
pthread_join(readers[i], NULL);
for (i = 0; i < NR; i++) {
pthread_join(rts[i], NULL);
}
for (i = 0; i < NUM_WRITERS; i++) {
pthread_join(writers[i], NULL);
for (i = 0; i < NW; i++) {
pthread_join(wts[i], NULL);
}
// cleanup
pthread_mutex_destroy(&mutex);
sem_destroy(&wrt);
pthread_mutex_destroy(&m);
sem_destroy(&w);
return 0;
}