CCP Answers
CCP Answers
8. Waiting state go to the runnable state when notified or their time is up.
9. In Round Robin algorithm, timeslices/quantum are assigned to each
process in equal portion.
10. One thread is waiting for another thread to finish transfer and vice versa.
They are stuck with each other and the program cannot continue. This is
called deadlock.
11. Briefly describe about the FOUR (4) conditions of having deadlock.
Deadlock is the event whereby there is no progress with either party
(threads) as they required each other resources. The conditions of having
deadlock are:
Serially reusable resources - the processes involved share resources which they use under
mutual exclusion.
Incremental acquisition - processes hold on to resources already allocated to them while
waiting to acquire additional resources.
No pre-emption - once acquired by a process, resources cannot be pre-empted (forcibly
withdrawn) but are only released voluntarily.
Wait-for cycle - a circular chain (or cycle) of processes exists such that each process holds a
resource which its successor in the cycle is waiting to acquire.
Implement the following case program using tryLock() method. Below are
three different class implemented in java.
Main.java
public class Main
{
public static void main(String arg[])
{
Chef c = new Chef();
Customer u1 = new Customer(1, c);
Customer u2 = new Customer(2, c);
Customer u3 = new Customer(3, c);
Customer u4 = new Customer(4, c);
Customer u5 = new Customer(5, c);
u1.start();
u2.start();
u3.start();
u4.start();
u5.start();
}
}
Chef.java
import java.util.Random;
public class Chef
{
void visit(Customer use)
{
System.out.println("Customer " + use.id + " visit Chef.");
try
{
Thread.sleep(new Random().nextInt(5) * 1000);
}
catch(Exception e)
{
}
System.out.println("Customer " + use.id + " left.");
}
}
Customer.java
public class Customer extends Thread
{
Chef c;
int id;
Answers
MainClass.java
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
u1.start();
u2.start();
u3.start();
u4.start();
u5.start();
}
}
Chef.java
import java.util.Random;
}
System.out.println("Customer " + use.id + "
left the restaurant.");
}
}
Customer.java
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;