Contoh source code proses computer :
import java.util.Random;
public class ShortestJobFirst {
static class Process {
public int arrivalTime;
public int startTime;
public int burstTime;
public Process(int t) { burstTime=t; }
}
static class Queue {
class QueueNode {
Process key;
QueueNode next = null;
QueueNode(Process p, QueueNode n) { key=p; next=n; }
QueueNode(Process p) { key=p; }
}
QueueNode tail;
public Queue() {
tail = null;
}
public boolean isEmpty() {
return tail==null;
}
public void enqueue(Process x, float burstTime) {
if ( tail==null ) {
tail = new QueueNode(x); tail.next=tail;
}
else tail = (tail.next = new QueueNode(x,tail.next));
}
public Process dequeue() {
if ( tail==null ) return null;
else { QueueNode p = tail.next;
if ( p!=tail ) tail.next = p.next;
else tail = null;
return p.key;
}
}
}
public static void main(String[] args) {
int T = 0;
int N = 0;
int NumProcesses = 100;
int QueuedProcesses = 0;
int totalProcessDuration = 0;
int totalTurnaroundTime = 0;
Process activeProcess = null;
int lastProcessArrival = 0;
Random rand0 = new Random();
Random rand1 = new Random();
Random rand2 = new Random();
Queue q = new Queue();
while ( N < NumProcesses ) {
int newProcessArrival = rand0.nextInt(1801);
do {
u = 1.-rand1.nextDouble();
v = 1.-rand2.nextDouble();
x = Math.sqrt (-2.*Math.log(u))*Math.cos(2.*Math.PI*v) + 2.0; /
} while ( x<0 );
int newProcessDuration = (int) Math.round (x*1000);
// Create the process p
Process p = new Process(newProcessDuration);
lastProcessArrival += newProcessArrival;
if ( lastProcessArrival < T ) lastProcessArrival = T;
p.arrivalTime = lastProcessArrival;
// If the active process has finished, start the next one
if ( activeProcess == null ) {
activeProcess = p;
T = p.startTime = p.arrivalTime;
}
// Otherwise, put the process in the queue
else { q.enqueue(p, p.burstTime); QueuedProcesses++; }
do { // this terminates processes
if ( T + activeProcess.burstTime > lastProcessArrival ) break;
totalProcessDuration += activeProcess.burstTime;
T += activeProcess.burstTime;
int turnaroundTime = T - activeProcess.arrivalTime;
totalTurnaroundTime += turnaroundTime;
System.out.printf ("Process %d terminated. Turnaround time: %.2f milliseconds.\n",
++N, (double) turnaroundTime/1000);
System.out.printf (" \t(arrival %.2f, begin %.2f, end %.2f, duration %.2f)\n",
(double) activeProcess.arrivalTime/1000, (double) activeProcess.startTime/1000,
(double) (activeProcess.startTime+activeProcess.burstTime)/1000,
(double) activeProcess.burstTime/1000);
// If terminated processes >= to max processes, break;
if ( N >= NumProcesses ) break;
if ( q.isEmpty( ) ) { activeProcess = null; break; }
// Otherwise, dequeue the active process and start the next one
else {
activeProcess = q.dequeue( ); QueuedProcesses--;
if ( activeProcess.arrivalTime <= T ) activeProcess.startTime = T;
else activeProcess.startTime = T = activeProcess.arrivalTime;
}
} while(true);
} // end while loop
// Print the results
System.out.printf ("\nSimulation time: %.2f milliseconds\n", (double) T/1000);
System.out.printf ("Average turnaround time: %.2f milliseconds\n",
(double) totalTurnaroundTime/NumProcesses/1000);
System.out.printf ("Average process duration: %.2f milliseconds\n",
(double) totalProcessDuration/NumProcesses/1000);
System.out.printf ("Throughput: %.2f processes per second\n", (double) NumProcesses/T * 1000000);
System.out.println ("Processes in the queue at the end of the simulation: " + QueuedProcesses);
} // end main
}
Gambar hasil output proses :
import java.util.Random;
public class ShortestJobFirst {
static class Process {
public int arrivalTime;
public int startTime;
public int burstTime;
public Process(int t) { burstTime=t; }
}
static class Queue {
class QueueNode {
Process key;
QueueNode next = null;
QueueNode(Process p, QueueNode n) { key=p; next=n; }
QueueNode(Process p) { key=p; }
}
QueueNode tail;
public Queue() {
tail = null;
}
public boolean isEmpty() {
return tail==null;
}
public void enqueue(Process x, float burstTime) {
if ( tail==null ) {
tail = new QueueNode(x); tail.next=tail;
}
else tail = (tail.next = new QueueNode(x,tail.next));
}
public Process dequeue() {
if ( tail==null ) return null;
else { QueueNode p = tail.next;
if ( p!=tail ) tail.next = p.next;
else tail = null;
return p.key;
}
}
}
public static void main(String[] args) {
int T = 0;
int N = 0;
int NumProcesses = 100;
int QueuedProcesses = 0;
int totalProcessDuration = 0;
int totalTurnaroundTime = 0;
Process activeProcess = null;
int lastProcessArrival = 0;
Random rand0 = new Random();
Random rand1 = new Random();
Random rand2 = new Random();
Queue q = new Queue();
while ( N < NumProcesses ) {
int newProcessArrival = rand0.nextInt(1801);
do {
u = 1.-rand1.nextDouble();
v = 1.-rand2.nextDouble();
x = Math.sqrt (-2.*Math.log(u))*Math.cos(2.*Math.PI*v) + 2.0; /
} while ( x<0 );
int newProcessDuration = (int) Math.round (x*1000);
// Create the process p
Process p = new Process(newProcessDuration);
lastProcessArrival += newProcessArrival;
if ( lastProcessArrival < T ) lastProcessArrival = T;
p.arrivalTime = lastProcessArrival;
// If the active process has finished, start the next one
if ( activeProcess == null ) {
activeProcess = p;
T = p.startTime = p.arrivalTime;
}
// Otherwise, put the process in the queue
else { q.enqueue(p, p.burstTime); QueuedProcesses++; }
do { // this terminates processes
if ( T + activeProcess.burstTime > lastProcessArrival ) break;
totalProcessDuration += activeProcess.burstTime;
T += activeProcess.burstTime;
int turnaroundTime = T - activeProcess.arrivalTime;
totalTurnaroundTime += turnaroundTime;
System.out.printf ("Process %d terminated. Turnaround time: %.2f milliseconds.\n",
++N, (double) turnaroundTime/1000);
System.out.printf (" \t(arrival %.2f, begin %.2f, end %.2f, duration %.2f)\n",
(double) activeProcess.arrivalTime/1000, (double) activeProcess.startTime/1000,
(double) (activeProcess.startTime+activeProcess.burstTime)/1000,
(double) activeProcess.burstTime/1000);
// If terminated processes >= to max processes, break;
if ( N >= NumProcesses ) break;
if ( q.isEmpty( ) ) { activeProcess = null; break; }
// Otherwise, dequeue the active process and start the next one
else {
activeProcess = q.dequeue( ); QueuedProcesses--;
if ( activeProcess.arrivalTime <= T ) activeProcess.startTime = T;
else activeProcess.startTime = T = activeProcess.arrivalTime;
}
} while(true);
} // end while loop
// Print the results
System.out.printf ("\nSimulation time: %.2f milliseconds\n", (double) T/1000);
System.out.printf ("Average turnaround time: %.2f milliseconds\n",
(double) totalTurnaroundTime/NumProcesses/1000);
System.out.printf ("Average process duration: %.2f milliseconds\n",
(double) totalProcessDuration/NumProcesses/1000);
System.out.printf ("Throughput: %.2f processes per second\n", (double) NumProcesses/T * 1000000);
System.out.println ("Processes in the queue at the end of the simulation: " + QueuedProcesses);
} // end main
}
Gambar hasil output proses :
First In First Out
Source code :
package fifo;
import java.util.*;
public class FIFO{
public static void main(String[] args) {
Stack stack=new Stack();
stack.push(new Integer(10));
stack.push("a");
System.out.println("The contents of Stack is" + stack);
System.out.println("The size of an Stack is" + stack.size());
System.out.println("The number poped out is" + stack.pop());
System.out.println("The number poped out is " + stack.pop());
//System.out.println("The number poped out is" + stack.pop());
System.out.println("The contents of stack is" + stack);
System.out.println("The size of an stack is" + stack.size());
}
}
Gambar output proses :
package fifo;
import java.util.*;
public class FIFO{
public static void main(String[] args) {
Stack stack=new Stack();
stack.push(new Integer(10));
stack.push("a");
System.out.println("The contents of Stack is" + stack);
System.out.println("The size of an Stack is" + stack.size());
System.out.println("The number poped out is" + stack.pop());
System.out.println("The number poped out is " + stack.pop());
//System.out.println("The number poped out is" + stack.pop());
System.out.println("The contents of stack is" + stack);
System.out.println("The size of an stack is" + stack.size());
}
}
Gambar output proses :
Thread
- Definisi:
Ulir atau thread (singkatan dari "thread of execution") dalam ilmu komputer, diartikan sebagai sekumpulan perintah (instruksi) yang dapat dilaksanakan (dieksekusi) secara sejajar dengan ulir lainnya, dengan menggunakan cara time slice (ketika satu CPU melakukan perpindahan antara satu ulir ke ulir lainnya) atau multiprocess (ketika ulir-ulir tersebut dilaksanakan oleh CPU yang berbeda dalam satu sistem). Ulir sebenarnya mirip dengan proses, tapi cara berbagi sumber daya antara proses dengan ulir sangat berbeda. Multiplethread dapat dilaksanakan secara sejajar pada sistem komputer. Secara umum multithreadingmelakukan time-slicing (sama dengan time-division multipleks), di mana sebuah CPU bekerja pada ulir yang berbeda, di mana suatu kasus ditangani tidak sepenuhnya secara serempak, untuk CPU tunggal pada dasarnya benar-benar melakukan sebuah pekerjaan pada satu waktu. Teknik penggantian (switching) ini memungkinkan CPU seolah-olah bekerja secara serempak.
- Contoh source code thread pada Java:
public class csj01x3 extends Thread
{
public static void main(String args[]) throws Throwable
{
new csj01x3().start();
new csj01x3().start();
// main thread is ending here,
// Thread-0 and Thread-1 continue to run.
}
public void run()
{
try {
for (int i=0; i<100; i++) {
System.out.println("thread "
+Thread.currentThread().getName()+" step "+i);
Thread.sleep(500);
}
} catch (Throwable t) { }
}
}