Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a) A file is to be shared among different processes, each of which has a unique

ID: 3698601 • Letter: A

Question

a) A file is to be shared among different processes, each of which has a unique number. The file can be accessed simultaneously by several processes, subject to the following constraint: The sum of all unique numbers associated with all the processes currently accessing the file must be less than n. Write a monitor to coordinate access to the file. When a signal is performed on a condition inside a monitor, the signaling process can either continue its execution or transfer control to the process that is signaled. How would the solution to the preceding exercise differ with these two different ways of performing signaling? b)

Explanation / Answer

for the given data wrote Program as:

#include <stdio.h>

int n=10;// file id number

int sumOfID=0;

int numOfProcessWaiting=0;

semaphore mutex=1;

semaphore okToAccess=0;

void get_access(int pid)

{

sem_wait(mutex);

while(sumOfID+pid > n)

   {

numOfProcessWaiting++;

sem_signal(mutex);

sem_wait(okToAccess);

sem_wait(mutex);

}

sumOfID += pid;

sem_signal(mutex);

}

void release_access(int pid)

{

int i;

sem_wait(mutex);

sumOfID -= pid;

for (i=0; i < numOfProcessWaiting;++i) {

sem_signal(okToAccess);

}

numOfProcessWaiting = 0;

sem_signal(mutex);

}

void Monitor()

{

   int id;

   for(id=0;id<20;id++)

   {

   get_access(id);

   }

    

   for(id=0;id<20;id++)

   {

   release_access(id);

   }

}

int main()

{

Monitor();

}