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

Process

The document contains a C# program that implements the Shortest Job First (SJF) scheduling algorithm for process management. It allows users to input process details such as arrival time and burst time, calculates completion, turnaround, and waiting times, and displays the results. Additionally, it provides average turnaround and waiting times and visualizes the process execution in a chart format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Process

The document contains a C# program that implements the Shortest Job First (SJF) scheduling algorithm for process management. It allows users to input process details such as arrival time and burst time, calculates completion, turnaround, and waiting times, and displays the results. Additionally, it provides average turnaround and waiting times and visualizes the process execution in a chart format.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

using System;

using System.Collections.Generic;
using System.Runtime.ConstrainedExecution;

class Process
{
public int PID { get; set; }
public int arrivalTime { get; set; }
public int burstTime { get; set; }
public int completionTime { get; set; }
public int turnaroundTime { get; set; }
public int waitingTime { get; set; }
}

class SJF
{
static void Main(string[] args)
{
try
{
List<Process> processes = new List<Process>();

Console.Write("Enter the number of processes: ");


int n = int.Parse(Console.ReadLine());

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


{
Process process = new Process();

Console.WriteLine($"Details for Process {i + 1}");


process.PID = i + 1;

Console.Write("Arrival Time: ");


process.arrivalTime = int.Parse(Console.ReadLine());

Console.Write("Burst Time: ");


process.burstTime = int.Parse(Console.ReadLine());

processes.Add(process);
}

processes.Sort((p1, p2) =>


p1.arrivalTime == p2.arrivalTime
? p1.burstTime.CompareTo(p2.burstTime)
: p1.arrivalTime.CompareTo(p2.arrivalTime));

int currentTime = 0;
List<Process> processing = new List<Process>();
int index = 0;

while (index < processes.Count || processing.Count > 0)


{

while (index < processes.Count && processes[index].arrivalTime <= currentTime)


{
processing.Add(processes[index]);
index++;
}

if (processing.Count == 0)
{

if (index < processes.Count)


{
currentTime = processes[index].arrivalTime;

}
}
else
{

processing.Sort((p1, p2) => p1.burstTime.CompareTo(p2.burstTime));

Process process = processing[0];


processing.RemoveAt(0);

process.completionTime = currentTime + process.burstTime;


process.turnaroundTime = process.completionTime - process.arrivalTime;
process.waitingTime = process.turnaroundTime - process.burstTime;

currentTime = process.completionTime;
}
}

Console.WriteLine("\nProcess\tArrival Time\tBurst Time\tCompletion Time\tTurnaround


Time\tWaiting Time");
foreach (var process in processes)
{
Console.WriteLine($"{process.PID}\t{process.arrivalTime}\t\t{process.burstTime}\t\
t{process.completionTime}\t\t{process.turnaroundTime}\t\t{process.waitingTime}");
}

double totalTurnaroundTime = 0;
double totalWaitingTime = 0;

foreach (var process in processes)


{
totalTurnaroundTime += process.turnaroundTime;
totalWaitingTime += process.waitingTime;
}

Console.WriteLine($"\nAverage Turnaround Time: {totalTurnaroundTime / n}");


Console.WriteLine($"Average Waiting Time: {totalWaitingTime / n}");
drawingChart(processes);
Console.ReadLine();
}
catch (Exception)
{
Console.WriteLine("Input Error");
Console.ReadKey();
}
}
static void drawingChart(List<Process> processesList)
{
Console.Write("|");
processesList.ForEach(process => Console.Write($" {process.PID} |"));

Console.WriteLine();
Console.Write($"0".PadRight(4));

processesList.ForEach(proc => Console.Write($"{proc.completionTime}".PadRight(4)));

}
}

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