Can you please help me with the PSEUDO CODE for this problem? Thank you in advan
ID: 3831402 • Letter: C
Question
Can you please help me with the PSEUDO CODE for this problem? Thank you in advance.
Processes 0, 1, 2, 3, 4 are executing concurrently. Process 1, Process 2, Process 3 and Process 4 must start their execution after execution of Process 0 ends. There is no enforcement of order between the executions of Process l, Process 2, Process3 and Process 4 a) Implement the required synchronization using minimum number of binary semaphores. Give the initial value of each semaphore. b) Implement the required synchronization using minimum number of counting semaphores. Give the initial value of each semaphore.Explanation / Answer
a)
/* shared memory */
int in = 0, out = 0, count = 0;
int buf[4]; int ewait = 0 ,nwait = 0;
semaphore n = 0, e = 0, s = 1;
append(int v)
{
wait(s);
if (count == 4)
{
ewait++;
signal(s);
wait(e);
ewait--;
}
buf[in] = v;
in = (in+1)%4;
count = count+1;
if (nwait > 0) signal(n);
else signal(s);
}
int take()
{
int v;
wait(s);
if (count == 0)
{
nwait++;
signal(s);
wait(n);
nwait--;
}
v = buf[out];
out = (out+1)%4;
count = count-1;
if (ewait > 0) signal(e);
else signal(s);
return v;
}
b)
typedef struct
{
int count;
queue q;
} SEMAPHORE;
P(s)
SEMAPHORE *s;
{
while (true)
{
Disable interrupts;
if (s->count > 0)
{
s->count-= 1;
Enable interrupts;
return;
}
Add process to s->q
Enable interrupts;
Redispatch
}
}
V(s)
SEMAPHORE *s;
{
Disable interrupts;
if (s->q empty)
{
s->count += 1;
}
else
{
Remove first process from s->q
Wake it up
}
Enable interrupts;
}