0% found this document useful (0 votes)
19 views3 pages

FCFS & SJF

Uploaded by

volterbtc00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

FCFS & SJF

Uploaded by

volterbtc00
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

FCFS

Program:
#include<iostream>
using namespace std;

class fcfs {
int a[10], k, b[10], c[10], d[10], n, i, j, sum, total;
float awt, atat;

public:
void getdata() {
k = total = sum = 0;
cout << "Enter the number of processes: ";
cin >> n;

// Input burst times for each process


for(i = 0; i < n; i++) {
cout << "Enter the burst time for process " << i+1 << ": ";
cin >> a[i];
d[i] = i+1; // Process number
}

b[0] = 0;

// Calculate completion times and total waiting time


for(i = 0; i < n; i++) {
k += a[i];
b[i+1] = k; // Burst time at which process i completes
}

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


c[i] = a[i] + b[i]; // Turnaround time for each process
sum += b[i]; // Total waiting time
total += c[i]; // Total turnaround time
}

awt = (float) sum / n; // Average waiting time


atat = (float) total / n; // Average turnaround time
}

void putdata() {
cout << "Process\tBurst time\tWaiting time\tTurnaround time\n";
for(i = 0; i < n; i++) {
cout << d[i] << "\t" << a[i] << "\t\t" << b[i] << "\t\t" << c[i] << "\n";
}

cout << "Average waiting time: " << awt << "\n";
cout << "Average turnaround time: " << atat << "\n";
}
};

int main() {
fcfs obj;
obj.getdata();
obj.putdata();
return 0;
}
Output:

SJF
Program:
#include<stdio.h>

int main() {
int i, n, p[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, min, k = 1, btime = 0;
int bt[10], temp, j, at[10], wt[10], tt[10], ta = 0, sum = 0;
float wavg = 0, tavg = 0, tsum = 0, wsum = 0;

printf(" ------- Shortest Job First Scheduling (NP) -------\n");


printf("\nEnter the number of processes: ");
scanf("%d", &n);

// Input burst time and arrival time for each process


for (i = 0; i < n; i++) {
printf("Enter the burst time of process %d: ", i + 1);
scanf("%d", &bt[i]);
printf("Enter the arrival time of process %d: ", i + 1);
scanf("%d", &at[i]);
}

/* Sorting processes based on Arrival Time */


for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
if (at[i] < at[j]) {
temp = p[j]; p[j] = p[i]; p[i] = temp;
temp = at[j]; at[j] = at[i]; at[i] = temp;
temp = bt[j]; bt[j] = bt[i]; bt[i] = temp;
}
}
}

/* Arranging the table according to Burst time,


Execution time and Arrival Time
Arrival time <= Execution time
*/
for (j = 0; j < n; j++) {
btime = btime + bt[j];
min = bt[k];
for (i = k; i < n; i++) {
if (btime >= at[i] && bt[i] < min) {
temp = p[k]; p[k] = p[i]; p[i] = temp;
temp = at[k]; at[k] = at[i]; at[i] = temp;
temp = bt[k]; bt[k] = bt[i]; bt[i] = temp;
}
}
k++;
}

/* Calculating Waiting time for each process */


wt[0] = 0;
for (i = 1; i < n; i++) {
sum = sum + bt[i - 1];
wt[i] = sum - at[i];
wsum = wsum + wt[i];
}

wavg = (wsum / n);

/* Calculating Turnaround time for each process */


for (i = 0; i < n; i++) {
ta = ta + bt[i];
tt[i] = ta - at[i];
tsum = tsum + tt[i];
}

tavg = (tsum / n);

/* Displaying the results */


printf("\n\nPROCESS\tBURST TIME\tARRIVAL TIME\tWAITING TIME\tTURNAROUND TIME");
for (i = 0; i < n; i++) {
printf("\n p%d\t%d\t\t%d\t\t%d\t\t%d", p[i], bt[i], at[i], wt[i], tt[i]);
}

printf("\n\nAVERAGE WAITING TIME : %.2f", wavg);


printf("\nAVERAGE TURNAROUND TIME : %.2f\n", tavg);

return 0;
}

Output:
------- Shortest Job First Scheduling (NP) -------

Enter the number of processes: 5


Enter the burst time of process 1: 2
Enter the arrival time of process 1: 1
Enter the burst time of process 2: 8
Enter the arrival time of process 2: 3
Enter the burst time of process 3: 1
Enter the arrival time of process 3: 2
Enter the burst time of process 4: 2
Enter the arrival time of process 4: 5
Enter the burst time of process 5: 9
Enter the arrival time of process 5: 4

You might also like