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

Homework Week-9

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

Homework Week-9

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

Homework Week-9

You are tasked with implementing a synchronization mechanism for a system where
multiple threads act as either readers or writers on a shared global array. Readers can
simultaneously read from the array, but a writer requires exclusive access, meaning no
other reader or writer should access the array when a writer is writing.
In this version of the problem, writers should have priority. If a writer requests access
to the array, it must be granted access as soon as possible, meaning that no new
readers should start reading while there is a writer waiting.
The array contains a xed number of elements, and readers may read any part of the
array, while writers modify a speci c portion (or the entire array) during their write
operation.
Global Array:
• De ne a global array, int shared_array[10];, that contains 10 elements.
• Readers may read values from any position of the array.
• Writers may modify values at speci c positions or the entire array.
Requirements:
1. Readers:
o Multiple reader threads should be allowed to read the values of the global
array concurrently.
o If a writer is writing or waiting to write, readers must wait until the writer
nishes.
o When a reader starts reading, it must signal that it is currently reading by
increasing a reader count (e.g., active_readers).
2. Writers:
o Only one writer thread can modify the global array at a time, with exclusive
access.
o If any readers are reading or another writer is writing, the writer must wait
until the array becomes free.
o When the writer nishes writing, it should signal that the resource (the
global array) is available for other readers or writers.
o The writer can modify one or more elements of the array or all elements,
simulating a write operation.
3. Synchronization:
o Readers and writers should be properly synchronized using a mutex to
protect access to the global array and the counters (active_readers,
active_writers).
o Use condition variables to manage the priority of writers and prevent
readers from accessing the array while a writer is waiting.
4. Writer Priority:
o If a writer is waiting to modify the array, no new readers should be allowed
to start reading until the writer nishes.
o Writers should be granted priority access in the order they request access
(FIFO).
5. Termination:
fi
fi
fi
fi
fi
fi
fi
o The program should gracefully terminate after a certain number of read
and write operations have been performed (for example, after 100 read/
write actions).
Constraints:
• You should use POSIX Threads (pthreads).
• You must use a mutex lock to protect access to the global array and the
counters.
• You must use condition variables to manage the state of readers and writers (to
handle waiting and signaling).

Code:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

int shared_array[10];
int active_readers = 0;
int active_writers = 0;
int waiting_writers = 0;
pthread_mutex_t rw_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t can_read = PTHREAD_COND_INITIALIZER;
pthread_cond_t can_write = PTHREAD_COND_INITIALIZER;

void *reader(void *arg) {


long id = (long)arg;
int iterations = 3;

for (int iter = 0; iter < iterations; iter++) {


pthread_mutex_lock(&rw_mutex);

while (active_writers > 0 || waiting_writers > 0) {


pthread_cond_wait(&can_read, &rw_mutex);
}

active_readers++;
pthread_mutex_unlock(&rw_mutex);

printf("Reader %ld is reading the array (iteration %d): {", id, iter + 1);
for (int i = 0; i < 10; i++) {
printf("%d", shared_array[i]);
if (i < 9) {
printf(", ");
}
}
printf("}\n");

sleep(1);

pthread_mutex_lock(&rw_mutex);
active_readers--;
if (active_readers == 0) {
pthread_cond_signal(&can_write);
}
pthread_mutex_unlock(&rw_mutex);

sleep(1);
}

return NULL;
}

void *writer(void *arg) {


long id = (long)arg;
int iterations = 3;

for (int iter = 0; iter < iterations; iter++) {


pthread_mutex_lock(&rw_mutex);
waiting_writers++;

while (active_readers > 0 || active_writers > 0) {


pthread_cond_wait(&can_write, &rw_mutex);
}

waiting_writers--;
active_writers++;
pthread_mutex_unlock(&rw_mutex);

printf("Writer %ld is writing to the array (iteration %d): {", id, iter + 1);
for (int i = 0; i < 10; i++) {
shared_array[i] = rand() % 100;
printf("%d", shared_array[i]);
if (i < 9) {
printf(", ");
}
}
printf("}\n");

sleep(1);

pthread_mutex_lock(&rw_mutex);
active_writers--;
if (waiting_writers > 0) {
pthread_cond_signal(&can_write);
} else {
pthread_cond_broadcast(&can_read);
}
pthread_mutex_unlock(&rw_mutex);

sleep(1);
}

return NULL;
}

int main() {
srand(time(NULL));

for (int i = 0; i < 10; i++) {


shared_array[i] = i;
}

pthread_t readers[2], writers[2];

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


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

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


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

for (int i = 0; i < 2; i++) {


pthread_join(readers[i], NULL);
}

for (int i = 0; i < 2; i++) {


pthread_join(writers[i], NULL);
}

return 0;
}
Screenshot of result:

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