Sie sind auf Seite 1von 32

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");
}

printf("\n \nPENJADWALAN SJF \n \n");


for(i=0;i {
time[i]=ptime[i];
at[i]=atime[i];
}
for(i=0;i {
for(j=i+1;j {
if(time[i]>time[j])
{
p=time[i];
time[i]=time[j];
time[j]=p;

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]);
}
}
}

printf("Tabel setelah diurutkan berdasarkan SJF");


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],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>

using namespace std;

int mat[10][6];

void swap(int *a, int *b)

int temp = *a;

*a = *b;

*b = temp;

void arrangeArrival(int num, int mat[][6])

for(int i=0; i<num; i++)

for(int j=0; j<num-i-1; j++)

if(mat[j][1] > mat[j+1][1])

for(int k=0; k<5; k++)

swap(mat[j][k], mat[j+1][k]);

}
}

void completionTime(int num, int mat[][6])

int temp, val;

mat[0][3] = mat[0][1] + mat[0][2];

mat[0][5] = mat[0][3] - mat[0][1];

mat[0][4] = mat[0][5] - mat[0][2];

for(int i=1; i<num; i++)

temp = mat[i-1][3];

int low = mat[i][2];

for(int j=i; j<num; j++)

if(temp >= mat[j][1] && low >= mat[j][2])

low = mat[j][2];

val = j;

mat[val][3] = temp + mat[val][2];

mat[val][5] = mat[val][3] - mat[val][1];

mat[val][4] = mat[val][5] - mat[val][2];

for(int k=0; k<6; k++)


{

swap(mat[val][k], mat[i][k]);

int main()

int num, temp;

cout<<"Enter number of Process: ";

cin>>num;

cout<<"...Enter the process ID...\n";

for(int i=0; i<num; i++)

cout<<"...Process "<<i+1<<"...\n";

cout<<"Enter Process Id: ";

cin>>mat[i][0];

cout<<"Enter Arrival Time: ";

cin>>mat[i][1];

cout<<"Enter Burst Time: ";

cin>>mat[i][2];

cout<<"Before Arrange...\n";

cout<<"Process ID\tArrival Time\tBurst Time\n";


for(int i=0; i<num; i++)

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<<"Process ID\tArrival Time\tBurst Time\tWaiting Time\tTurnaround Time\n";

for(int i=0; i<num; i++)

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

// C++ program to implement Shortest Remaining Time First

// Shortest Remaining Time First (SRTF)

#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\n";

// 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 << "\t\t"

<< proc[i].bt << "\t\t " << wt[i]

<< "\t\t " << tat[i] << endl;

cout << "\nAverage waiting time = "

<< (float)total_wt / (float)n;

cout << "\nAverage 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
#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

//Implementation fo SHORTEST JOB FIRST Using C++

#include <iostream>
#include <algorithm>

using namespace std;

int ab;

typedef struct schedule

string pro_id;

int at,bt,ct,ta,wt;

/*

artime = Arrival time,

bt = Burst time,

ct = Completion time,

ta = Turn around time,

wt = Waiting time

*/

}schedule;

bool compare(schedule a,schedule b)


{

return a.at < b.at;

/* This process will always return TRUE

if above condition comes*/

bool compare2(schedule a,schedule b)

return a.bt < b.bt && a.at <= ab;

/* This process will always return TRUE

if above condition comes*/

int main()

schedule pro[10];

//An array of Processes

int n,i,j;

//n = number of processes, i= iteration variable

cout<<"Enter the number of schedule::";


cin>>n;

cout<<"Enter the schedule id arrival time burst time :::";

for(i=0;i<n;i++)

cin>>pro[i].pro_id;

cin>>pro[i].at;

cin>>pro[i].bt;

/*sort is a predefined funcion defined in algorithm.h header


file,

it will sort the processes according to their arrival time*/

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++)

//before executing make it in one statement


cout<<pro[i].pro_id<<"\t"<<pro[i].at<<"\t"<<pro[i].bt

<<"\t"<<pro[i].ct<<"\t"<<pro[i].ta<<"\t"<<pro[i].wt;

cout<<endl;

return 0;

2.SJF CODE

1. //shortest job First >> SJF


