0% found this document useful (0 votes)
13 views2 pages

Practical 1

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)
13 views2 pages

Practical 1

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/ 2

Practical 1: Write a program to simulate CPU Scheduling Algorithms: FCFS, SJF

(Preemptive), Priority (Non-Preemptive) and Round Robin (Preemptive).

SJF:

#include<stdio.h>

int main() {
int p[20], bt[20], wt[20], tat[20], n, i, j, temp;
float wtavg = 0, tatavg = 0;
printf("Onkar lonsane Roll no = 28");

printf("\nEnter the number of processes: ");


scanf("%d", &n);

// Input burst times for each process and initialize process numbers
printf("\nEnter Burst Time for each process:\n");
for(i = 0; i < n; i++) {
p[i] = i + 1; // Process numbers from 1 to n
printf("P%d: ", p[i]);
scanf("%d", &bt[i]);
}

// Sort processes based on burst time using Bubble Sort


for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(bt[j] > bt[j + 1]) {
// Swap burst times
temp = bt[j];
bt[j] = bt[j + 1];
bt[j + 1] = temp;

// Swap process numbers accordingly


temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}

// Calculate waiting time for each process


wt[0] = 0; // First process has 0 waiting time
for(i = 1; i < n; i++) {
wt[i] = 0;
for(j = 0; j < i; j++) {
wt[i] += bt[j];
}
}

// Calculate turnaround time for each process


for(i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}

// Calculate average waiting time and average turnaround time


for(i = 0; i < n; i++) {
wtavg += wt[i];
tatavg += tat[i];
}
wtavg /= n;
tatavg /= n;

// Display results
printf("\nProcess\tBurst Time\tWaiting Time\tTurnaround Time\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t\t%d\t\t%d\n", p[i], bt[i], wt[i], tat[i]);
}
printf("\nAverage Waiting Time: %.2f", wtavg);
printf("\nAverage Turnaround Time: %.2f\n", tatavg);

return 0;
}

Output:

/tmp/q8EEUuwxue.o
Onkar lonsane Roll no = 28
Enter the number of processes: 3

Enter Burst Time for each process:


P1: 3
P2: 4
P3: 5

Process Burst Time Waiting Time Turnaround Time


P1 3 0 3
P2 4 3 7
P3 5 7 12

Average Waiting Time: 3.33


Average Turnaround Time: 7.33

=== Code Execution Successful ===

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy