Beruflich Dokumente
Kultur Dokumente
SJF Code
SJF Code
# include
# include
main()
{
int k=0,ptime[25],n,s=0,i,sum=0, atime[25],at[25];
char name[25][25], m[25][25];
int t,p,time[10],j,l;
float avg;
printf("===============================================\n");
printf(" PROGRAM PENJADWALAN SHORTEST JOB FIRST (SJF) \n");
printf("===============================================\n\n");
printf ("Masukkan jumlah proses: ");
scanf ("%d",&n);
printf ("\n\n");
for(i=0;i {
printf("Masukkan nama proses ");
printf("%d : ",i+1);
scanf("%s",name[i]);
}
printf("\n \n");
for(i=0;i {
printf("Masukkan lama proses ");
printf("%s : ",name[i]);
scanf("%d",&ptime[i]);
}
printf("\n");
printf("---------------------------------");
printf("\n| Nama Proses \t| Lama Proses \t|\n");
printf("---------------------------------\n");
for(i=0;i {
printf("|\t %s \t|\t %d \t|\n",name[i],ptime[i]);
printf("---------------------------------\n");
}
l=at[i];
at[i]=at[j];
at[j]=l;
strcpy(m[i],name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],m[i]);
}
}
}
for(i=0;i {
printf("|\t %s \t|\t %d \t|\n",name[i],time[i]);
printf("---------------------------------\n");
}
for(i=0;i {
printf("\nProses %s dari %d ke %d \n",name[i],k,(k+time[i]));
k+=time[i];
}
for(i=0;i<(n-1);i++)
{
s+=time[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n\nRata-rata Waktu Tunggu : \t");
printf("%2f",avg);
sum=avg=s=0;
for(i=0;i {
s+=time[i];
sum+=s;
}
avg=(float)sum/n;
printf("\n\nRata-rata Turn Arround Time : \t");
printf("%2f",avg);
sum=avg=s=0;
printf("\n\n");
printf("\n\n=========================================\n");
printf("Terimakasih telah menggunakan program ini\n");
printf("=========================================\n");
printf("\n");
}
// C++ program to implement Shortest Job first with Arrival Time
#include<iostream>
int mat[10][6];
*a = *b;
*b = temp;
swap(mat[j][k], mat[j+1][k]);
}
}
temp = mat[i-1][3];
low = mat[j][2];
val = j;
swap(mat[val][k], mat[i][k]);
int main()
cin>>num;
cout<<"...Process "<<i+1<<"...\n";
cin>>mat[i][0];
cin>>mat[i][1];
cin>>mat[i][2];
cout<<"Before Arrange...\n";
cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\n";
arrangeArrival(num, mat);
completionTime(num, mat);
cout<<"Final Result...\n";
cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\t\t"<<mat[i]
[4]<<"\t\t"<<mat[i][5]<<"\n";
SRTF
#include <bits/stdc++.h>
struct Process {
// processes
int wt[])
int rt[n];
rt[i] = proc[i].bt;
// completed
while (complete != n) {
// current time`
minm = rt[j];
shortest = j;
check = true;
if (check == false) {
t++;
continue;
rt[shortest]--;
// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
// executed
if (rt[shortest] == 0) {
// Increment complete
complete++;
check = false;
// Find finish time of current
// process
finish_time = t + 1;
wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
// Increment time
t++;
// bt[i] + wt[i]
}
// Function to calculate average time
total_tat = 0;
// processes
findWaitingTime(proc, n, wt);
// all processes
// details
// Driver code
int main()
Process proc[] = { { 1, 6, 1 }, { 2, 8, 1 },
{ 3, 7, 2 }, { 4, 3, 3 } };
findavgTime(proc, n);
return 0;
SJF CODE
#include <iostream>
using namespace std;
int main(){
int i,j,k,p,s=0, got=0, idle=0, temp_burst, temp_row, pre_process_row, done=0;
float sum=0;
cout<<"Please enter the number of process : ";
cin>>p;
int a[p][5];
int b[p][5];
cout<<"\nProcess\tArrival\tBurst\n-------\t-------\t-----\n";
for(i=0;i<p;i++){
for(j=0;j<3;j++){
cin>>a[i][j];
}
a[i][3]=a[i][2];
}
cout<<"\n\nTime-Line is as follows (Verticle View)....\n\n";
i=a[0][1];
while(done!=p){
got=0;
k=0;
while(k<p){
if(a[k][1]<=i){
if(a[k][2]!=0){
got=1;
temp_burst=a[k][2];
temp_row=k;
idle=0;
break;
}
else
k++;
}
else{
if(idle==0)
printf("%5d-----------\n |Idle |\n",i);
idle=1;
break;
}
}
if(got!=0){
k=0;
while(a[k][1]<=i && k<p){
if(a[k][2]!=0){
if(temp_burst>a[k][2]){
temp_burst=a[k][2];
temp_row=k;
}
}
k++;
}
a[temp_row][2]-=1;
if(i==a[0][1])
printf("%5d-----------\n |p-%-4d|\n",i,a[temp_row][0]);
else{
if(pre_process_row!=temp_row)
printf("%5d-----------\n |p-%-4d|\n",i,a[temp_row][0]);
}
pre_process_row=temp_row;
if(a[temp_row][2]==0){
done++;
b[s][0]=a[temp_row][0];
b[s][1]=a[temp_row][1];
b[s][2]=i;
b[s][3]=a[temp_row][3];
b[s][4]=((i-a[temp_row][1])-a[temp_row][3])+1;
sum+=((i-a[temp_row][1])-a[temp_row][3])+1;
s++;
}
}
i++;
}
printf("%5d-----------\n",i);
cout<<endl<<endl;
cout<<"Table of processes with completion record as they were completed\n\n";
cout<<"\n\nProcess\tArrival\tFin\tTotal\tWait\n-------\t-------\t---\t-----\t----\n";
for(i=0;i<s;i++)
cout<<b[i][0]<<"\t"<<b[i][1]<<"\t"<<b[i][2]<<"\t"<<b[i] [3]<<"\t"<<b[i][4]<<"\n";
cout<<"\nAvg. Wait time = "<<sum/p<<endl<<endl;
cout<<"__________________________________________________________\nProgramme
d by : Tanmay Chakrabarty, onlineclassnotes.com\n\n";
//system("pause");
return 0;
}
1.SJF
#include <iostream>
#include <algorithm>
int ab;
string pro_id;
int at,bt,ct,ta,wt;
/*
bt = Burst time,
ct = Completion time,
wt = Waiting time
*/
}schedule;
int main()
schedule pro[10];
int n,i,j;
for(i=0;i<n;i++)
cin>>pro[i].pro_id;
cin>>pro[i].at;
cin>>pro[i].bt;
sort(pro,pro+n,compare);
// initial values
pro[0].ct=pro[0].bt+pro[0].at;
pro[0].ta=pro[0].ct-pro[0].at;
pro[0].wt=pro[0].ta-pro[0].bt;
for(i=1;i<n;i++)
ab=pro[i-1].ct;
sort(pro+i,pro+n,compare2);
if(pro[i-1].ct<pro[i].at)
pro[i].ct=pro[i-1].ct+pro[i].bt+(pro[i].at-pro[i-1].ct);
else
pro[i].ct=pro[i-1].ct+pro[i].bt;
pro[i].ta=pro[i].ct-pro[i].at;
pro[i].wt=pro[i].ta-pro[i].bt;
for(i=0;i<n;i++)
<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<pro[i].wt;
cout<<endl;
return 0;
2.SJF CODE
4.SJF CODE
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("nEnter Burst Time:n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1;
}
//sorting of burst times
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n;
total=0;
printf("nProcesst Burst Time tWaiting TimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];
total+=tat[i];
printf("np%dtt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n;
printf("nnAverage Waiting Time=%f",avg_wt);
printf("nAverage Turnaround Time=%fn",avg_tat);
}
5.SJF CODE
#include <iostream>
using namespace std;
//calculations
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if (i>=2 && burst[i-1]>burst[j-1])
{
temp = burst[i-1];
temp2 = arrival[i-1];
burst[i-1]=burst[j-1];
arrival[i-1]=arrival[j-1];
burst[j-1]=temp;
arrival[j-1]=temp2;
}
}
if(i==1)
{
start[0]=0;
end[0]=burst[0];
wait[0]=0;
}
else
{
start[i-1]=end[i-2];
end[i-1]=start[i-1]+burst[i-1];
wait[i-1]=start[i-1]+arrival[i-1];
}
//throughput
if (start[i+1] <= throughput)
tp = i+1;
}
//output
cout << "\n\nPROCESS \t BURST TIME\tARRIVAL TIME\tWAIT TIME\tSTART
TIME\tEND TIME\n";
for (i=0;i<n;i++){
cout << "\nP[" << i + 1 << "]" << "\t\t" << burst[i] << "\t\t" <<
arrival[i] << "\t\t" << wait[i] << "\t\t" << start[i] << "\t\t" << end[i];
}
//avg wait time
for(i=1,tot=0;i<n;i++){
tot+=wait[i-1];
avgwait=tot/n;
}
//avg turnaround time
for(i=1,tot=0;i<n;i++){
tot+=end[i-1];
avgturnaround=tot/n;
}
//avg response time
for(i=1,tot=0;i<n;i++){
tot+=start[i-1];
avgresponse=tot/n;
}
cout << "\n\nAverage Wait Time: " << avgwait;
cout << "\nAverage Response Time: " << avgturnaround;
cout << "\nAverage Turnaround Time: " << avgresponse;
cout << "\nThroughput for (" << throughput << "): " << tp << endl;
}
SJF CODE
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter number of process:");
scanf("%d",&n);
printf("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
printf("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
//sorting burst time in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(bt[j]<bt[pos])
pos=j;
}
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process will be zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=(float)total/n; //average waiting time
total=0;
printf("\nProcess\t Burst Time \tWaiting Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\np%d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=(float)total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%f",avg_wt);
printf("\nAverage Turnaround Time=%f\n",avg_tat);
}
SJF CODE
// C++ program to implement Shortest Remaining
// Time First
#include <bits/stdc++.h>
using namespace std;
struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};
// Function to find the waiting time for all
// processes
void findWaitingTime(Process proc[], int n,
int wt[])
{
int rt[n];
// Copy the burst time into rt[]
for (int i = 0; i < n; i++)
rt[i] = proc[i].bt;
int complete = 0, t = 0, minm = INT_MAX;
int shortest = 0, finish_time;
bool check = false;
// Process until all processes gets
// completed
while (complete != n) {
// Find process with minimum
// remaining time among the
// processes that arrives till the
// current time`
for (int j = 0; j < n; j++) {
if ((proc[j].art <= t) &&
(rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}
if (check == false) {
t++;
continue;
}
// Reduce remaining time by one
rt[shortest]--;
// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;
// If a process gets completely
// executed
if (rt[shortest] == 0) {
// Increment complete
complete++;
check = false;
// Find finish time of current
// process
finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;
if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}
// Function to calculate turn around time
void findTurnAroundTime(Process proc[], int n,
int wt[], int tat[])
{
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++)
tat[i] = proc[i].bt + wt[i];
}
// Function to calculate average time
void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0,
total_tat = 0;
// Function to find waiting time of all
// processes
findWaitingTime(proc, n, wt);
// Function to find turn around time for
// all processes
findTurnAroundTime(proc, n, wt, tat);
// Display processes along with all
// details
cout << "Processes "
<< " Burst time "
<< " Waiting time "
<< " Turn around time ";
// Calculate total waiting time and
// total turnaround time
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << " "
<< proc[i].bt << " " << wt[i]
<< " " << tat[i] << endl;
}
cout << " Average waiting time = "
<< (float)total_wt / (float)n;
cout << " Average turn around time = "
<< (float)total_tat / (float)n;
}
// Driver code
int main()
{
Process proc[] = { { 1, 6, 1 }, { 2, 8, 1 },
{ 3, 7, 2 }, { 4, 3, 3 } };
int n = sizeof(proc) / sizeof(proc[0]);
findavgTime(proc, n);
return 0;
}
SJF CODE IN VB
Public Class frmSJF
002
003 Public Function NumberSorting(ByVal numbers() As Integer) _
004 As Integer()
005
006 For j As Integer = 0 To numbers.Length - 1
007
008 For x As Integer = 0 To numbers.Length - 1
009 If numbers(x) > numbers(j) Then
010 numbers(x) = numbers(x) + numbers(j)
011 numbers(j) = numbers(x) - numbers(j)
012 numbers(x) = numbers(x) - numbers(j)
013 End If
014 Next
015 Next
016
017 Return numbers
018
019 End Function
020
Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As
021
System.EventArgs) Handles btnOK.Click
022
023
024
025 If tbBT4.Text = "" And tbBT5.Text = "" Then
026
Dim numbers() As Integer = {Val(tbBT1.Text), Val(tbBT2.Text),
027
Val(tbBT3.Text)}
028 Dim sortedNumberList() As Integer = NumberSorting(numbers)
029 Dim ans1(3) As Integer
030
031 ans1(0) = sortedNumberList(0) + 0
032 ans1(1) = sortedNumberList(1) + ans1(0)
033 ans1(2) = sortedNumberList(2) + ans1(1)
034
035 Label1.Text = 0
036 Label2.Text = ans1(0)
037 Label3.Text = ans1(1)
038 Label4.Text = ans1(2)
039
lblCompAWT.Text = "0" + " + " + ans1(0).ToString + " + " +
040
ans1(1).ToString
lblCompAWTAns.Text = (0 + ans1(0) + ans1(1)).ToString + " /
041
" + "3"
lblTAWT.Text = ((0 + ans1(0) + ans1(1)) / 3).ToString + "
042
ms"
043
lblCompATAT.Text = ans1(0).ToString + " + " +
044
ans1(1).ToString + " + " + ans1(2).ToString()
lblCompATATAns.Text = (ans1(0) + ans1(1) + ans1(2)).ToString
045
+ " / " + "3"
lblTATAT.Text = ((ans1(0) + ans1(1) + ans1(2)) / 3).ToString
046
+ " ms"
047
048 ElseIf tbBT5.Text = "" Then
049
Dim numbers() As Integer = {Val(tbBT1.Text), Val(tbBT2.Text),
050
Val(tbBT3.Text), Val(tbBT4.Text)}
051 Dim sortedNumberList() As Integer = NumberSorting(numbers)
052 Dim ans2(4) As Integer
053
054
055 ans2(0) = sortedNumberList(0) + 0
056 ans2(1) = sortedNumberList(1) + ans2(0)
057 ans2(2) = sortedNumberList(2) + ans2(1)
058
059 Label1.Text = 0
060 Label2.Text = ans2(0)
061 Label3.Text = ans2(1)
062 Label4.Text = ans2(2)
063
lblCompAWT.Text = "0" + " + " + ans2(0).ToString + " + " +
064
ans2(1).ToString + " + " + ans2(2).ToString()
lblCompAWTAns.Text = (0 + ans2(0) + ans2(1) +
065
ans2(2)).ToString + " / " + "4"
lblTAWT.Text = ((0 + ans2(0) + ans2(1) + ans2(2)) /
066
4).ToString + " ms"
067
lblCompATAT.Text = ans2(0).ToString + " + " +
068
ans2(1).ToString + " + " + ans2(2).ToString() + " + " + ans2(3).ToString
lblCompATATAns.Text = (ans2(0) + ans2(1) + ans2(2) +
069
ans2(3)).ToString + " / " + "4"
lblTATAT.Text = ((ans2(0) + ans2(1) + ans2(2) + ans2(3)) /
070
5).ToString + " ms"
071
072 ElseIf tbBT4.Text = "" Then
073
074 MsgBox("Do not leave this Blank")
075
076 Else
077
Dim numbers() As Integer = {Val(tbBT1.Text), Val(tbBT2.Text),
078
Val(tbBT3.Text), _
079 Val(tbBT4.Text), Val(tbBT5.Text)}
080 Dim sortedNumberList() As Integer = NumberSorting(numbers)
081 Dim ans(5) As Double
082
083 ans(0) = sortedNumberList(0) + 0
084 ans(1) = sortedNumberList(1) + ans(0)
085 ans(2) = sortedNumberList(2) + ans(1)
086 ans(3) = sortedNumberList(3) + ans(2)
087 ans(4) = sortedNumberList(4) + ans(3)
088
089 Label1.Text = 0
090 Label2.Text = ans(0)
091 Label3.Text = ans(1)
092 Label4.Text = ans(2)
093 Label5.Text = ans(3)
094 Label6.Text = ans(4)
095
lblCompAWT.Text = "0" + " + " + ans(0).ToString + " + " +
096
ans(1).ToString + " + " + ans(2).ToString() + " + " + ans(3).ToString
lblCompAWTAns.Text = (0 + ans(0) + ans(1) + ans(2) +
097
ans(3)).ToString + " / " + "5"
lblTAWT.Text = ((0 + ans(0) + ans(1) + ans(2) + ans(3)) /
098
5).ToString + " ms"
099
lblCompATAT.Text = ans(0).ToString + " + " + ans(1).ToString
100 + " + " + ans(2).ToString() + " + " + ans(3).ToString + " + " +
ans(4).ToString
101 lblCompATATAns.Text = (ans(0) + ans(1) + ans(2) + ans(3) +
ans(4)).ToString + " / " + "5"
lblTATAT.Text = ((ans(0) + ans(1) + ans(2) + ans(3) +
102
ans(4)) / 5).ToString + " ms"
103
104 End If
105 End Sub
106 End Class
SJF
Name: Shortest Job First(SJF)
// Description:This code is used to implement the Shortest Job First
Scheduling Algorithm.
// By: Adri Jovin (from psc cd)
//**************************************