0% found this document useful (0 votes)
36 views

Lab Assignment

The document describes two tasks for an operating systems lab assignment. Task 1 involves writing code to calculate waiting times and average times for processes using a Gantt chart scheduling algorithm. Task 2 involves writing code to implement a priority scheduling algorithm to generate a Gantt chart showing start and completion times of processes. The code provided implements functions for calculating waiting times, turnaround times, and displays the Gantt chart output.

Uploaded by

Noorulain Memon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Lab Assignment

The document describes two tasks for an operating systems lab assignment. Task 1 involves writing code to calculate waiting times and average times for processes using a Gantt chart scheduling algorithm. Task 2 involves writing code to implement a priority scheduling algorithm to generate a Gantt chart showing start and completion times of processes. The code provided implements functions for calculating waiting times, turnaround times, and displays the Gantt chart output.

Uploaded by

Noorulain Memon
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Operating System

Spring 2021
Lab Assignment

Task1:
Gantt chart

P1 P2 P3 P5 P4

0 24 46 53 56 59

Code:
import java.text.ParseException;
class GFG {
// Function to find the waiting time for all
// processes
static void findWaitingTime(int processes[], int n, int bt[], int wt[]) {
// waiting time for first process is 0
wt[0] = 0;
Operating System
Spring 2021
Lab Assignment

// calculating waiting time


for (int i = 1; i < n; i++) {
wt[i] = bt[i - 1] + wt[i - 1];
}
}
//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;
//Function to find waiting time of all processes
findWaitingTime(processes, n, bt, wt);
//Function to find turn around time for all processes
findTurnAroundTime(processes, n, bt, wt, tat);
//Display processes along with all details
System.out.printf("Processes Burst time Waiting"
+" time Turn around time\n");
// 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];
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);
// Driver code
public static void main(String[] args) throws ParseException {
//process id's
int processes[] = {1, 2, 3, 4, 5};
int n = processes.length;
Operating System
Spring 2021
Lab Assignment

//Burst time of all processes


int burst_time[] = {24, 22, 7, 3, 3};
findavgTime(processes, n, burst_time);
}
}

Task2:
#include <bits/stdc++.h>
using namespace std;
#define totalprocess 5
// Making a struct to hold the given input
struct process
{
int at, bt, pr, pno;
};
process proc[50];
/*
Writing comparator function to sort according to priority if
arrival time is same
*/
bool
comp(process a, process b)
{
if (a.at == b.at)
{
return a.pr < b.pr;
}
Else
{
return a.at < b.at;
}
}
// Using FCFS Algorithm to find Waiting time
Operating System
Spring 2021
Lab Assignment

void
get_wt_time (int wt[])
{
// declaring service array that stores cumulative burst time
int service[50];
// Initilising initial elements of the arrays
service[0] = proc[0].at;
wt[0] = 0;
for (int i = 1; i < totalprocess; i++)
{
service[i] = proc[i - 1].bt + service[i - 1];
wt[i] = service[i] - proc[i].at;
// If waiting time is negative, change it into zero
if (wt[i] < 0)
{
wt[i] = 0;
}
}
}
Operating System
Spring 2021
Lab Assignment

void
get_tat_time (int tat[], int wt[])
{
// Filling turnaroundtime array
for (int i = 0; i < totalprocess; i++)
{
tat[i] = proc[i].bt + wt[i];
}
}
void
findgc ()
{
//Declare waiting time and turnaround time array
int wt[50], tat[50];
double wavg = 0, tavg = 0;
// Function call to find waiting time array
get_wt_time (wt);
//Function call to find turnaround time
get_tat_time (tat, wt);
int stime[50], ctime[50];
stime[0] = proc[0].at;
ctime[0] = stime[0] + tat[0];
// calculating starting and ending time
for (int i = 1; i < totalprocess; i++)
{
stime[i] = ctime[i - 1];
ctime[i] = stime[i] + tat[i] - wt[i];
}
cout <<
"Process_no\tStart_time\tComplete_time\tTurn_Around_Time\tWaiting_Time"
<<
endl;
// display the process details
for (int i = 0; i < totalprocess; i++)
Operating System
Spring 2021
Lab Assignment

{
wavg += wt[i];
tavg += tat[i];
cout << proc[i].pno << "\t\t" <<
stime[i] << "\t\t" << ctime[i] << "\t\t" <<
tat[i] << "\t\t\t" << wt[i] << endl;
}
// display the average waiting time
//and average turn around time
cout << "Average waiting time is : ";
cout << wavg / (float) totalprocess << endl;
cout << "average turnaround time : ";
cout << tavg / (float) totalprocess << endl;
}
int
main ()
{
int arrivaltime[] = { 0, 6, 11, 7, 9 };
int bursttime[] = { 20, 11, 3, 6, 5 };
int priority[] = { 0, 3, 1, 2, 4 };
for (int i = 0; i < totalprocess; i++)
{
proc[i]. at = arrivaltime[i];
proc[i].bt = bursttime[i];
proc[i].pr = priority[i];
proc[i].pno = i + 1;
}
//Using inbuilt sort function
sort (proc, proc + totalprocess, comp);
//Calling function findgc for finding Gantt Chart
findgc ();
return 0;
}
Operating System
Spring 2021
Lab Assignment

Gantt chart

P1 P3
P1PP1 P4 P2 P5

0 20 23 29 40 45

You might also like