edited by
1,821 views
7 votes
7 votes

A concurrent system consists of $3$ processes using a shared resource $R$ in a non-preemptible and mutually exclusive manner. The processes have unique priorities in the range $1 \dots 3$, $3$ being the highest priority. It is required to synchronize the processes such that the resource is always allocated to the highest priority requester. The pseudo code for the system is as follows.

Shared data

mutex:semaphore = 1:/* initialized to 1*/

process[3]:semaphore = 0; /*all initialized to 0 */

R_requested [3]:boolean = false; /*all initialized to flase */

busy: boolean = false; /*initialized to false */

Code for processes

begin process
my-priority:integer;
my-priority:=___; /*in the range 1..3*/
repeat
    request_R(my-priority);
    P (proceed [my-priority]);
    {use shared resource R}
    release_R (my-priority);
forever
end process;

Procedures

procedure request_R(priority);
P(mutex);
if busy = true then
    R_requested [priority]:=true;
else
begin
    V(proceed [priority]);
    busy:=true;
end
V(mutex)

Give the pseudo code for the procedure release_R.

edited by

3 Answers

Best answer
8 votes
8 votes
procedure release_R(priority)
begin
    P(mutex); //only one process must be executing the following part at a time
    R_requested[priority] = false; //this process has requested, 
//allocated the resource and now finished using it
    for (i = 3 downto 1)//starting from highest priority process
    begin
        if R_requested[i] then
        begin
            V(proceed[i]);//Process i is now given access to resource
            break;
        end
    end
    if (!R_requested[0] && !R_requested[1] && !R_requested[2]) then 
        busy = false;//no process is waiting and so next incoming resource 
        //can be served without any wait
    V(mutex); //any other process can now request/release resource
end
0 votes
0 votes
procedure release_R(priority)
begin
    P(mutex);
    request_R[priority] = false; //this should be false already, just to be safe
    busy = false;
    for (i = 3 downto 1)
    begin
        if R_requeted[i] then
        begin
            R_requested[i] = false;
            busy = true;
            V(proceed[i]);
            break;
        end
    end
    V(mutex);
end

Please verify the solution, I may be wrong.

0 votes
0 votes
procedure release_R(priority)
begin
    P(mutex);
    request_R[priority] = false; //this should be false already, just to be safe
    for (i = 3 downto 1)
    begin
        if R_requeted[i] then
        begin
            V(proceed[i]);
            break;
        end
    end
    if (request_R[0]==request_R[1]==request_R[2]==false) then 
    busy = false; 
    V(mutex);
end

Related questions

50 votes
50 votes
13 answers
1
25 votes
25 votes
6 answers
4
Kathleen asked Sep 29, 2014
17,431 views
An operating system contains $3$ user processes each requiring $2$ units of resource $R$. The minimum number of units of $R$ such that no deadlocks will ever arise is$3$$...