47.2k views
5 votes
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. 3. Exercise 6.29 (10 points) Your solution should use condition variables. Assume a broadcast() operation can be invoked on a condition variable c to resume all processes suspended on c. Hint: Your monitor should contain two functions: one function is called before a process accesses a file and the other function is called after a process accesses a file.

1 Answer

2 votes

Answer:

monitor fileSharer

{

enum {THINKING, WAITING, READING} state[N];

condition self[N];

int total;

void open(int i) {

state[i] = WAITING;

if (i + total >= N)

{ self[i].wait(); } // Leaves monitor

state[i] = READING;

total += i;

}

void close(int i) {

state[i] = THINKING;

total -= i;

// Can signal one waiting proc whose ID won't break bank.

for (int x = N - total - 1; x >= 0; x--) {

if (state[x] == WAITING) {

self[x].signal(); break;

}

}

}

initialization.code() {

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

state[i] = THINKING;

}

total = 0;

}

}

User Amir Keshavarz
by
5.2k points