Sabtu, 23 Agustus 2014

Penjadwalan Proses CPU ( Sederhana )

Assalammu'alaikum .., Salam Sejahtera ..,
Bagaimana kabarnya ??
Lama saya nggak posting di blog ini, karena begitu sibuknya dengan tugas kuliah & kerja. Kali ini saya akan memberikan Script Java Penjadwalan Proses CPU.
Ini adalah Script sederhanannya , dan masih bisa dikembangkan.
Okey.. langsung saja , berikut script javanya :
.
.


import java.util.Scanner;
public class Penjadwalan_Proses
{
 void buatGaris(int lenGrantChart)
 {
  for(int i=0; i < lenGrantChart; i++)
  {
   System.out.print("------");
  }
  System.out.println();
 }

 public Jadwal_1()
 {
  /*inisialisasi variabel*/
  Scanner scanner = new Scanner(System.in);

  int readyQueue[][];
  int grantChart[][];

  int totalProcess  = 0;
  int timeSlice   = 0;
  int totalBurstTime  = 0;
  int totalQuantum  = 0;


  System.out.print("Masukkan jumlah proses : ");
  totalProcess = scanner.nextInt();

  System.out.print("Masukkan time slice    : ");
  timeSlice = scanner.nextInt();

  //ini akan membuat array [Pn][name], [Pn][BT], [Pn][WT], [Pn][TO], [Pn][RT]
  readyQueue = new int[totalProcess][5];
  System.out.println();

  /*masukkan burst time proses*/
  for(int i=0; i < readyQueue.length; i++)
  {
   //set nama proses
   readyQueue[i][0] = i;
 
   //set burst time proses
   System.out.print("Masukan burst time P"+ i + " : ");
   readyQueue[i][1] = scanner.nextInt();
 
   //hitung jumlah burst time
   totalBurstTime += readyQueue[i][1];
 
   //hitung jumlah quantum per proses
   totalQuantum += (int) Math.ceil((double)readyQueue[i][1] / (double)timeSlice);
 
   //set default waiting time, turn around time, dan respon time
   readyQueue[i][2] = 0; //set waiting time
   readyQueue[i][3] = 0; //set turn around time
   readyQueue[i][4] = 0; //set respon time
  }

  /*running proses*/

  grantChart = new int[totalQuantum][3];
  int time = 0;
  int index = 0;

  while (time < totalBurstTime)
  {
 
   for(int i=0; i < readyQueue.length; i++)
   {
    //respon time
    if(index < totalProcess) readyQueue[i][4] = time;
 
    if(readyQueue[i][1] != 0)
    {
     //waiting time
     readyQueue[i][3] += time;
   
     grantChart[index][0] = readyQueue[i][0]; //name process
     grantChart[index][1] = time; //start time quantum process
 
   
     //jika burst time >= time slice
     if(readyQueue[i][1] >= timeSlice)
     {
      grantChart[index][2] = time = time + timeSlice; //end time quantum process
      readyQueue[i][1] -= timeSlice; //kurangi burst time sebanyak time slice
     }
     else if(readyQueue[i][1] < timeSlice && readyQueue[i][1] > 0)
     {
      grantChart[index][2] = time = time + readyQueue[i][1]; //end time quantum process
      readyQueue[i][1] = 0; //burst time telah habis
     }
     //turn around time
     if(readyQueue[i][1] == 0) readyQueue[i][2] = time;
   
     index++;
    }
   }
  }
  //cetak grant chart
  buatGaris(grantChart.length);

  for(int i=0; i < grantChart.length; i++)
  {
   System.out.print("  P" + grantChart[i][0] + "  ");
  }
  System.out.println();

  buatGaris(grantChart.length);

  for(int i=0; i < grantChart.length; i++)
  {
   if(i == 0) System.out.print(grantChart[i][1]);
   System.out.print("    " + grantChart[i][2]);
  }
  System.out.println();

  buatGaris(grantChart.length);

  System.out.println();

  for(int i = 0; i < readyQueue.length; i++)
  {
   System.out.println("Proses P"+readyQueue[i][0] + "\n" +
    "Turn around Time : " + readyQueue[i][2] + "\n" +
    "Waiting Time     : " + readyQueue[i][3] + "\n" +
    "Respond Time     : " + readyQueue[i][4] + "\n"
   );
  }
 }
 public static void main (String[] args)
 {
  new Jadwal_1();
 }
}


Cukup sederhana bukan ..
Itulah sedikit ilmu yg dapat kami bagi , kami harapkan kritik dan saranya agar dapat berbagi ilmu dan membangun untuk Blog ini.
Terima Kasih atas kunjungannya.
Wassalamu'allaikum .. ..

Tidak ada komentar:

Posting Komentar