You are on page 1of 4

Subject/Course

Platform Technologies

Name:
JEREMY A DANZALAN
Program/Course: PT101 BSIT-2A

Guide Question/s
1. Draw the process of Synchronization of the following . You can also provide screen
shots if needed.

a. Binary Semaphore

Code

Struct Semaphore{

enum value(0. 1);


}

wait (Semaphore mutex){

if (mutex.value == ){
mutex.value =0;
}
else {
processes.push(P);
sleep( );
}
}
Signal ( Semaphore mutex){
If (mutex.processes is empty){
Mutex.value =1;
}
Else {
process p= processes.pop( );
wakeup(p);
}
}

b. Counting Semaphore

Struct Semaphore( ){
int value;
Queue<process> processes;
}
wait (Semaphore s){
s.value -=1;
if (s.value< 0{
processes.push(p);
block( );
}
else {
return;
}
}

signal ( Semaphore s){


s.value += 1;
if(s.value >= 0) {
process p = processes.pop( );
wakeup(p);
}

Covered under Copyright Law of the 2 | Page


Philippines
All Rights Reserved @ OLSHCO
else{
return;
}
}

c. Monitor

monitor monitor name


{
Variables;
Conditional variables;

Procedure p1{….}
Procedure p2{….}

d. Monitor using Semaphore

wait ( mutex);

if (next_count >0)
signal (next);
else
signal(mutex);

Covered under Copyright Law of the 3 | Page


Philippines
All Rights Reserved @ OLSHCO
e. Dining-Philosophers Solution using Monitors

monitor DiningPhilosophers
{

Enum{THINGKING, HUNGRY, EATING} state[5];


Condition self[5];

void pickup (int i) {


state[i] = HUNGRY;
test(i);
if (state[i] != EATING)
self[i].wait( );
}

Void putdown(int i) {
state[i] =THINKING;
test((i +4) %5);
test((i+1) %5);
}

Void test(int i) {
if ((state[(i+ 4) %5] != EATING) &&
(state [i] == HUNGRY) &&
(state[(i +1) %5] != EATING)) {
state[i] = EATING;
self[i].signal( );
}
}

initialization_code( ) {
for ( int I =0; i <5; i++)
state[i] =THINKING;
}
}

Covered under Copyright Law of the 4 | Page


Philippines
All Rights Reserved @ OLSHCO

You might also like