Programming

Cpu Scheduling algorithm’s Implementation in Java

CPU scheduling policies like FCFS (first come first serve) ,SJF with preemptive as well as Non preemptive,Round Robin,Priority scheduling are implemented in Java language:

Download Its Perfect Copy in   .txt Format

Download Steps to Run this Java code in .doc file

 


Download Its OUTPUT Copy in .doc Word Format

Instruction to run the below program in windows OS:

1.save the program as rectangle.java in bin folder of jdk.
2.compile the program using ” javac rectangle.java “in cmd prompt
3.run the file using appletviewer rectangle.java
4.give input values in cmd prompt and finally observe Gantt Chart
in appletviewer window.

INPUT is: rectangle.java :

/* Program starts here: */

Advertisements

45 thoughts on “Cpu Scheduling algorithm’s Implementation in Java”

    1. Bro.. IF you have got the total correct version of this program,then please share with us.as I know it contains some errors,but this program had done what we needed at the time when its first thought of.

      1. what do you mean bro..!?! it will not run even though I run it using textpad and same result in the cmd command.. what I gonna do bro.. can you help me bro..?! and I am going to translate it in netbeans IDE… Please help me bro… share your knowledge…

        1. Bro !!! thank you very much bro… your the man!!! “The Java MAN!!” I’ve run it already !! Thanks to you bro… !!! Ahmm… I just one favor last bro… can you send me the necessary explanations about the program bro..on how it flows… and to debug it..?!? you can just comment it bro.. in your program… Once again, Thank you very… very…very… much my friend!!! (^_^)

          1. welcome Bro,as far as commenting help on this program,I will update it later,so for now,please read your theory on cpu scheduling well,so that above code will be easily understood by you.

            1. okey bro… Hehehehehe… ahmmm,, bro this is my project bro in OS and finals is fast approaching bro… (T_T) but I will try it bro, the main thing that I got puzzled is that array2 passsing of elements which very critical in understanding how the program works … so I think I will just have do it in my own… Hehehehee.. but you have time … please help me bro… Thank you as always… Heheheheh…

    1. At some point in CODE,I tried this as:
      [
      System.out.print(“Arrival Time for P”+i+” :”);
      pro[i][2]=Integer.parseInt(obj.readLine());
      ]

      can you please scan through code,to catch above code Line,where I accepted input as Arrival time.

  1. Hey Sir. What am i Gonna do now? its not running in my Eclipse . this is my runner. i dont know how to run it in CMD. please tell me how thanks 🙂

  2. for(int j=i+1;jpro[j][2]) ??

    this same for loop error is in most of the cases!
    can this be explained please?
    how long will this loop?
    giving me syntax error, as what is jpro[j][2]?

    thank u!

  3. Write a C, C++ or java program that will implement the following scheduling algorithm:

    Shortest Job First (Non-Preemptive and Preemptive)

    The program should be able to compute the average waiting time & the average turnaround time for the system.

    This is only my exercise for my OS, not a tutorial or quiz or assignment. i saw ur code is very long and i still not understand. Can u send me ur simple code that can make me more understand and i can use it for my advance exercise? hope u can help me.. thanks alot.

    1. If you are copying this program as it is,then for your requirement you only need to run CASE 2 and CASE 3 in switch case block. So I would suggest you first try to construct required code from the code provided on this page as reference,because I would take some time to send your required program.I also learned like this,using other programmers code as start point to build my own according to my requirements. Good Luck.

  4. i wanted to ask another thing, i want arrival time for each case, and i/o burst as inputs also, then how do i implement? can u give some idea? or some code example? that could help 🙂

    thank u so much 🙂

    p.s. – yea i downloaded the file, and it was perfect 🙂 thanks! 🙂

    1. @ Ojas, if you keenly observe the code you will see Arrival Time code and Burst time as INputs also. ANd I would suggest,try to work on your requirements of logic on paper first ,and I’m sure you will get a better logic to solve this problem ,better than what published here.

      1. yea, sorry my bad, i saw the arrival time in there, but was wondering about the i/o burst, as cpu burst is taken, and i can work on it 🙂
        thank u, ill put some logic first!
        and if i have doubt, i can call for ur help 🙂
        thank u again! 🙂

  5. Here’s the working version… I have edited it my needs, in particular, got rid of the image displaying the drawn rectangles, found the pop-up more annoying than useful.

    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.util.*;
    import java.lang.*;

    /*
    Dmitri
    CS 121 Operating Systems
    */

    public class FinalAssignment
    {
    public static void main(String[] args)
    {
    try
    {

    int apw1[];
    int p[];
    int i=0,n,n1;
    boolean gonethrough = false;

    BufferedReader obj=new BufferedReader(new InputStreamReader (System.in));

    System.out.println(“ENTER no. of processes:”);
    n=Integer.parseInt(obj.readLine());

    int ch;
    int bt[]=new int[n];

    do{

    System.out.println(“MENU FOR CPU SCHEDULING”);
    System.out.println(“1.FCFS method”);
    System.out.println(“2.SHORTEST JOB FIRST method(non-preemptive)”);
    System.out.println(“3.SHORTEST JOB FIRST method(preemptive)”);
    System.out.println(“4.ROUND ROBIN method”);
    System.out.println(“5.PRIORITY method (non-preemptive)”);
    System.out.println(“6.PRIORITY method (preemptive)”);
    System.out.println(“7.EXIT”);
    System.out.println(“ENTER YOUR CHOICE”);

    ch=Integer.parseInt(obj.readLine());

    if(ch==7){
    System.exit(0);
    }

    switch(ch)
    {
    // —————————————————————————————————————————————————————-
    case 1:

    {

    for(i=0; i<n; i++)
    {System.out.println("ENTER burst time for each process: p"+(i+1));
    bt[i]=Integer.parseInt(obj.readLine());
    }
    n1=n;
    int turn[]=new int[n];
    apw1=new int[n+1];
    apw1[0]=0;
    float t=0;
    float tu=0;
    p=new int[n];
    for( i=1;i<n;i++)
    apw1[i]=bt[i-1]+apw1[i-1];

    System.out.println("—- Individal Waiting TIme —-");
    for( i=0;i<n;i++)
    {
    System.out.println("Process p"+(i+1)+": "+apw1[i]+" ");
    }

    // for(i=0;i<n;i++)
    // {
    // p[i]=i+1;
    // System.out.print(p[i]);
    // }

    for( i=0;i<n;i++)
    t+=apw1[i];
    System.out.println("\n\n—- Average Waiting —-");
    float avg=t/n;
    System.out.println("average waiting: "+avg);

    /*for( i=0;i
    System.out.println("response time for process p"+(i+1)+"is"+apw1[i]+" ");*/
    System.out.println("\n\n—- Turn-around Time —-");
    for( i=0;i<n;i++)
    {
    turn[i]=bt[i]+apw1[i];
    System.out.println("Process p"+(i+1)+": "+turn[i]+" ");
    }

    for( i=0;i<n;i++)
    tu+=turn[i];
    System.out.println("\n\n—- Average Turn-around Time —-");
    float avg1=tu/n;
    System.out.println("average turn-around time: "+avg1);

    //paint(g);
    }
    gonethrough = true;
    break;
    // —————————————————————————————————————————————————————-
    // —————————————————————————————————————————————————————-
    case 2:
    {
    for( i=0;i<n;i++)
    {System.out.println("ENTER burst time for each process: p"+(i+1));
    bt[i]=Integer.parseInt(obj.readLine());
    }
    n1=n;
    int temp;
    float t=0;
    int turn[]=new int[n];
    apw1=new int[n+1];
    apw1[0]=0;
    p=new int[n];
    int sj[]=new int[n];
    float tu=0;

    for( i=0;i<n;i++)
    sj[i]=bt[i];

    for( i=0;i<n;i++)
    for(int j=i+1;jsj[j])
    {
    temp=sj[i];
    sj[i]=sj[j];
    sj[j]=temp;
    }

    for( i=0;i<n;i++)
    for(int j=0;j<n;j++)
    if(sj[i]==bt[j])
    p[i]=j+1;

    for(i=0;i<n;i++)
    apw1[i+1]=sj[i]+apw1[i];

    System.out.println("—- Individual Waiting Time —-");
    for( i=0;i<n;i++)
    {
    System.out.println("Process p"+p[i]+": "+apw1[i]+" ");
    }

    System.out.println("\n\n—- Average Waiting Time —-");
    for( i=0;i<n;i++)
    t+=apw1[i];
    float avg=t/n;
    System.out.println("Average Waiting Time: "+avg);

    /*for( i=0;i
    System.out.println("response time for process p"+p[i]+"is"+apw1[i]+" ");*/

    System.out.println("\n\n—- Turnaround Time —-");

    for( i=0;i<n;i++)
    {
    int k=p[i];
    turn[i]=bt[k-1]+apw1[i];
    System.out.println("Process p"+p[i]+": "+turn[i]+" ");
    }

    for( i=0;i<n;i++)
    tu+=turn[i];

    System.out.println("\n\n—- Average Turnaround Time —-");

    float avg1=tu/n;
    System.out.println("Average Turnaround TIme: "+avg1);

    // paint(g);

    }
    gonethrough = true;
    break;
    // —————————————————————————————————————————————————————-
    // —————————————————————————————————————————————————————-
    // —————————————————————————————————————————————————————-
    case 3:
    {

    int total=0,k=0,co=0,small=999,sp=0,sp1=0,x=0,count=0;
    int pro[][]=new int[n][3];
    int wt[]=new int[n];
    int wt1[]=new int[n];
    int bt1[]=new int[n];
    int tt[]=new int[n];
    apw1=new int [20];
    p=new int[20];

    for(i=0;i<n;i++)
    {
    pro[i][0]=i;
    System.out.print("Burst Time for p"+i+": ");
    pro[i][1]=Integer.parseInt(obj.readLine());
    total+=pro[i][1];
    System.out.print("Arrival Time for p"+i+": ");
    pro[i][2]=Integer.parseInt(obj.readLine());
    }
    for(i=0;i<n;i++)
    wt1[i]=pro[i][2];

    for(i=0;i<n;i++)
    bt1[i]=pro[i][1];

    for(i=0;i<n;i++)
    for(int j=i+1;jpro[j][2])
    {
    int temp[]=pro[i];
    pro[i]=pro[j];
    pro[j]=temp;
    }

    for(i=1;i<=total;i++)
    {
    small=999;
    for(int j=co;j=pro[j][2])
    co++;
    for(int j=0;jpro[j][1]&&pro[j][1]!=0)
    {
    small=pro[j][1];
    sp=pro[j][0];
    sp1=j;
    }
    }
    if(p[x]==sp)
    {
    apw1[x+1]++;

    }
    else
    {
    x++;
    p[x]=sp;
    apw1[x+1]=apw1[x];
    apw1[x+1]++;
    count++;
    }

    pro[sp1][1]-=1;
    if(pro[sp1][1]==0)
    tt[sp1]=i;
    for(int j=0;j<n;j++)
    {
    if(pro[j][1]!=0&&j!=sp)
    wt[j]+=1;
    }
    k++;
    }
    for(i=0;i<n;i++)
    for(int j=i+1;jpro[j][0])
    {
    int temp[]=pro[i];
    pro[i]=pro[j];
    pro[j]=temp;
    int tem=wt[i],tem1=tt[i];
    wt[i]=wt[j];
    wt[j]=tem;
    tt[i]=tt[j];
    tt[j]=tem1;

    }

    System.out.println();

    for(int m=0;m<=count;m++)
    p[m]+=1;

    double awt=0.0,att=0.0;

    System.out.println("\n\n—- Waiting Time —-");

    System.out.println();
    for (i = 0; i<n;i++)
    {
    int temp = wt[i]-wt1[i];
    if(temp<0){
    temp=0;
    }
    System.out.println("Process p"+(i+1)+": "+temp+" ");
    awt+=(wt[i]-wt1[i]);
    att+=(wt[i]-wt1[i]+bt1[i]);
    }

    System.out.println("\n\n—- Turnaround Time —-");

    for (i = 0; i<n;i++)
    {
    System.out.println("Process p"+(i+1)+": "+(wt[i]-wt1[i]+bt1[i])+" ");
    }
    System.out.println("\n\n—- Average Waiting Time —-");
    System.out.println("Average waiting time: "+(awt/n));
    System.out.println("\n\n—- Average Turnaround Time —-");
    System.out.println("Average turnaround time: "+(att/n));
    n1=count+1;
    // paint(g);

    }
    gonethrough = true;
    break;
    // —————————————————————————————————————————————————————-
    // —————————————————————————————————————————————————————-
    // —————————————————————————————————————————————————————-
    // —————————————————————————————————————————————————————-
    case 4:
    {
    for( i=0;i<n;i++)
    {System.out.println("ENTER burst time for each process: p"+(i+1));
    bt[i]=Integer.parseInt(obj.readLine());
    }
    int ro[]=new int[n];
    apw1=new int[20];
    apw1[0]=0;

    int sq[]=new int[20];

    int temp;
    float t=0;
    float tu=0;
    int apw[]=new int[n];
    int bttime=0;
    int turn[]=new int[n];
    int f=0;

    for(i=0;i=ts)
    {
    ro[i]=ro[i]-ts;
    p[k]=i+1;
    k++;
    i++;
    temp=ts;
    sq[k]=sq[k-1]+temp;
    turn[i-1]=sq[k];

    f=0;
    for(int m=0;m0 && ro[i]>0)
    {
    temp=ro[i];
    ro[i]=0;
    p[k]=i+1;
    i++;
    k++;
    sq[k]=sq[k-1]+temp;
    turn[i-1]=sq[k];

    f=0;
    for(int m=0;m<n;m++)
    f+=ro[m];

    count++;

    }

    if(i==n)
    i=0;

    }while(f!=0);

    System.out.println();

    for(i=0;i<=count;i++)
    {
    apw1[i]=sq[i];
    }

    System.out.println();

    for(i=0;i<n;i++)
    {
    apw[i]=turn[i]-bt[i];
    t+=apw[i];
    tu+=turn[i];
    }
    System.out.println("—- Waiting Time —-");
    for(i=0;i<n;i++)
    System.out.println("Process p"+(i+1)+": "+apw[i]);
    System.out.println("\n\n—- Average Waiting —-");
    float avg=t/n;
    System.out.println("Average waiting time: "+avg);
    System.out.println("\n\n—- Turn-Around Time —-");
    for(i=0;i<n;i++)
    System.out.println("Process p"+(i+1)+": "+turn[i]);

    float avg1=tu/n;
    System.out.println("Average Turn-Around: "+avg1);

    n1=count;

    // paint(g);

    }
    gonethrough = true;
    break;
    // —————————————————————————————————————————————————————-
    case 5:
    {
    for( i=0;i<n;i++)
    {System.out.println("ENTER burst time for each process: p"+(i+1));
    bt[i]=Integer.parseInt(obj.readLine());
    }
    n1=n;
    int pr[]=new int[n];
    int pr1[]=new int[n];
    float t=0;
    int temp;
    int turn[]=new int[n];
    p=new int[n];
    apw1=new int[n+1];
    apw1[0]=0;
    float tu=0;

    for(i=0;i<n;i++)
    {
    System.out.println("Enter the priority for p"+(i+1));
    pr[i]=Integer.parseInt(obj.readLine());
    }

    for(i=0;i<n;i++)
    {
    pr1[i]=pr[i];
    }

    for( i=0;i<n;i++)
    for(int j=i+1;jpr1[j])
    {
    temp=pr1[i];
    pr1[i]=pr1[j];
    pr1[j]=temp;
    }

    for( i=0;i<n;i++)
    for(int j=0;j<n;j++)
    if(pr1[i]==pr[j])
    p[i]=j+1;

    for(i=0;i<n;i++)
    {
    int k=p[i];
    apw1[i+1]=bt[k-1]+apw1[i];
    }

    System.out.println("—- Individual Waiting TIme —-");

    for( i=0;i<n;i++)
    {
    System.out.println("Process p"+p[i]+": "+apw1[i]+" ");
    }

    System.out.println("\n\n—- Average Waiting Time —-");

    for( i=0;i<n;i++)
    t+=apw1[i];
    float avg=t/n;
    System.out.println("Average waiting time is: "+avg);

    /*for( i=0;i<n;i++)
    System.out.println("response time for process p"+p[i]+"is"+apw1[i]+" ");*/

    System.out.println("\n\n—- Turn-Around Time —-");

    for( i=0;i<n;i++)
    {
    int k=p[i];
    turn[i]=bt[k-1]+apw1[i];
    System.out.println("Process p"+p[i]+": "+turn[i]+" ");
    }

    for( i=0;i<n;i++)
    tu+=turn[i];
    System.out.println("\n\n—- Average Turn-Around Time —-");
    float avg1=tu/n;
    System.out.println("Average turn-around time is: "+avg1);

    // paint(g);

    }
    gonethrough = true;
    break;
    // —————————————————————————————————————————————————————-
    // —————————————————————————————————————————————————————-
    case 6:

    {
    int total=0,k=0,co=0,small=999,sp=0,sp1=0,x=0,count=0;
    int pro[][]=new int[n][4];
    int wt[]=new int[n];
    int wt1[]=new int[n];
    int bt1[]=new int[n];
    int tt[]=new int[n];
    apw1=new int [20];
    p=new int[20];

    for(i=0;i<n;i++)
    {
    pro[i][0]=i;
    System.out.print("Burst Time for P"+i+": ");
    pro[i][3]=Integer.parseInt(obj.readLine());
    total+=pro[i][3];
    System.out.print("Arrival Time for P"+i+": ");
    pro[i][2]=Integer.parseInt(obj.readLine());
    System.out.print("Priority for P"+i+": ");
    pro[i][1]=Integer.parseInt(obj.readLine());

    }
    for(i=0;i<n;i++)
    wt1[i]=pro[i][2];

    for(i=0;i<n;i++)
    bt1[i]=pro[i][3];

    for(i=0;i<n;i++)
    for(int j=i+1;jpro[j][2])
    {
    int temp[]=pro[i];
    pro[i]=pro[j];
    pro[j]=temp;
    }

    for(i=1;i<=total;i++)
    {
    small=999;
    for(int j=co;j=pro[j][2])
    co++;
    for(int j=0;jpro[j][1]&&pro[j][3]!=0)
    {
    small=pro[j][1];
    sp=pro[j][0];
    sp1=j;
    }
    }
    if(p[x]==sp)
    {
    apw1[x+1]++;

    }
    else
    {
    x++;
    p[x]=sp;
    apw1[x+1]=apw1[x];
    apw1[x+1]++;
    count++;
    }

    pro[sp1][3]-=1;
    if(pro[sp1][3]==0)
    tt[sp1]=i;
    for(int j=0;j<n;j++)
    {
    if(pro[j][3]!=0&&j!=sp)
    wt[j]+=1;
    }
    k++;
    }
    for(i=0;i<n;i++)
    for(int j=i+1;jpro[j][0])
    {
    int temp[]=pro[i];
    pro[i]=pro[j];
    pro[j]=temp;
    int tem=wt[i],tem1=tt[i];
    wt[i]=wt[j];
    wt[j]=tem;
    tt[i]=tt[j];
    tt[j]=tem1;

    }

    System.out.println();

    for(int m=0;m<=count;m++)
    p[m]+=1;

    double awt=0.0,att=0.0;

    System.out.println("—- Waiting Time —-");

    System.out.println();
    for (i = 0; i<n; i++)
    {
    int temp = wt[i]-wt1[i];
    if(temp<0){
    temp=0;
    }
    System.out.println("Process p"+(i+1)+": "+temp);
    awt+=(wt[i]-wt1[i]);
    att+=(wt[i]-wt1[i]+bt1[i]);
    }

    System.out.println("\n\n—- Turnaround Time —-");

    for (i = 0; i<n; i++)
    {
    int temp = wt[i]-wt1[i]+bt1[i];
    if(temp0 && ch<7 && !gonethrough);

    }

    catch (Exception e)
    {}

    }

    }

    1. The last bit got torn off, sorry. Starting from ‘case 6:’

      // —————————————————————————————————————————————————————-
      // —————————————————————————————————————————————————————-
      case 6:

      {
      int total=0,k=0,co=0,small=999,sp=0,sp1=0,x=0,count=0;
      int pro[][]=new int[n][4];
      int wt[]=new int[n];
      int wt1[]=new int[n];
      int bt1[]=new int[n];
      int tt[]=new int[n];
      apw1=new int [20];
      p=new int[20];

      for(i=0;i<n;i++)
      {
      pro[i][0]=i;
      System.out.print("Burst Time for P"+i+": ");
      pro[i][3]=Integer.parseInt(obj.readLine());
      total+=pro[i][3];
      System.out.print("Arrival Time for P"+i+": ");
      pro[i][2]=Integer.parseInt(obj.readLine());
      System.out.print("Priority for P"+i+": ");
      pro[i][1]=Integer.parseInt(obj.readLine());

      }
      for(i=0;i<n;i++)
      wt1[i]=pro[i][2];

      for(i=0;i<n;i++)
      bt1[i]=pro[i][3];

      for(i=0;i<n;i++)
      for(int j=i+1;jpro[j][2])
      {
      int temp[]=pro[i];
      pro[i]=pro[j];
      pro[j]=temp;
      }

      for(i=1;i<=total;i++)
      {
      small=999;
      for(int j=co;j=pro[j][2])
      co++;
      for(int j=0;jpro[j][1]&&pro[j][3]!=0)
      {
      small=pro[j][1];
      sp=pro[j][0];
      sp1=j;
      }
      }
      if(p[x]==sp)
      {
      apw1[x+1]++;

      }
      else
      {
      x++;
      p[x]=sp;
      apw1[x+1]=apw1[x];
      apw1[x+1]++;
      count++;
      }

      pro[sp1][3]-=1;
      if(pro[sp1][3]==0)
      tt[sp1]=i;
      for(int j=0;j<n;j++)
      {
      if(pro[j][3]!=0&&j!=sp)
      wt[j]+=1;
      }
      k++;
      }
      for(i=0;i<n;i++)
      for(int j=i+1;jpro[j][0])
      {
      int temp[]=pro[i];
      pro[i]=pro[j];
      pro[j]=temp;
      int tem=wt[i],tem1=tt[i];
      wt[i]=wt[j];
      wt[j]=tem;
      tt[i]=tt[j];
      tt[j]=tem1;

      }

      System.out.println();

      for(int m=0;m<=count;m++)
      p[m]+=1;

      double awt=0.0,att=0.0;

      System.out.println("—- Waiting Time —-");

      System.out.println();
      for (i = 0; i<n; i++)
      {
      int temp = wt[i]-wt1[i];
      if(temp<0){
      temp=0;
      }
      System.out.println("Process p"+(i+1)+": "+temp);
      awt+=(wt[i]-wt1[i]);
      att+=(wt[i]-wt1[i]+bt1[i]);
      }

      System.out.println("\n\n—- Turnaround Time —-");

      for (i = 0; i<n; i++)
      {
      int temp = wt[i]-wt1[i]+bt1[i];
      if(temp0 && ch<7 && !gonethrough);

      }

      catch (Exception e)
      {}

      }

      }

    1. Bro plz help me in your program when i run this in cmd prompt it run but there is a problem in (” enter no of choices ” )this option is not working and same problem with (“exit option ) when i press the exit option then it doesnot exit plz help me bro its relly urgent

  6. Thanks man! But.. How about if someone arrived at time 0 and burst only to 3 then the next jobs arrival time is 6. thats my problem

  7. i need some information about this project like why this program takes only rectangle.java input inspite of if we change the class name. also i know about other factors like apw, co,pro. plz tell me.

    1. Really sorry, currently I hardly code anything. I usually do most excelsheets and high level tech integrations rather than coding. But I highly recommend to ask for help after searching Stackoverflow.com, because some brilliant minds are active there for you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s