You are on page 1of 7

OS Lab

Name: Adhish Deb


Reg.no. : 201900087
Sec: A

1.
#include <bits/stdc++.h>
using namespace std;

struct Process {
int pid; // Process ID
int bt; // Burst Time
int art; // Arrival Time
};

// Function to find the waiting time for all


// processes
void findWaitingTime(Process proc[], int n, int wt[])

{
int rt[n];

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


rt[i] = proc[i].bt;

int complete = 0, t = 0, minm = INT_MAX;


int shortest = 0, finish_time;
bool check = false;

// Process until all processes gets


while (complete != n) {

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


if ((proc[j].art <= t) &&
(rt[j] < minm) && rt[j] > 0) {
minm = rt[j];
shortest = j;
check = true;
}
}

if (check == false) {
t++;
continue;
}
// Update minimum
minm = rt[shortest];
if (minm == 0)
minm = INT_MAX;

if (rt[shortest] == 0) {

complete++;
check = false;

// Find finish time of current


// process
finish_time = t + 1;

// Calculate waiting time


wt[shortest] = finish_time -
proc[shortest].bt -
proc[shortest].art;

if (wt[shortest] < 0)
wt[shortest] = 0;
}
// Increment time
t++;
}
}

// Function to calculate turn around time


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

// Function to calculate average time


void findavgTime(Process proc[], int n)
{
int wt[n], tat[n], total_wt = 0,
total_tat = 0;

// Function to find waiting time of all processes

findWaitingTime(proc, n, wt);

findTurnAroundTime(proc, n, wt, tat);


cout << "Processes "
<< " Burst time "
<< " Waiting time "
<< " Turn around time\n";

// Calculate total waiting time and total turnaround time


for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
cout << " " << proc[i].pid << "\t\t"
<< proc[i].bt << "\t\t " << wt[i]
<< "\t\t " << tat[i] << endl;
}

cout << "\nAverage waiting time = "


<< (float)total_wt / (float)n;
cout << "\nAverage turn around time = "
<< (float)total_tat / (float)n;
}

// Driver code
int main()
{
Process proc[] = { { P1, 5, 5 }, { P2, 4, 6},
{ P3, 3, 7 }, { P4, 1, 9 }, {P5,2,2 }, {P6,6,3} };
int n = sizeof(proc) / sizeof(proc[0]);

findavgTime(proc, n);
return 0;
}
2.

#include<stdio.h>
#include <stdlib.h>

//Function to find waiting time for each process


void waitingTime(int processes[],int execution_time[],int wt[],int n)
{
wt[0] = 0;

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


{
wt[i] = execution_time[i-1] + wt[i-1] ;
}
}

//Fuction to prepare and display Gannt Chart


void ganttchart(int processes[], int execution_time[],int wt[],int n)
{
//FILE * output;
//output = fopen("output.txt", "w");

printf("Gantt Chart:\n");
//fprintf(output,"Gantt Chart:\n");
for (int i=0; i<n; i++)
{
for(int j=0;j<execution_time[i];j++)
{
if(j == 0)
{
printf("|");
//fprintf(output,"|");
}

if(j == (execution_time[i]/2))
{
printf("J%d",processes[i]);
//fprintf(output,"J%d",processes[i]);
}

printf("-");
//fprintf(output,"-");
}

if(i == (n-1))
{
printf("|\n");
//fprintf(output,"|\n");
}

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


{
for(int j=0;j<execution_time[i];j++)
{
if(j == 0)
{
printf("%d",wt[i]);
//fprintf(output,"%d",wt[i]);
}

if(j == (execution_time[i]/2))
{
printf(" ");
//fprintf(output," ");
}

if(j<(execution_time[i]-1))
{
printf(" ");
//fprintf(output," ");
}
}

if(i == (n-1))
{
printf(" %d",wt[i]+execution_time[i]);
//fprintf(output," %d",wt[i]+execution_time[i]);
}

//fclose(output);

//Function to find average waiting time the given processes


void avgWaitingTime( int processes[], int execution_time[],int n)
{
int wt[n],total_wt = 0;
waitingTime(processes, execution_time, wt,n);

ganttchart(processes,execution_time,wt,n);

//FILE * output;
//output = fopen("output.txt", "a");

printf("\n\nWaiting time of Each Process:\n");


//fprintf(output,"\n\nWaiting time of Each Process:\n");
for (int i=0; i<n; i++)
{
printf("J%d = %d\n",processes[i],wt[i]);
//fprintf(output,"J%d = %d\n",processes[i],wt[i]);
}
for (int i=0; i<n; i++)
{
total_wt = total_wt + wt[i];
}

float avg_wt = (float)total_wt / (float)n;


printf("\nAverage waiting time = %d/%d =
%f",total_wt,n,avg_wt);
//fprintf(output,"\nAverage waiting time = %d/%d = %f",total_wt,n,avg_wt);
//fclose(output);
}

//Function to schedule processes using shortest job first algorithm


void sjf_schedule(int *processes,int *execution_time,int n)
{
int tmp;
for (int i = 0 ; i < (n - 1); i++)
{
for (int j = 0 ; j < (n - i - 1); j++)
{
if (execution_time[j] > execution_time[j+1])
{
tmp = execution_time[j];
execution_time[j] = execution_time[j+1];
execution_time[j+1] = tmp;

tmp = processes[j];
processes[j] = processes[j+1];
processes[j+1] = tmp;
}
}
}
}
int main()
{
int n;
printf("Enter number of processes : ");
scanf("%d",&n);

int processes[n],execution_time[n],tmp;

//FILE * input;
//input = fopen("input.txt", "w");

//fprintf(input,"Process Name Execution Time\n ");


for(int i=0;i<n;i++)
{
printf("Enter process id :");
scanf("%d",&processes[i]);
//fprintf(input, " J%d ", processes[i]);
printf("Enter execution time for process %d :",i+1);
scanf("%d",&execution_time[i]);
//fprintf(input, " %d \n ", execution_time[i]);
}
//fclose(input);

sjf_schedule(processes,execution_time,n);
avgWaitingTime(processes,execution_time,n);
return 0;
}

You might also like