0% found this document useful (0 votes)
23 views2 pages

Os 4B

This C code implements a reader-writer problem using semaphores for synchronization between multiple reader and writer threads. It defines reader and writer functions that simulate reading and writing operations. The main function creates reader and writer threads, joining them after the maximum number of operations is reached to ensure all threads complete.

Uploaded by

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

Os 4B

This C code implements a reader-writer problem using semaphores for synchronization between multiple reader and writer threads. It defines reader and writer functions that simulate reading and writing operations. The main function creates reader and writer threads, joining them after the maximum number of operations is reached to ensure all threads complete.

Uploaded by

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

#include <stdio.

h>
#include <pthread.h>
#include <semaphore.h>
#include <stdlib.h>

#define NUM_READERS 3
#define NUM_WRITERS 2
#define MAX_OPERATIONS 10 // Define the maximum number of operations for readers
and writers

sem_t mutex, writeMutex;


int readCount = 0;
int totalReaderOperations = 0;
int totalWriterOperations = 0;

void *reader(void *arg) {


while (totalReaderOperations < MAX_OPERATIONS) {
// Reader Entry Section
sem_wait(&mutex);
readCount++;
if (readCount == 1) {
sem_wait(&writeMutex);
}
sem_post(&mutex);

// Reading
printf("Reader %ld is reading...\n", (long)arg);
// Simulate reading by sleeping
sleep(2);

// Reader Exit Section


sem_wait(&mutex);
readCount--;
if (readCount == 0) {
sem_post(&writeMutex);
}
sem_post(&mutex);

// Increment the total reader operations


totalReaderOperations++;
}
pthread_exit(NULL);
}

void *writer(void *arg) {


while (totalWriterOperations < MAX_OPERATIONS) {
// Writer Entry Section
sem_wait(&writeMutex);

// Writing
printf("Writer %ld is writing...\n", (long)arg);
// Simulate writing by sleeping
sleep(3);

// Writer Exit Section


sem_post(&writeMutex);

// Increment the total writer operations


totalWriterOperations++;
}
pthread_exit(NULL);
}

int main() {
pthread_t readers[NUM_READERS], writers[NUM_WRITERS];

sem_init(&mutex, 0, 1);
sem_init(&writeMutex, 0, 1);

for (long i = 0; i < NUM_READERS; i++) {


pthread_create(&readers[i], NULL, reader, (void *)i);
}

for (long i = 0; i < NUM_WRITERS; i++) {


pthread_create(&writers[i], NULL, writer, (void *)i);
}

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);
}

return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy