0% found this document useful (0 votes)
42 views12 pages

Assignment 4

cse

Uploaded by

atharvag062
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)
42 views12 pages

Assignment 4

cse

Uploaded by

atharvag062
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/ 12

Assignment 4

Name: Atharav Gaikwad


Reg. no: 22BCE8897
Lab slot: L53+54

Q) What is FCFS algorithm ?


A) FCFS is a scheduling algorithm that helps operating systems and networks
efficiently and automatically carry out tasks, processes, and requests in the
order in which they are added to the queue. FCFS scheduling is also known as
first-in, first-out (FIFO) or first-come, first-choice (FCFC) scheduling. FCFS
algorithms perform tasks and requests predictably due to their simplicity. FCFS
algorithms are an emulation of real-world customer service situations.

Q) What is SJF ?
A)The shortest job first (SJF) or shortest job next, is a scheduling policy that
selects the waiting process with the smallest execution time to execute next.
SJN, also known as Shortest Job Next (SJN), can be pre-emptive or non-pre-
emptive.

1) FCFS ALGORITHM

import java.text.ParseException;
class GFG {

static void findWaitingTime(int processes[], int n,


int bt[], int wt[]) {
// waiting time for first process is 0
wt[0] = 0;

// calculating waiting time


for (int i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
}

// Function to calculate turn around time


static void findTurnAroundTime(int processes[], int n,
int bt[], int wt[], int tat[]) {
// calculating turnaround time by adding
// bt[i] + wt[i]
for (int i = 0; i < n; i++) {
tat[i] = bt[i] + wt[i];
}
}

//Function to calculate average time


static void findavgTime(int processes[], int n, int bt[]) {
int wt[] = new int[n], tat[] = new int[n];
int total_wt = 0, total_tat = 0;

findWaitingTime(processes, n, bt, wt);

findTurnAroundTime(processes, n, bt, wt, tat);


System.out.printf("Processes Burst time Waiting"
+" time Turn around time\n");

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


total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
System.out.printf(" %d ", (i + 1));
System.out.printf(" %d ", bt[i]);
System.out.printf(" %d", wt[i]);
System.out.printf(" %d\n", tat[i]);
}
float s = (float)total_wt /(float) n;
int t = total_tat / n;
System.out.printf("Average waiting time = %f", s);
System.out.printf("\n");
System.out.printf("Average turn around time = %d ", t);
}

public static void main(String[] args) throws ParseException {


//process id's
int processes[] = {1, 2, 3};
int n = processes.length;
int burst_time[] = {10, 5, 8};
findavgTime(processes, n, burst_time);

}
}

Output:

2) CPU SCHEDULING USING SJF


import java.io.*
import java.util.*;

public class Main {


public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n;
// Matrix for storing Process Id, Burst
// Time, Average Waiting Time & Average
// Turn Around Time.
int[][] A = new int[100][4];
int total = 0;
float avg_wt, avg_tat;
System.out.println("Enter number of process:");
n = input.nextInt();
System.out.println("Enter Burst Time:");
for (int i = 0; i < n; i++) {
// User Input Burst Time and alloting
// Process Id.
System.out.print("P" + (i + 1) + ": ");
A[i][1] = input.nextInt();
A[i][0] = i + 1;
}
for (int i = 0; i < n; i++) {
// Sorting process according to their
// Burst Time.
int index = i;
for (int j = i + 1; j < n; j++) {
if (A[j][1] < A[index][1]) {
index = j;
}
}
int temp = A[i][1];
A[i][1] = A[index][1];
A[index][1] = temp;
temp = A[i][0];
A[i][0] = A[index][0];
A[index][0] = temp;
}
A[0][2] = 0;
// Calculation of Waiting Times
for (int i = 1; i < n; i++) {
A[i][2] = 0;
for (int j = 0; j < i; j++) {
A[i][2] += A[j][1];
}
total += A[i][2];
}
avg_wt = (float)total / n;
total = 0;
// Calculation of Turn Around Time and printing the
// data.
System.out.println("P\tBT\tWT\tTAT");
for (int i = 0; i < n; i++) {
A[i][3] = A[i][1] + A[i][2];
total += A[i][3];
System.out.println("P" + A[i][0] + "\t"
+ A[i][1] + "\t" + A[i][2]
+ "\t" + A[i][3]);
}
avg_tat = (float)total / n;
System.out.println("Average Waiting Time= "
+ avg_wt);
System.out.println("Average Turnaround Time= "
+ avg_tat);
}
}

Output:
C. CPU SCHEDULING USING PRIORITY

PROGRAM:
#include <stdio.h>

int main()
{
int i, n, j, a[10], b[10], pri[10], pro[10], temp1, temp2, temp3;
float avgWT = 0, avgTAT = 0;

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


scanf("%d", &n);

printf("\nEnter the process number, burst time, and priority for


each process:\n");
for(i = 0; i < n; i++)
{
printf("\nEnter the process number: ");
scanf("%d", &pro[i]);
printf("Enter the burst time for process: ");
scanf("%d", &a[i]);
printf("Enter the priority for the process: ");
scanf("%d", &pri[i]);
}
// Sorting processes based on priority (higher number means
higher priority)
for(i = 0; i < n; i++)
{
for(j = i + 1; j < n; j++)
{
if(pri[i] < pri[j])
{
// Swap burst time
temp1 = a[i];
a[i] = a[j];
a[j] = temp1;

// Swap priority
temp2 = pri[i];
pri[i] = pri[j];
pri[j] = temp2;

// Swap process number


temp3 = pro[i];
pro[i] = pro[j];
pro[j] = temp3;
}
}
}

// Initialize waiting time for the first process as 0


b[0] = 0;

printf("\nProcess No\tPriority\tBurst Time\tWaiting


Time\tTurnaround Time\n");

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


{
// Turnaround time is burst time + waiting time
int TAT = a[i] + b[i];

// Calculate average turnaround time


avgTAT += TAT;

// Print process info


printf("%d\t\t%d\t\t%d\t\t%d\t\t%d\n", pro[i], pri[i], a[i], b[i],
TAT);

// Waiting time for the next process


if(i + 1 < n)
b[i + 1] = b[i] + a[i];
// Calculate average waiting time
avgWT += b[i];
}

// Calculate averages
avgWT /= n;
avgTAT /= n;

// Print averages
printf("\nAverage Waiting Time = %.2f", avgWT);
printf("\nAverage Turnaround Time = %.2f\n", avgTAT);

return 0;
}
OUTPUT

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