Parallel Algorithm
====================================
import java.util.*;
import java.io.*;
import jpb.*;
public class ffc
{
static final int NUMBER_OF_JOBS = 5;
public static void main (String[] arg)
{
String temp;
int i = 0,n=0,count = 0, quantum = 12;
double t = 0.0,totalWaitingTime=0.0;
long[] burstTimeRate = new long[NUMBER_OF_JOBS];//How many quantum does a
process use?
String[] jobList = new String[ NUMBER_OF_JOBS ];
double[] queueWaitingTime = new double[NUMBER_OF_JOBS];//waitingTime of each
process is in the queue.
double[] waitingTime = new double[NUMBER_OF_JOBS];
double[] burstTime = new double[NUMBER_OF_JOBS];
double[] arrivingTime = new double[NUMBER_OF_JOBS];
queueWaitingTime[0] = 0;
arrivingTime[0] = 0;
Random generator = new Random();
for(i = 0; i < NUMBER_OF_JOBS;i++)
{
jobList[i] = readEntry("Input the job you want to run:(please enter letters) ");
burstTime[i] = Math.round(generator.nextDouble() * 20*1000)/1000;
//CPU burst time is between 0~20.
if( i >= 1)
arrivingTime[i] = Math.round(generator.nextDouble()*4000)/1000;
//get a radom double number between 0~5,and set the arrivingTime much
//less than the CPU burst time
}
System.out.println("\n\nJob\tBurstTime\tArrivingTime");
for(i=0; i<NUMBER_OF_JOBS; i++)
System.out.println(jobList[i]+"\t"+burstTime[i] + "\t\t"+arrivingTime[i]);
//for the first in first serve algorithm(FIFS)
System.out.println();
System.out.println("----------now ready to demonstrate FFS----------");
System.out.println();
System.out.println("in process of calculation.....\b");
try{
Thread.sleep((long) (Math.random()*4000));
}catch(InterruptedException e) {}
for (i = 0; i < NUMBER_OF_JOBS; i++ )
{
if(i==0)
waitingTime[i] = 0;
else
{
if ( arrivingTime[i] > burstTime[i-1]+waitingTime[i-1] )
waitingTime[i] = arrivingTime[i];
else
waitingTime[i] = waitingTime[i-1] + burstTime[i-1];
}
}
for (i=0;i < NUMBER_OF_JOBS; i++ )
totalWaitingTime += waitingTime[i];
Shared shared = new Shared();
CustomThread thread1 = new CustomThread(shared, "one");
CustomThread thread2 = new CustomThread(shared, "two");
CustomThread thread3 = new CustomThread(shared, "three");
CustomThread thread4 = new CustomThread(shared, "four");
CustomThread thread5 = new CustomThread(shared, "five");
try {
thread1.join();
thread2.join();
thread3.join();
thread4.join();
thread5.join();
} catch(InterruptedException e) {}
System.out.println( "The average waiting time of the FFS job list is: "+totalWaitingTime/5);
System.out.println();
System.out.println("----------This is the ending of the FFS
simulation----------");
System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n");
int select = -1;
do{
System.out.print("please press Enter to go to shortest-job-first demostration");
try{
select = System.in.read();
}catch(Exception e){System.out.println("program shut down due to the wrong
input");}
}while (select != '\n');
//For the shortest job first round robin algorithm
System.out.println();
System.out.println("----------now is the SJF simulation----------");
for( i = 0; i < NUMBER_OF_JOBS; i++)
{
for( int j = i+1; j < NUMBER_OF_JOBS; j++)
{
if(burstTime[i] > burstTime[j])
{
temp = jobList[i];
jobList[i] = jobList[j];
jobList[j] = temp;
t = arrivingTime[i];
arrivingTime[i] = arrivingTime[j];
arrivingTime[j] = t;
t = burstTime[i];
burstTime[i] = burstTime[j];
burstTime[j] = t;
}//end of if ocnidtion,swap jobLists, arrivingTime, and burstTime.
}//end of the inside for loop
}//sorting the jobList array ,make the burst time from the smallest one to the
largest one.
System.out.println("in process of calculation.....");
try{
Thread.sleep((long) (Math.random()*3000));
}catch(InterruptedException e) {}
System.out.println();
System.out.println( "Right now job list and their bursttimes become: " );
System.out.println("Job\tBurstTime\tArrivingTime");
for( i = 0; i <NUMBER_OF_JOBS; i++ )
System.out.println( jobList[i]+ "\t" + burstTime[i] + "\t\t" + arrivingTime[i]
);
System.out.println();
//print out the jobList after sorted.
for(i=0;i<NUMBER_OF_JOBS;i++)
burstTimeRate[i]= Math.round( burstTime[i] / quantum - 0.5 ) * quantum;
//calculate hwo many quantum does each process use,the result will use later.
//Because burstTime will be change after execution.
System.out.println("Job\tqueueWaitingTime");
while(burstTime[NUMBER_OF_JOBS-1] != 0)
{
count = 0;//If count is equal to 0,means the for loop havn't start or already
finished .
for ( i = 0; i< NUMBER_OF_JOBS ; i++ )
{
if (burstTime[i] != 0)
{
count++;//if count is equal to 1,that means we have to add previous
//waitingTime to the new coming job(update the new one's waitingTime).
if (burstTime[i] > quantum)
//Testing if the job is finished,if it is finished,processor will not run it
again.
{
if(i > 0 && i< NUMBER_OF_JOBS-1)
{
if(n==0)//n=0 is means that the first round job gets into processor.
{
if ( arrivingTime[i] > queueWaitingTime[i])
queueWaitingTime[i] = arrivingTime[i];
queueWaitingTime[i+1] = queueWaitingTime[i] + quantum;
//calcualte the waitingTime of the next job.
}
else
{
if (count==1)
queueWaitingTime[i] = queueWaitingTime[NUMBER_OF_JOBS-1]+quantum;
queueWaitingTime[i+1] = queueWaitingTime[i] + quantum;
}
}
if (i ==0)
{
if(n==0)
{
queueWaitingTime[i] = arrivingTime[i];
if(i < NUMBER_OF_JOBS-1)
queueWaitingTime[i+1] = queueWaitingTime[i] + quantum;
}
else
{
if(count ==1)
queueWaitingTime[i] = queueWaitingTime[NUMBER_OF_JOBS-1] + quantum;
queueWaitingTime[i+1] = queueWaitingTime[i]+quantum;
}
}
burstTime[i] = burstTime[i] - quantum;
System.out.println(jobList[i]+" "+queueWaitingTime[i]);
}
else//burstTime is less than quantum.
{
if(i > 0 && i < NUMBER_OF_JOBS-1)
{
if(n==0)
{
if ( arrivingTime[i] > queueWaitingTime[i] )
queueWaitingTime[i] = arrivingTime[i];
queueWaitingTime[i+1] = queueWaitingTime[i] + burstTime[i];
//calcualte the waitingTime of the next job.
}
else
{
if (count==1)
queueWaitingTime[i] = queueWaitingTime[NUMBER_OF_JOBS-1] + quantum;
queueWaitingTime[i+1] = queueWaitingTime[i]+burstTime[i];
}
}
if(i==0)
{
if (n==0)
{
queueWaitingTime[i] = arrivingTime[i];
if (i<NUMBER_OF_JOBS-1)
queueWaitingTime[i+1] = queueWaitingTime[i] + burstTime[i];
}
else
{
if (count==1)
queueWaitingTime[i] = queueWaitingTime[NUMBER_OF_JOBS-1]+ quantum;
queueWaitingTime[i+1] = queueWaitingTime[i] + burstTime[i];
}
}
burstTime[i] = 0;
//because busrtTime is less than quantum,
//that means this process will be finished,so after execute process.
//Set the burstTime =0;
System.out.println(jobList[i]+ "\t" + queueWaitingTime[i]);
try{
Thread.sleep((long) (queueWaitingTime[i]*500));
}catch(InterruptedException e) {}
}//end of inside if condition
}//end of outside if condition
}//end of for loop
n++;
}//end of while loop
System.out.println();
for(i = 0; i < NUMBER_OF_JOBS; i ++)
{
if (burstTimeRate[i] > 0)
waitingTime[i] = queueWaitingTime[i] - burstTimeRate[i];
else
waitingTime[i] = queueWaitingTime[i];
//if jobs burstTime is less than quantum time,so the waitingTime
//is equal to the waiting time in the queue.
System.out.println(jobList[i]+ " waitingTime is: "+waitingTime[i]);
}//get the waitingTime of every job.
System.out.println();
totalWaitingTime = 0.0;
for(i = 0;i<NUMBER_OF_JOBS;i++)
totalWaitingTime += waitingTime[i];
System.out.println("the average waiting time is: "+ totalWaitingTime/NUMBER_OF_JOBS);
System.out.println("----------This is the ending of the SJF
simulation----------");
}//end of main program
public static String readEntry(String prompt)
{
try{
StringBuffer buffer = new StringBuffer();
System.out.print(prompt);
System.out.flush();
int c = System.in.read();
while(c != '\n' && c!= -1)
{buffer.append((char) c);
c= System.in.read();
}
return buffer.toString().trim();
} catch (IOException e)
{
return " ";
}
}//the end of readEntry fuction,get six processes.
}//end of the class
class CustomThread extends Thread
{
Shared shared;
public CustomThread(Shared shared, String string)
{
super(string);
this.shared = shared;
start();
}
public void run() {
synchronized(shared) {
shared.doWork(Thread.currentThread().getName());
}
}
}
class Shared
{
void doWork(String string)
{
System.out.println("Starting " + string);
try{
Thread.sleep((long)(Math.random()*2000));
}catch(InterruptedException e) {}
System.out.println("Ending " + string);
}
}