Upcoming SlideShare
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Standard text messaging rates apply

Ujian 2 MK SO FCFS Sinkronisasi java

64

Published on

Ujian 2 MK SO FCFS Sinkronisasi java …

Ujian 2 MK SO FCFS Sinkronisasi java

Penjadwalan FCFS dan Sinkronisasi cubby hole Java

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
64
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript

• 1. Materi Penjadwalan dan Sikronisasi (FCFS-CubbyHole) - 21 Mei 2014|Hal 1 dari 3 Ujian 2 Matakuliah: Sistem Operasi Jurusan: Informatika - Universitas Syiah Kuala Semester Genap 2013/2014 - Pengajar: Arie Budiansyah, M.Eng. -------------------------------------------------------------------------------------------------------------- Dibawah ini ada sebuah program penjadwalan FCFS dan sinkronisasi yang masih perlu diperbaiki. Tugas anda adalah menyempurnakan program ini agar berjalan sesuai dengan konsep penjadwalan dan sinkronisasi. *good luck. “Bersemangatlah melakukan hal-hal yang bermanfaat bagimu dan jangan malas (putus asa)” ############### import java.util.*; class fcfs_sinkronisasi { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("enter no. of processes : "); int n=sc.nextInt(); int[] process = new int[n+1]; int[] burst = new int[n+1]; int[] waiting = new int[n+1]; int[] turn =new int[n+1]; for(int m=1; m<=n; m++){ process[m]=m; System.out.print("enter burst time of process "+m+": "); burst[m]=sc.nextInt(); //System.out.println(""); } turn[1]=burst[1]; // turnaround time of first process is equal to its burst time. for(int i=2; i<=n; i++) { turn[i]=burst[i]+turn[i-1]; waiting[i]=turn[i]-burst[i]; } for (int i=1; i<=n; i++) { System.out.print("Burst Time "+i+": "+burst[i]+" --> "); System.out.println("Waiting Time "+i+": "+waiting[i]); } for (int i=1; i<=n; i++) { CubbyHole ch = new CubbyHole(); //ch[0] = new CubbyHole();
• 2. Materi Penjadwalan dan Sikronisasi (FCFS-CubbyHole) - 21 Mei 2014|Hal 2 dari 3 Petugas pgs = new Petugas(ch, n, burst[i]); //pgs[0] = new Petugas(); Kendaraan kdr = new Kendaraan(ch, n); //kdr[0] = new Kendaraan(); //System.out.print("jumlah Process: "+process.length); //System.out.println("burstTime: "+burst[i]); //System.out.println("Process ke: "+i+" --> Burst Time: "+burst[i]); //ch[i] = new CubbyHole(); //Petugas(ch, n, burst); //kdr[i] = new Kendaraan(ch[i], n); //System.out.print("ch: "+ch[i]); //System.out.print("pgs: "+pgs[i]); //System.out.println("kdr: "+kdr[i]); pgs.start(); kdr.start(); } } } class CubbyHole { private int contents; private boolean available = false; public synchronized int get() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = false; notifyAll(); return contents; } public synchronized void put(int value) { while (available == true) { try { wait(); } catch (InterruptedException e) { } } contents = value; available = true;
• 3. Materi Penjadwalan dan Sikronisasi (FCFS-CubbyHole) - 21 Mei 2014|Hal 3 dari 3 notifyAll(); } } class Petugas extends Thread { private CubbyHole cubbyhole; private int processLength; private int burstTime; public Petugas(CubbyHole c, int processLength, int burstTime) { cubbyhole = c; this.processLength = processLength; this.burstTime = burstTime; //System.out.println("jmlh Proses: "+ processLength); } public void run() { for (int i = 1; i < this.processLength; i++) { //System.out.print("BurstTime: "+ burstTime); cubbyhole.put(i); System.out.println("Petugas memberi tiket: " + i); try { sleep((int)(burstTime * 1000)); } catch (InterruptedException e) { } } } } class Kendaraan extends Thread { private CubbyHole cubbyhole; private int processLength; public Kendaraan(CubbyHole c,int processLength) { cubbyhole = c; this.processLength = processLength; } public void run() { int value = 0; for (int i = 0; i < this.processLength; i++) { value = cubbyhole.get(); System.out.println("Kendaraan mengambil tiket: " + value); } } }
• 4. D:Bahan mengajarGenap20132014SOQUIS-SO_1208107010012_PerbaikanProgramFCFSfcfs_sinkronisasi.java 02 June 2014 16:10 /* NAMA : CUT DIRAYATI SAFIRA * NIM : 1208107010012 * quis sistem operasi */ import java.util.*; public class fcfs_sinkronisasi { public static void main(String args[]) { System.out.println("===================================================="); System.out.println("ttFCFS & SINKRONISASItt"); System.out.println("By: CUT DIRAYATI SAFIRA - 1208107010012"); System.out.println("====================================================n"); Scanner sc = new Scanner(System.in); System.out.print("Masukkan Banyak Kendaraan (n) : "); int n=sc.nextInt(); System.out.print("n"); int[] process=new int[n+1]; int [] burst=new int[n+1]; int [] waiting=new int[n+1]; int[] turn=new int[n+1]; for(int m=0;m<n;m++){ process[m]=m; System.out.print("Masukkan Burst Time dari Proses ke-"+(m+1)+" : "); burst[m]=sc.nextInt(); System.out.print("n"); } turn[0]=burst[0]; // turnaround time of first process is equal to its burst time. for(int i=1;i<n;i++) { turn[i]=burst[i]+turn[i-1]; waiting[i]=turn[i]-burst[i]; } for(int i=0; i<n; i++){ System.out.print("Burst Time ke-"+(i+1)+" adalah "+burst[i]+" & "); System.out.println("Waiting Time ke-"+(i+1)+" adalah "+waiting[i]); } System.out.print("n"); //ch[0]=new CubbyHole(); CubbyHole ch = new CubbyHole(); Petugas pgs = new Petugas(ch, n, burst); //pgs[0] = new Petugas(); Kendaraan kdr = new Kendaraan(ch,n); //kdr[0] = new Kendaraan(); //System.out.println("Jumlah Proses : "+process.length); //System.out.println("burst time : "+burst[i]); //System.out.println("Process ke : "+i+"--> Burst Time : "+burst[i]); //ch[i] = new CubbyHole(); //Petugas(ch, n, burst); //kdr[i]=new Kendaraan(ch[i],n); -1-
• 5. D:Bahan mengajarGenap20132014SOQUIS-SO_1208107010012_PerbaikanProgramFCFSfcfs_sinkronisasi.java 02 June 2014 16:10 //System.out.println("ch: "+ch[i]); //System.out.println("pgs: "+pgs[i]); //System.out.println("kdr: "+kdr[i]); pgs.start(); kdr.start(); }//} /*int tot_turn=0,tot_wait=0; for(int i=1;i<=n;i++){ tot_turn+=turn[i]; tot_wait+=waiting[i]; } float avg_turn=(float)tot_turn/n; float avg_wait=(float)tot_wait/n; for(int m=1;m<=n;m++){ System.out.println("nprocess "+process[m]); System.out.print("turn around time : "+turn[m]); System.out.print(" waiting time : "+waiting[m]); } System.out.println("ntotal turn around time : "+tot_turn); System.out.println("ntotal waiting time : "+tot_wait); System.out.println("navg turn around time : "+avg_turn); System.out.println("navg waiting time : "+avg_wait);*/ int i; } /* NAMA : CUT DIRAYATI SAFIRA * NIM : 1208107010012 * quis sistem operasi */ public class Petugas extends Thread { private CubbyHole cubbyhole; private int processLength; private int[] burstTime; public Petugas(CubbyHole c, int processLength, int[] burstTime ){ cubbyhole=c; this.processLength = processLength; this.burstTime = burstTime; /*for(int i=0; i<processLength;i++){ System.out.println("burst "+i+": "+burstTime[i]); }*/ //System.out.println("jumlah proses : "+processLength); } public void run(){ synchronized(cubbyhole){ for(int i=0; i<this.processLength; i++){ //System.out.print("Burst Time :+burstTime"); cubbyhole.put(i); System.out.print("Petugas memberi tiket ke-"+(i+1)+" dengan Burst Time adalah "+ burstTime[i]); -2-
• 6. D:Bahan mengajarGenap20132014SOQUIS-SO_1208107010012_PerbaikanProgramFCFSfcfs_sinkronisasi.java 02 June 2014 16:10 try{ sleep((int)((burstTime[i])*100)); }catch(InterruptedException e){ System.out.println(e); } } } } } class Kendaraan extends Thread{ private CubbyHole cubbyhole; private int processLength; public Kendaraan(CubbyHole c, int processLength){ cubbyhole=c; this.processLength=processLength; } public void run(){ int value=0; synchronized(cubbyhole){ for(int i=0; i<this.processLength; i++){ value=cubbyhole.get(); System.out.print(" dan Kendaraan mengambil tiket ke-"+(value+1)+"n"); } } } } /* NAMA : CUT DIRAYATI SAFIRA * NIM : 1208107010012 * quis sistem operasi */ public class CubbyHole { private int contents; private boolean available = false; public synchronized int get() { while (available == false) { try { wait(); } catch (InterruptedException e) { } } available = false; notifyAll(); return contents; } public synchronized void put(int value) { while (available == true) { try { wait(); -3-
• 7. D:Bahan mengajarGenap20132014SOQUIS-SO_1208107010012_PerbaikanProgramFCFSfcfs_sinkronisasi.java 02 June 2014 16:10 } catch (InterruptedException e) { } } contents = value; available = true; notifyAll(); } } -4-