Homework Week-9
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;
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;
}
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));
return 0;
}
Screenshot of result: