package prog2new;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Scanner;
public class Prog2new {
private static int CPUs;
private static int processes;
private static int quantum;
private static int[][] processTable;
final private static int processItems = 16;
private static int time;
final private static int pid = 0;
final private static int prio = 1;
final private static int submit = 2;
final private static int totCpu = 3;
final private static int CPUCycle = 4;
final private static int IOCycle = 5;
final private static int totalCpuUsed = 6;
final private static int totalIO = 7;
final private static int totalIdle = 8;
final private static int CPUInUse = 9;
final private static int running = 10;
final private static int ready = 11;
final private static int riq = 12;
final private static int blocked = 13;
final private static int not_yet_submitted = 14;
final private static int completed = 15;
private static PriorityQueue<Integer> prio1;
private static PriorityQueue<String> prio2;
private static PriorityQueue<String> prio3;
private static PriorityQueue<String> prio4;
private static PriorityQueue<String> prio5;
private static PriorityQueue<String> prio6;
private static PriorityQueue<String> prio7;
private static PriorityQueue<String> prio8;
private static PriorityQueue<String> prio9;
private static PriorityQueue<String> prio10;
public static void main(String[] args) {
getSimulation();
createQueues();
runSimulation();
}
private static void getSimulation() {
Scanner in = new Scanner(System.in);
CPUs = in.nextInt();
processes = in.nextInt();
processTable = new int[processes][processItems];
quantum = in.nextInt();
for(int i = 0; i < processes; i++){
for(int j = 0; j < 6; j++){
processTable[i][j] = in.nextInt();
}
}
for(int i = 0; i < processes; i++ ){
processTable[i][not_yet_submitted] = 1;
}
}
private static void runSimulation() {
time = 0;
while(true){
checkTime( time );
addToPrioQueue();
if( startProcesses() == 0 ){
int stillNS = 0;
for( int i = 0; i < processes; i++ ){
stillNS = stillNS + processTable[i][not_yet_submitted];
}
if( stillNS == 0 ){
finishRunningProcs();
break;
}
}
time++;
for( int i = 0; i < processes; i++ ){
if( processTable[i][running] == 1 ){
processTable[i][totalCpuUsed]++;
} else if ( processTable[i][ready] == 1 ){
processTable[i][totalIdle]++;
} else{
processTable[i][totalIO]++;
}
}
}
printResults();
}
private static void checkTime( int currentTime ) {
for( int i = 0; i < processes; i++ ){
if( processTable[i][submit] == currentTime ){
processTable[i][not_yet_submitted] = 0;
processTable[i][ready] = 1;
}
}
checkCompleted();
checkQuantumExpWithMoreCPU();
checkForMoveToIO();
checkFinishedIO();
checkPremp();
}
private static void addToPrioQueue() {
for( int i = 0; i < processes; i++ ){
if( processTable[i][ready] == 1 && processTable[i][riq] != 1 ){
insert( processTable[i][prio] , processTable[i][pid] );
processTable[i][riq] = 1;
}
}
}
private static void createQueues() {
int[] priorities = new int[11];
for( int i = 0; i < processes; i++ ){
priorities[ processTable[i][prio] ] = priorities[
processTable[i][prio] ] + 1;
}
if( priorities[1] > 0 ){
// prio1 = new PriorityQueue<>(1, new Comparator<String>() {
// @Override
// public int compare( String x, String y ){
// int first = Integer.parseInt(x);
// int second = Integer.parseInt(y);
// return first - second;
// }
// });
prio1 = new PriorityQueue<>(1);
}
if( priorities[2] > 0 ){
prio2 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
if( priorities[3] > 0 ){
prio3 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
if( priorities[4] > 0 ){
prio4 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
if( priorities[5] > 0 ){
prio5 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
if( priorities[6] > 0 ){
prio6 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
if( priorities[7] > 0 ){
prio7 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
if( priorities[8] > 0 ){
prio8 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
if( priorities[9] > 0 ){
prio9 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
if( priorities[10] > 0 ){
prio10 = new PriorityQueue<>(1, new Comparator<String>() {
@Override
public int compare( String x, String y ){
int first = Integer.parseInt(x);
int second = Integer.parseInt(y);
return first - second;
}
});
}
}
private static void insert( int priority, int PID ) {
switch( priority ){
case 1:
prio1.add( PID );
break;
case 2:
prio2.add( Integer.toString( PID ) );
break;
case 3:
prio3.add( Integer.toString( PID ) );
break;
case 4:
prio4.add( Integer.toString( PID ) );
break;
case 5:
prio5.add( Integer.toString( PID ) );
break;
case 6:
prio6.add( Integer.toString( PID ) );
break;
case 7:
prio7.add( Integer.toString( PID ) );
break;
case 8:
prio8.add( Integer.toString( PID ) );
break;
case 9:
prio9.add( Integer.toString( PID ) );
break;
case 10:
prio10.add( Integer.toString( PID ) );
break;
default:
System.err.println( "Error: Priority value for processes "
+ "needs to be between 1 and 10, not " + priority
+ ", as in process with PID " + PID);
}
}
private static int startProcesses() {
int startedPID;
boolean skip = false;
for( int i = 1; i <= CPUs; i++ ){
for( int j = 0; j < processes; j++ ){
int runningCPU = i;
if( runningCPU == processTable[j][CPUInUse] ){
skip = true;
}
}
if( !skip ){
if( prio1 == null || prio1.isEmpty() ){
if( prio2 == null || prio2.isEmpty() ){
if( prio3 == null || prio3.isEmpty() ){
if( prio4 == null || prio4.isEmpty() ){
if( prio5 == null || prio5.isEmpty() ){
if( prio6 == null || prio6.isEmpty() ){
if( prio7 == null || prio7.isEmpty() ){
if( prio8 == null || prio8.isEmpty() ){
if( prio9 == null ||
prio9.isEmpty() ){
if( prio10 == null ||
prio10.isEmpty() ){
return 0;
}else{
startedPID =
Integer.parseInt(
prio10.remove() );
startProcessInTable(
startedPID, i );
return 1;
}
}else{
startedPID = Integer.parseInt(
prio9.remove() );
startProcessInTable(
startedPID, i );
return 1;
}
}else{
startedPID = Integer.parseInt(
prio8.remove() );
startProcessInTable(
startedPID, i );
return 1;
}
}else{
startedPID = Integer.parseInt(
prio7.remove() );
startProcessInTable( startedPID, i );
return 1;
}
}else{
startedPID = Integer.parseInt(
prio6.remove() );
startProcessInTable( startedPID, i );
return 1;
}
}else{
startedPID = Integer.parseInt(
prio5.remove() );
startProcessInTable( startedPID, i );
return 1;
}
}else{
startedPID = Integer.parseInt( prio4.remove() );
startProcessInTable( startedPID, i );
return 1;
}
}else{
startedPID = Integer.parseInt( prio3.remove() );
startProcessInTable( startedPID, i );
return 1;
}
}else{
startedPID = Integer.parseInt( prio2.remove() );
startProcessInTable( startedPID, i );
return 1;
}
}else{
startedPID = prio1.remove();
startProcessInTable( startedPID, i );
return 1;
}
}
}
return 0;
}
private static void printResults() {
System.out.println( "Simulation # 1" );
System.out.println( "--------------" );
System.out.println( "Input:" );
System.out.printf( "\t%d CPU, %d process, quantum size = %d\n",
CPUs, processes, quantum );
for( int i = 0; i < processes; i++){
System.out.printf( "\tPID %d, prio = %d, submit = %d, totCPU = " +
"%d, CPU = %d, I/O = %d\n", processTable[i][pid],
processTable[i][prio], processTable[i][submit],
processTable[i][totCpu], processTable[i][CPUCycle],
processTable[i][IOCycle]);
}
System.out.println( "Output:" );
for( int i = 0; i < processes; i++ ){
System.out.printf( "\tPID %d completed execution at %d, turn around " +
"time = %d\n", processTable[i][pid],
getExecutionTime( i ), getTurnaroundTime( i ) );
}
System.out.printf( "\tAverage CPU idle time = %d (%d%%)\n\tAverage " +
"process turnaround time = %d", getAvgCPUIdle(),
getAvgCPUIdlePerc(), getAvgTurnaroundTime() );
}
private static int getExecutionTime( int pit ){
return processTable[pit][totalCpuUsed] + processTable[pit][totalIO] +
processTable[pit][totalIdle] + processTable[pit][submit];
}
private static int getTurnaroundTime( int pit ){
return getExecutionTime( pit ) - processTable[pit][submit];
}
private static int getAvgCPUIdle(){
int avgIdle = 0;
for( int i = 0; i < processes; i++ ){
avgIdle = avgIdle + processTable[i][totalIdle] +
processTable[i][totalIO];
}
avgIdle = avgIdle / processes;
return avgIdle;
}
private static int getAvgCPUIdlePerc(){
int avgIdle = getAvgCPUIdle();
int finishTime = 0;
for( int i = 0; i < processes; i++ ){
if( finishTime < getExecutionTime( i ) ){
finishTime = getExecutionTime( i );
}
}
return 100 * avgIdle / finishTime;
}
private static int getAvgTurnaroundTime(){
int avgTurnaround = 0;
for( int i = 0; i < processes; i++ ){
avgTurnaround = avgTurnaround + getTurnaroundTime( i );
}
avgTurnaround = avgTurnaround / processes;
return avgTurnaround;
}
private static void startProcessInTable(int startedPID, int CPU ) {
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == startedPID ){
processTable[i][ready] = 0;
processTable[i][riq] = 0;
processTable[i][running] = 1;
processTable[i][CPUInUse] = CPU;
break;
}
}
}
private static void finishRunningProcs() {
for( int i = 0; i < processes; i++ ){
if( processTable[i][completed] != 1 ){
finish( i );
}
}
}
private static void finish( int pit ){
int cpuStillNeeded;
int cpuUsed = processTable[ pit ][ totalCpuUsed ];
int totalCPUoa = processTable[ pit ][ totCpu ];
int cpuCycle = processTable[ pit ][ CPUCycle ];
int IOUsed = processTable[ pit ][ totalIO ];
int ioCycle = processTable[ pit ][ IOCycle ];
if( processTable[pit][running] == 1 ){
int whereICC = cpuUsed % quantum;
cpuUsed = cpuUsed + ( cpuCycle - whereICC );
if( cpuUsed == totalCPUoa ){
}else{
cpuStillNeeded = totalCPUoa - cpuUsed;
int cyclesStillNeeded = cpuStillNeeded / cpuCycle;
cpuUsed = cpuUsed + ( cyclesStillNeeded * cpuCycle );
IOUsed = IOUsed + ( cyclesStillNeeded * ioCycle );
if( cpuUsed != totalCPUoa ){
IOUsed = IOUsed + ioCycle;
cpuUsed = totalCPUoa;
}
}
}else if( processTable[pit][ready] == 1 ){
cpuStillNeeded = totalCPUoa - cpuUsed;
int cyclesStillNeeded = cpuStillNeeded / cpuCycle;
if( cyclesStillNeeded <= 1 ){
cpuUsed = totalCPUoa;
}else{
cyclesStillNeeded--;
cpuUsed = cpuUsed + cpuCycle;
cpuUsed = cpuUsed + ( cyclesStillNeeded * cpuCycle );
IOUsed = IOUsed + ( cyclesStillNeeded * ioCycle );
if( cpuUsed != totalCPUoa ){
IOUsed = IOUsed + ioCycle;
cpuUsed = totalCPUoa;
}
}
}else{
int IOCLeft = ioCycle - ( IOUsed % ioCycle );
IOUsed = IOUsed + IOCLeft;
cpuStillNeeded = totalCPUoa - cpuUsed;
int cyclesStillNeeded = cpuStillNeeded / cpuCycle;
if( cyclesStillNeeded <= 1 ){
cpuUsed = totalCPUoa;
}else{
cyclesStillNeeded--;
cpuUsed = cpuUsed + cpuCycle;
cpuUsed = cpuUsed + ( cyclesStillNeeded * cpuCycle );
IOUsed = IOUsed + ( cyclesStillNeeded * ioCycle );
if( cpuUsed != totalCPUoa ){
IOUsed = IOUsed + ioCycle;
cpuUsed = totalCPUoa;
}
}
}
processTable[ pit ][ totalCpuUsed ] = cpuUsed;
processTable[ pit ][ totalIO ] = IOUsed;
}
private static void checkCompleted() {
for( int i = 0; i < processes; i++ ){
if ( processTable[i][totalCpuUsed] == processTable[i][totCpu] ){
processTable[i][running] = 0;
processTable[i][completed] = 1;
}
}
}
private static void checkQuantumExpWithMoreCPU() {
for ( int i = 0; i < processes; i++ ){
if ( processTable[i][running] == 1 ){
if ( ( processTable[i][totalCpuUsed] % quantum ) == 0 &&
( processTable[i][totalCpuUsed] % CPUCycle ) != 0 ){
processTable[i][running] = 0;
processTable[i][ready] = 1;
processTable[i][CPUInUse] = 0;
}
}
}
}
private static void checkForMoveToIO() {
for( int i = 0; i < processes; i++ ){
if( processTable[i][running] == 1 ){
if( ( processTable[i][totalCpuUsed] % quantum ) == 0 &&
( processTable[i][totalCpuUsed] % CPUCycle ) == 0 ){
processTable[i][running] = 0;
processTable[i][blocked] = 1;
processTable[i][CPUInUse] = 0;
}
}
}
}
private static void checkFinishedIO() {
for ( int i = 0; i < processes; i++ ){
if( processTable[i][blocked] == 1 ){
if ( ( processTable[i][totalIO] % IOCycle ) == 0 ){
processTable[i][blocked] = 0;
processTable[i][ready] = 1;
}
}
}
}
private static void checkPremp() {
for( int i = 1; i <= CPUs; i++ ){
int runningPid = 0;
int runningPrio = 0;
for ( int j = 0; j < processes; j++ ){
if( processTable[j][CPUInUse] == i ){
runningPid = processTable[j][pid];
runningPrio = processTable[j][prio];
}
}
switch( runningPrio ){
case 1:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 2:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 3:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 4:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 5:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 6:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 7:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 8:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 9:
checkHigherQueues( runningPrio, runningPid, i );
break;
case 10:
checkHigherQueues( runningPrio, runningPid, i );
break;
}
}
}
private static void checkHigherQueues( int runningPrio,
int runningPid, int cpu ) {
if( runningPrio == 10 ){
check10( runningPid, cpu, runningPrio );
}else if ( runningPrio == 9 ){
check9( runningPid, cpu, runningPrio );
}else if ( runningPrio == 8 ){
check8( runningPid, cpu, runningPrio );
}else if ( runningPrio == 7 ){
check7( runningPid, cpu, runningPrio );
}else if ( runningPrio == 6 ){
check6( runningPid, cpu, runningPrio );
}else if ( runningPrio == 5 ){
check5( runningPid, cpu, runningPrio );
}else if ( runningPrio == 4 ){
check4( runningPid, cpu, runningPrio );
}else if ( runningPrio == 3 ){
check3( runningPid, cpu, runningPrio );
}else if ( runningPrio == 2 ){
check2( runningPid, cpu, runningPrio );
}else if ( runningPrio == 1 ){
check1( runningPid, cpu, runningPrio );
}
}
private static int check10(int runningPid, int cpu, int runningPrio) {
if ( runningPrio == 10 ){
if( prio10.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio10.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
if( prio10 == null || prio10.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check9(int runningPid, int cpu, int runningPrio) {
if ( runningPrio == 9 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0 ){
if( prio9.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio9.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
} else{
return 1;
}
}else{
if( prio9 == null || prio9.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check8(int runningPid, int cpu, int runningPrio) {
if( runningPrio == 8 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0){
int test9 = check9( runningPid, cpu, runningPrio );
if( test9 == 0 ){
if( prio8.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio8.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
if( prio8 == null || prio8.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check7(int runningPid, int cpu, int runningPrio) {
if( runningPrio == 7 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0){
int test9 = check9( runningPid, cpu, runningPrio );
if( test9 == 0 ){
int test8 = check8( runningPid, cpu, runningPrio );
if( test8 == 0 ){
if( prio7.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio7.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
if( prio7 == null || prio7.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check6(int runningPid, int cpu, int runningPrio) {
if( runningPrio == 6 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0){
int test9 = check9( runningPid, cpu, runningPrio );
if( test9 == 0 ){
int test8 = check8( runningPid, cpu, runningPrio );
if( test8 == 0 ){
int test7 = check7( runningPid, cpu, runningPrio );
if( test7 == 0 ){
if( prio6.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio6.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
if( prio6 == null || prio6.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check5(int runningPid, int cpu, int runningPrio) {
if( runningPrio == 5 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0){
int test9 = check9( runningPid, cpu, runningPrio );
if( test9 == 0 ){
int test8 = check8( runningPid, cpu, runningPrio );
if( test8 == 0 ){
int test7 = check7( runningPid, cpu, runningPrio );
if( test7 == 0 ){
int test6 = check6( runningPid, cpu, runningPrio );
if( test6 == 0 ){
if( prio5.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio5.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
if( prio5 == null || prio5.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check4(int runningPid, int cpu, int runningPrio) {
if( runningPrio == 4 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0){
int test9 = check9( runningPid, cpu, runningPrio );
if( test9 == 0 ){
int test8 = check8( runningPid, cpu, runningPrio );
if( test8 == 0 ){
int test7 = check7( runningPid, cpu, runningPrio );
if( test7 == 0 ){
int test6 = check6( runningPid, cpu, runningPrio );
if( test6 == 0 ){
int test5 = check5( runningPid, cpu, runningPrio );
if( test5 == 0 ){
if( prio4.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio4.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
if( prio4 == null || prio4.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check3(int runningPid, int cpu, int runningPrio) {
if( runningPrio == 3 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0){
int test9 = check9( runningPid, cpu, runningPrio );
if( test9 == 0 ){
int test8 = check8( runningPid, cpu, runningPrio );
if( test8 == 0 ){
int test7 = check7( runningPid, cpu, runningPrio );
if( test7 == 0 ){
int test6 = check6( runningPid, cpu, runningPrio );
if( test6 == 0 ){
int test5 = check5( runningPid, cpu, runningPrio );
if( test5 == 0 ){
int test4 = check4( runningPid, cpu, runningPrio );
if( test4 == 0 ){
if( prio3.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio3.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
if( prio3 == null || prio3.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check2(int runningPid, int cpu, int runningPrio) {
if( runningPrio == 2 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0){
int test9 = check9( runningPid, cpu, runningPrio );
if( test9 == 0 ){
int test8 = check8( runningPid, cpu, runningPrio );
if( test8 == 0 ){
int test7 = check7( runningPid, cpu, runningPrio );
if( test7 == 0 ){
int test6 = check6( runningPid, cpu, runningPrio );
if( test6 == 0 ){
int test5 = check5( runningPid, cpu, runningPrio );
if( test5 == 0 ){
int test4 = check4( runningPid, cpu, runningPrio );
if( test4 == 0 ){
int test3 = check3( runningPid, cpu, runningPrio );
if( test3 == 0 ){
if( prio2.isEmpty() ){
return 0;
}else{
int nextPid = Integer.parseInt( prio2.peek() );
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
if( prio2 == null || prio2.isEmpty() ){
return 0;
}else{
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
return 1;
}
}
}
private static int check1(int runningPid, int cpu, int runningPrio) {
if( runningPrio == 1 ){
int test10 = check10( runningPid, cpu, runningPrio );
if( test10 == 0){
int test9 = check9( runningPid, cpu, runningPrio );
if( test9 == 0 ){
int test8 = check8( runningPid, cpu, runningPrio );
if( test8 == 0 ){
int test7 = check7( runningPid, cpu, runningPrio );
if( test7 == 0 ){
int test6 = check6( runningPid, cpu, runningPrio );
if( test6 == 0 ){
int test5 = check5( runningPid, cpu, runningPrio );
if( test5 == 0 ){
int test4 = check4( runningPid, cpu, runningPrio );
if( test4 == 0 ){
int test3 = check3( runningPid, cpu, runningPrio );
if( test3 == 0 ){
int test2 = check2( runningPid, cpu, runningPrio );
if( test2 == 0 ){
if( prio1.isEmpty() ){
return 0;
}else{
int nextPid = prio1.peek();
if ( runningPid > nextPid ){
for( int i = 0; i < processes; i++ ){
if( processTable[i][pid] == runningPid ){
processTable[i][ready] = 1;
processTable[i][running] = 0;
processTable[i][CPUInUse] = 0;
}
}
}
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}else{
return 1;
}
}
return 0;
}
}