Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Problem with CPU Task Scheduling Code

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem with CPU Task Scheduling Code

    Hello everyone. I have been using netbeans to help code and debug my program over the past month. Our assignment is due next on 15 Nov and I still can't get it to work properly.

    The input to the program goes like this:

    1 1 10
    1 1 0 20 5 50
    1 2 4
    1 1 0 20 5 50
    2 1 0 20 5 50

    This is two separate simulations. the first line states the number of cpus, the number of processes, and then the quantum for the cpus. The line/lines that follow are the information for each process. The process id, the process priority, the time to submit the process, the total cpu time needed, the time for each cpu cycle, and then the time for each IO cycle. The processes will start doing a cpu cycle and will alternate cpu/IO until the cpu cycle finishes the needed time.

    the first simulation seems to work when I run it, but any simulation with two or more processes does not return the correct result.

    Here is the code, thank you if you can help. I know it's long, but efficiency is not needed so I made it easier to understand which ended up being longer in length.

    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;
        }
    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problem with CPU Task Scheduling Code

    You've posted your assignment, your desire, and a lot of code, but not enough information, I think, to hone down where the problem might lie. Have you tried to debug this code yet, either with a debugger, or with a "poor-man's debugger" -- a lot of println statements?

    The key here will be I think for you to first isolate the error, and this will take some effort, but you will likely need to do this before we can help you.

    Also, I can't help but thinking that those "hills" of else if statements should be refactored into more readable and debuggable code.

    Edit: also there's a lot of unnecessary repetition in your code. You likely can refactor this code and make it smaller and easier for others to read and understand and for you to debug.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with CPU Task Scheduling Code

    Quote Originally Posted by curmudgeon View Post
    You've posted your assignment, your desire, and a lot of code, but not enough information, I think, to hone down where the problem might lie. Have you tried to debug this code yet, either with a debugger, or with a "poor-man's debugger" -- a lot of println statements?

    The key here will be I think for you to first isolate the error, and this will take some effort, but you will likely need to do this before we can help you.
    That's what I have trying to figure out. I think, the problem may be coming from startprocess function. I'll test it more tomorrow and see what I can find. Thanks.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Problem with CPU Task Scheduling Code

    Quote Originally Posted by smlrwd View Post
    That's what I have trying to figure out. I think, the problem may be coming from startprocess function. I'll test it more tomorrow and see what I can find. Thanks.
    Sounds good. good luck with this!

Similar Threads

  1. Problem with java program and CPU usage
    By Bahramudin in forum Java Theory & Questions
    Replies: 3
    Last Post: July 25th, 2012, 09:26 AM
  2. The problem with Java and CPU usage, need HELP
    By Bahramudin in forum Member Introductions
    Replies: 1
    Last Post: July 25th, 2012, 09:23 AM
  3. Scheduling Problem
    By pmg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 19th, 2011, 09:26 PM
  4. SIGAR to find CPU info-problem
    By ttsdinesh in forum Exceptions
    Replies: 7
    Last Post: October 4th, 2009, 10:33 AM
  5. application Task problem - updating JTree
    By idandush in forum AWT / Java Swing
    Replies: 2
    Last Post: June 18th, 2009, 03:15 AM