2.
3. #include<iostream>
4.  
5. using namespace std;
6.  
7. int main()
8. {
9.     int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
10.     cout<<"Enter Total Number of Process:";
11.     cin>>n;
12.  
13.     cout<<"\nEnter Burst Time and Priority\n";
14.     for(i=0;i<n;i++)
15.     {
16.         cout<<"\nP["<<i+1<<"]\n";
17.         cout<<"Burst Time:";
18.         cin>>bt[i];
19.         cout<<"Priority:";
20.         cin>>pr[i];
21.         p[i]=i+1;           //contains process number
22.     }
23.  
24.     //sorting burst time, priority and process number in ascending order using
selection sort
25.     for(i=0;i<n;i++)
26.     {
27.         pos=i;
28.         for(j=i+1;j<n;j++)
29.         {
30.             if(pr[j]<pr[pos])
31.                 pos=j;
32.         }
33.  
34.         temp=pr[i];
35.         pr[i]=pr[pos];
36.         pr[pos]=temp;
37.  
38.         temp=bt[i];
39.         bt[i]=bt[pos];
40.         bt[pos]=temp;
41.  
42.         temp=p[i];
43.         p[i]=p[pos];
44.         p[pos]=temp;
45.     }
46.  
47.     wt[0]=0;            //waiting time for first process is zero
48.  
49.     //calculate waiting time
50.     for(i=1;i<n;i++)
51.     {
52.         wt[i]=0;
53.         for(j=0;j<i;j++)
54.             wt[i]+=bt[j];
55.  
56.         total+=wt[i];
57.     }
58.  
59.     avg_wt=total/n;      //average waiting time
60.     total=0;
61.  
62.     cout<<"\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time";
63.     for(i=0;i<n;i++)
64.     {
65.         tat[i]=bt[i]+wt[i];     //calculate turnaround time
66.         total+=tat[i];
67.         cout<<"\nP["<<p[i]<<"]\t\t  "<<bt[i]<<"\t\t    "<<wt[i]<<"\t\t\t"<<tat[i];
68.     }
69.  
70.     avg_tat=total/n;     //average turnaround time
71.     cout<<"\n\nAverage Waiting Time="<<avg_wt;
72.     cout<<"\nAverage Turnaround Time="<<avg_tat;
73.  
74.     return 0;
75. }
3.SJF CODE
// C++ program to implement Shortest Job first
#include<bits/stdc++.h>
using namespace std;
  
struct Process
{
   int pid; // Process ID
   int bt;  // Burst Time
};
  
// This function is used for sorting all
// processes in increasing order of burst
// time
bool comparison(Process a, Process b)
{
     return (a.bt < b.bt);
}
  
// Function to find the waiting time for all
// processes
void findWaitingTime(Process proc[], int n, int wt[])
{
    // waiting time for first process is 0
    wt[0] = 0;
  
    // calculating waiting time
    for (int i = 1; i < n ; i++ )
        wt[i] = proc[i-1].bt + wt[i-1] ;
}
  
// 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 turn
    // around 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}, {2, 8}, {3, 7}, {4, 3}};
    int n = sizeof proc / sizeof proc[0];
  
    // Sorting processes by burst time.
    sort(proc, proc + n, comparison);
  
    cout << "Order in which process gets executed ";
    for (int i = 0 ; i < n; i++)
        cout << proc[i].pid <<" ";
  
    findavgTime(proc, n);
    return 0;
}

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;

void SJF_NP(int n, int burst[], int arrival[], int throughput)


{
cout << "Output for SJF_Non_Preemptive scheduling algorithm" << endl;

int i, j, temp, temp2;


double tot, avgwait, avgturnaround, avgresponse, tp;
//array instantiations
int start[n], end[n], wait[n];

//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)
//**************************************

//Source Code produced by Adri Jovin.J.J


#include<stdio.h>
main()
{
float avgwt,avgtt;
char pname[10][10],c[10][10];
int wt[10],tt[10],bt[10],at[10],t,q,i,n,sum=0,sbt=0,ttime,j,ss=0;
printf("\n\n Enter the number of processes:");
scanf("%d",&n);
printf("\n\n Enter the NAME,BURSTTIME and ARRIVALTIME of the processes");
for(i=0;i<n;i++)
{
printf("\n\n NAME :");
scanf("%s",&pname[i]);
printf("\n\nBURST TIME :");
scanf("%d",&bt[i]);
printf("\n\n ARRIVAL TIME :");
scanf("%d",&at[i]);
}
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
if(at[i]==at[j])
if(bt[i]>bt[j])
{
t=at[i];
at[i]=at[j];
at[j]=t;
q=bt[i];
bt[i]=bt[j];
bt[j]=q;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
if(at[i]!=at[j])
if(bt[i]>bt[j])
{
t=at[i];
at[i]=at[j];
at[j]=t;
q=bt[i];
bt[i]=bt[j];
bt[j]=q;
strcpy(c[i],pname[i]);
strcpy(pname[i],pname[j]);
strcpy(pname[j],c[i]);
}
}
wt[0]=0;
for(i=0;i<n;i++)
{
wt[i+1]=wt[i]+bt[i];
sum=sum+(wt[i]-at[i]);
sbt=sbt+(wt[i+1]-at[i]);
tt[i]=wt[i]+bt[i];
ss=ss+bt[i];
}
printf("\n\n GANTT CHART");
printf("\n\n
------------------------------------------------------------------\n");
for(i=0;i<n;i++)
{
printf("|\t%s\t",pname[i]);
sbt=sbt+wt[i+1];
tt[i]=wt[i]+bt[i];
ss=ss+bt[i];
}
printf("\n\nGANTT CHART");
printf("\n-----------------------------------------------------------------
---\n");
for(i=0;i<n;i++)
{
printf("|\t%s\t",pname[i]);
}
printf("\n-----------------------------------------------------------------
---\n");
for(i=0;i<n;i++)
{
printf("%d\t\t",wt[i]);
}
printf("%d\n",ss);
printf("\n-----------------------------------------------------------------
---\n");
printf("\n\n Total WAITING TIME of the process=%d",sum);
printf("\n\nTotal TURNAROUND TIME of the process=%d",sbt);
avgwt=(float)sum/n;
avgtt=(float)sbt/n;
printf("\n\nAverage WAITING TIME of the process=%f",avgwt);
printf("\n\nAverage TURNAROUND TIME of the process=%f",avgtt);
}

Das könnte Ihnen auch gefallen