148k views
4 votes
Provide relevant parts of C-like codes for processes X1, X2, X3, X4 and X5 in the following synchronization problem. Each process has a special (synchronization) point in its code, and:- Process X1 may cross its synchronization point unconditionally- Process X4 has to wait for processes X3 and X1 to cross or reach their synchronization points, and only then X4 may cross its synchronization point,- Process X2 has to wait for process X1 to cross or reach its synchronization point, and only then X2 may cross its synchronization point,- Process X3 has to wait for process X4 to cross or reach its synchronization point, and only then X3 may cross its synchronization point,- Process X5 has to wait for processes X1, X3 and X4 to cross their synchronization points, and only then X5 may cross its synchronization point.Assume that each process will cross its synchronization point only once.

User Leggetter
by
4.8k points

1 Answer

2 votes

Answer:

import java.util.concurrent.Semaphore;

public class synchronization implements Runnable {

Semaphore b11 = new Semaphore(1);

@Override

//run method

public void run() {

boolean flvalue=false;

System.out.println("Started");

while(!flvalue) {

System.out.println("Thread is running");

}

System.out.println("Stopped");

}

//main method

public static void main(String args[]) {

final synchronization t11 = new synchronization();

Thread x1=new Thread(){

@Override

//run method

public void run(){

t11.mutualExclusion();

}

};

Thread x2=new Thread(){

@Override

//run method

public void run(){

t11.mutualExclusion();

}

};

Thread x3=new Thread(){

@Override

//run method

public void run(){

t11.mutualExclusion();

}

};

Thread x4=new Thread(){

@Override

//run method

public void run(){

t11.mutualExclusion();

}

};

Thread x5=new Thread(){

@Override

//run method

public void run(){

t11.mutualExclusion();

}

};

x1.start();

x2.start();

x3.start();

x4.start();

x5.start();

}

private void mutualExclusion() {

try {

b11.acquire();

//mutual value

String tn=Thread.currentThread().getName();

String numberOy= tn.replaceAll("[^0-9]", "");

int num=Integer.parseInt(numberOy)+1;

System.out.println("Thread "+num + " is running");

Thread.sleep(100);

} catch (InterruptedException ie11) {

ie11.printStackTrace();

} finally {

b11.release();

String tn11=Thread.currentThread().getName();

String numberOy= tn11.replaceAll("[^0-9]", "");

int numval=Integer.parseInt(numberOy)+1;

System.out.println("Thread "+numval + " completed...!!!");

}

}

}

Step-by-step explanation:

User Moin
by
4.9k points