Ceng 328 Lab Quiz 4

Q1. Assume that there is a file in the system and some users wants to read some data from it and some of them wants to change the content of this file. The description of the   problem is that a reader and writer should not access this file at the same time but many readers can access that file at the same time. This problem is called reader-writer problem as you know. The solution of that one is as follows:

Reader Writer

…… ……

start_read(); start_write();

…… …….

end_read(); end_write();

…… …….

Semaphore db=1, //database free or not

mutex=1; //used for managing critical region

int RC=0; //number of active readers

start_read(){

while(waiting_writer && RC!=0);

down(mutex);

RC++;

if(RC==1)

down(db);

up(mutex);

}

end_read(){

down(mutex);

RC--;

if(RC==0)

up(db);

up(mutex);

}

start_write(){

waiting_writer=1;

down(db);

waiting_writer=0;

}

end_write(){

up(db);

}

Here only problem is that if there is a large number of readers, writers has no chance to access file. This problem is called “starvation problem”. How can you solve this problem?

Q2. Assume that there is a bank and customer wants to make some banking processes. In this problem; every entering customer takes a number before making banking-process. Until that number is called, the customer waits. Whenever a banker is free, the next number is called. Write a procedure for the banker to execute and another one for the customer.

Ans.

Semaphore mutex, //controlling customer number

bank;//controlling banker

int customer=0;

customer(){

down(mutex);

customer++;

down(bank);

up(mutex);

}

banker(){

down(mutex);

customer--;

up(bank);

up(mutex);

}