A file is to be shared among different processes, each of which has a unique id
ID: 3735855 • Letter: A
Question
A file is to be shared among different processes, each of which has a unique id number and a priority level number. The file can be accessed simultaneously by several processes, subject to the following constraint: The process priority number must be greater than priority numbers all of the other processes that are currently accessing the file. Using pseudocode (see below), write a monitor to coordinate access to the file. A basic monitor is set up as follows:
Monitor name
{
//shared variables
REQUESTprocedure ( ){
}
RELEASEprocedure ( ){
{
. . .
//initialization code( ){
}
}
Explanation / Answer
Please find my answer.
Please rate my answer if it helped you.
Solution:
Monitor file_access
{
int sum_proc=0; /* Shared var that contains the sum of the process proc_ids currently accessing the file */
int wait_proc=0; /* Number of process waiting process on the semaphore ok_access */
semaphore mutex=1; /* Semaphore variable ;) */
semaphore ok_access=0; /* The synchronization semaphore */
request(int proc_id)
{
sem_wait(mutex);
while(sum_proc+proc_id > n) {
wait_proc++;
sem_signal(mutex);
sem_wait(ok_access);
sem_wait(mutex);
}
sum_proc += proc_id;
sem_signal(mutex);
}
release(int proc_id)
{
int i;
sem_wait(mutex);
sum_proc -= proc_id;
for (i=0; i < wait_proc;++i) {
sem_signal(ok_access);
}
wait_proc = 0;
sem_signal(mutex);
}
}
main()
{
request(proc_id);
do_stuff();
release(proc_id);
}
Explaination:
1.request() method will check if your sum of process+proc_id = n. while the number > n, then the
threads should wait on a condition lock. If it is less than current count, it will increment the current count and return the mutex.
2.Release () wakes up all waiting processes. The process that are awake tries to get the nutex. If they wont , they go back to sleep.
But it is not ok to wake up the first wait_proc process in this problem because process may have too large a value of id.
And there maybe a chance that more than one process may be eligible to go in(in case of small id) when one process release access.
3.wait_proc variable is set to 0 in release() after the for loop.This will avoid any unnecessary signals from subsequent release accesses.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.