diff --git a/OS/C/Week7/aq1.c b/OS/C/Week7/aq1.c new file mode 100644 index 0000000..e69de29 diff --git a/OS/C/Week7/q1 b/OS/C/Week7/q1 new file mode 100755 index 0000000..01dc9fa Binary files /dev/null and b/OS/C/Week7/q1 differ diff --git a/OS/C/Week7/q1.c b/OS/C/Week7/q1.c new file mode 100644 index 0000000..d4ac3af --- /dev/null +++ b/OS/C/Week7/q1.c @@ -0,0 +1,103 @@ +#include +#include +#include +#include +#include + +#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; +} diff --git a/OS/C/Week7/q2.c b/OS/C/Week7/q2.c new file mode 100644 index 0000000..1d68558 --- /dev/null +++ b/OS/C/Week7/q2.c @@ -0,0 +1,111 @@ +// Write a C program to solve the readers and writers Problem. + +#include +#include +#include +#include +#include + +#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; +}