You are on page 1of 47

ASSIGNMENT

ON

Operating Systems
Laboratory

COURSE CODE: CSE-302

COURSE TITLE: Operating Systems Laboratory

SUBMITTED BY:

NAME: Rubaiya Reza Sohana

ID: 316

BATCH: 47

SESSION: 2018

SEMESTER: 3rd year 1st semester

INSTITUTION: Dept. of Computer Science & Engineering, Jahangirnagar University

SUBMITTED TO:

Bulbul Ahammad

Lecturer

Department of Computer Science & Engineering, Jahangirnagar University

SUBMISSION DATE: 15-12-2020


First Come First Served (FCFS)
Code:

#include<stdio.h>

int main(){

int bt[10]={0},at[10]={0},tat[10]={0},wt[10]={0},ct[10]={0};

int n,sum=0;

float totalTAT=0,totalWT=0;

printf("Enter number of processes ");

scanf("%d",&n);

printf("Enter arrival time and burst time for each process\n\n");

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

{
printf("Arrival time of process[%d] ",i+1);

scanf("%d",&at[i]);

printf("Burst time of process[%d] ",i+1);

scanf("%d",&bt[i]);

printf("\n");

//calculate completion time of processes

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

sum+=bt[j];

ct[j]+=sum;

//calculate turnaround time and waiting times


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

tat[k]=ct[k]-at[k];

totalTAT+=tat[k];

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

wt[k]=tat[k]-bt[k];

totalWT+=wt[k];

printf("Solution: \n\n");

printf("P#\t AT\t BT\t CT\t TAT\t WT\t\n\n");

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

printf("P%d\t %d\t %d\t %d\t %d\t


%d\n",i+1,at[i],bt[i],ct[i],tat[i],wt[i]);

}
printf("\n\nAverage Turnaround Time = %f\n",totalTAT/n);

printf("Average WT = %f\n\n",totalWT/n);

return 0;

Image:
Shortest Job First (SJF)- Preemptive
Code:

#include<bits/stdc++.h>

using namespace std;

struct process{

int process_id;

int arrival;

int burst;

int start_time;

int completion;

int tat;

int waiting;

};

int main()
{

int p_no;

struct process p[10];

float avg_waiting;

int total_waiting = 0;

int mark[10];

int burst_remaining[100];

memset(mark,0,sizeof(mark));

int i;

cout<<setprecision(2)<<fixed;

cout<<"Enter the number of processes ";

cin>>p_no;

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

cout<<"Enter Arrival Time for process "<<i+1<< " - ";

cin>>p[i].arrival;

cout<<endl;
}

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

cout<<"Enter Burst Time for process "<<i+1<<" - ";

cin>>p[i].burst;

p[i].process_id = i+1;

burst_remaining[i]=p[i].burst;

cout<<endl;

int current_time = 0;

int completed = 0;

while (completed!=p_no)

int id = -1;

int maxbur=10000;

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

{
if(p[i].arrival<=current_time && mark[i]==0)

if(burst_remaining[i]<maxbur)

maxbur=burst_remaining[i];

id=i;

if(burst_remaining[i]==maxbur)

if(p[i].arrival<p[id].arrival)

maxbur = burst_remaining[i];

id=i;

if(id!= -1)
{

if(burst_remaining[id]==p[id].burst)

p[id].start_time=current_time;

burst_remaining[id] -= 1;

current_time++;

if(burst_remaining[id]==0)

p[id].completion = current_time;

p[id].tat=p[id].completion - p[id].arrival;

p[id].waiting = p[id].tat - p[id].burst;

total_waiting=total_waiting+p[id].waiting;

mark[id]=1;

completed++;
}

else
{

current_time++;

avg_waiting=(float) total_waiting/p_no;

cout<<endl;

cout<<endl;

cout<<"process\t"<<"Arrival\t"<<"Burst\t"<<"Start\t"<<"Completion\t"<<"TurnAr
ound\t"<<"Waiting\t"<<endl;

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

{
cout<<"P"<<p[i].process_id<<"\t"<<p[i].arrival<<"\t"<<p[i].burst<<"\t"<<p[i].start
_time<<"\t\t"<<p[i].completion<<"\t\t"<<p[i].tat<<"\t\t"<<p[i].waiting<<"\t"<<"\
n"<<endl;

cout<<"Total Waiting Time :" <<total_waiting<<endl;

cout<<"Average Waiting Time : " <<avg_waiting<<endl;

Image:
Shortest Job First (SJF)-Non Preemptive
Code:

#include<bits/stdc++.h>

using namespace std;

struct process{

int process_id;

int arrival;

int burst;

int start_time;

int completion;

int tat;

int waiting;

};

int main()

{
int p_no;

struct process p[10];

float avg_waiting;

int total_waiting = 0;

int mark[10];

memset(mark,0,sizeof(mark));

int i;

cout<<setprecision(2)<<fixed;

cout<<"Enter the number of processes ";

cin>>p_no;

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

cout<<"Enter Arrival Time for process "<<i+1<< " - ";

cin>>p[i].arrival;

cout<<endl;

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


{

cout<<"Enter Burst Time for process "<<i+1<<" - ";

cin>>p[i].burst;

p[i].process_id = i+1;

cout<<endl;

int current_time = 0;

int completed = 0;

while (completed!=p_no)

int id = -1;

int maxbur=10000;

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

if(p[i].arrival<=current_time && mark[i]==0)

if(p[i].burst<maxbur)
{

maxbur=p[i].burst;

id=i;

if(p[i].burst==maxbur)

if(p[i].arrival<p[id].arrival)

maxbur = p[i].burst;

id=i;

if(id!= -1)

p[id].start_time=current_time;

p[id].completion = p[id].start_time + p[id].burst;


p[id].tat=p[id].completion - p[id].arrival;

p[id].waiting = p[id].tat - p[id].burst;

total_waiting=total_waiting+p[id].waiting;

mark[id]=1;

completed++;

current_time=p[id].completion;

else
{

current_time++;

avg_waiting=(float) total_waiting/p_no;

cout<<endl;
cout<<endl;

cout<<"process\t"<<"Arrival\t"<<"Burst\t"<<"Start\t"<<"Completion\t"<<"TurnAr
ound\t"<<"Waiting\t"<<endl;

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

cout<<"P"<<p[i].process_id<<"\t"<<p[i].arrival<<"\t"<<p[i].burst<<"\t"<<p[i].start
_time<<"\t\t"<<p[i].completion<<"\t\t"<<p[i].tat<<"\t\t"<<p[i].waiting<<"\t"<<"\
n"<<endl;

cout<<"Total Waiting Time :" <<total_waiting<<endl;

cout<<"Average Waiting Time : " <<avg_waiting<<endl;

Image:

Priority Scheduling –Non Preemptive


Code:

#include<bits/stdc++.h>

using namespace std;

struct process{

int process_id;

int arrival;

int burst;

int start_time;

int completion;

int tat;

int waiting;

int priority;

};

int main()
{

int p_no;

struct process p[10];

float avg_waiting,avg_tat;

int total_waiting = 0;

int total_tat=0;

int mark[10];

memset(mark,0,sizeof(mark));

int i;

cout<<setprecision(2)<<fixed;

cout<<"Enter the number of processes ";

cin>>p_no;

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

cout<<"Enter Arrival Time for process "<<i+1<< " - ";

cin>>p[i].arrival;

cout<<endl;
cout<<"Enter Burst Time for process "<<i+1<<" - ";

cin>>p[i].burst;

p[i].process_id = i+1;

cout<<endl;

cout<<"Enter Priority for process "<<i+1<<" - ";

cin>>p[i].priority;

cout<<endl;

int current_time = 0;

int completed = 0;

while (completed!=p_no)

int id = -1;

int maxprior=10000;

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

if(p[i].arrival<=current_time && mark[i]==0)

{
if(p[i].priority<maxprior)

maxprior=p[i].priority;

id=i;

if(p[i].priority==maxprior)

if(p[i].arrival<p[id].arrival)

maxprior = p[i].priority;

id=i;

if(id!= -1)

p[id].start_time=current_time;
p[id].completion = p[id].start_time + p[id].burst;

p[id].tat=p[id].completion - p[id].arrival;

p[id].waiting = p[id].tat - p[id].burst;

total_waiting=total_waiting+p[id].waiting;

total_tat=total_tat+p[id].tat;

mark[id]=1;

completed++;

current_time=p[id].completion;

else
{

current_time++;

avg_waiting=(float) total_waiting/p_no;
avg_tat=(float) total_tat/p_no;

cout<<endl;

cout<<endl;

cout<<"process\t"<<"Arrival\t"<<"Burst\t"<<"Start\t"<<"Completion\t"<<"TurnAr
ound\t"<<"Waiting\t"<<endl;

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

cout<<"P"<<p[i].process_id<<"\t"<<p[i].arrival<<"\t"<<p[i].burst<<"\t"<<p[i].start
_time<<"\t\t"<<p[i].completion<<"\t\t"<<p[i].tat<<"\t\t"<<p[i].waiting<<"\t"<<"\
n"<<endl;

cout<<"Total Waiting Time :" <<total_waiting<<endl;

cout<<"Average Waiting Time : " <<avg_waiting<<endl;

cout<<"Total Turnaround Time :" <<total_tat<<endl;

cout<<"Average Turnaround Time : " <<avg_tat<<endl;

}
Image:

Priority Scheduling – Preemptive


Code:

#include <iostream>

#include <algorithm>

#include <iomanip>

#include <string.h>

using namespace std;

struct process {

int pid;

int arrival_time;

int burst_time;

int priority;

int start_time;

int completion_time;

int turnaround_time;

int waiting_time;
int response_time;

};

int main() {

int n;

struct process p[100];

float avg_turnaround_time;

float avg_waiting_time;

float avg_response_time;

float cpu_utilisation;

int total_turnaround_time = 0;

int total_waiting_time = 0;

int total_response_time = 0;

int total_idle_time = 0;

float throughput;

int burst_remaining[100];

int is_completed[100];

memset(is_completed,0,sizeof(is_completed));
cout << setprecision(2) << fixed;

cout<<"Enter the number of processes: ";

cin>>n;

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

cout<<"Enter arrival time of process "<<i+1<<": ";

cin>>p[i].arrival_time;

cout<<"Enter burst time of process "<<i+1<<": ";

cin>>p[i].burst_time;

cout<<"Enter priority of the process "<<i+1<<": ";

cin>>p[i].priority;

p[i].pid = i+1;

burst_remaining[i] = p[i].burst_time;

cout<<endl;

int current_time = 0;

int completed = 0;

int prev = 0;
while(completed != n) {

int idx = -1;

int mx = 1000;

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

if(p[i].arrival_time <= current_time && is_completed[i] == 0) {

if(p[i].priority < mx) {

mx = p[i].priority;

idx = i;

if(p[i].priority == mx) {

if(p[i].arrival_time < p[idx].arrival_time) {

mx = p[i].priority;

idx = i;

if(idx != -1) {
if(burst_remaining[idx] == p[idx].burst_time) {

p[idx].start_time = current_time;

total_idle_time += p[idx].start_time - prev;

burst_remaining[idx] -= 1;

current_time++;

prev = current_time;

if(burst_remaining[idx] == 0) {

p[idx].completion_time = current_time;

p[idx].turnaround_time = p[idx].completion_time - p[idx].arrival_time;

p[idx].waiting_time = p[idx].turnaround_time - p[idx].burst_time;

p[idx].response_time = p[idx].start_time - p[idx].arrival_time;

total_turnaround_time += p[idx].turnaround_time;

total_waiting_time += p[idx].waiting_time;

total_response_time += p[idx].response_time;

is_completed[idx] = 1;

completed++;
}

else {

current_time++;

int min_arrival_time = 10000000;

int max_completion_time = -1;

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

min_arrival_time = min(min_arrival_time,p[i].arrival_time);

max_completion_time = max(max_completion_time,p[i].completion_time);

avg_turnaround_time = (float) total_turnaround_time / n;

avg_waiting_time = (float) total_waiting_time / n;

avg_response_time = (float) total_response_time / n;

cpu_utilisation = ((max_completion_time - total_idle_time) / (float)


max_completion_time )*100;

throughput = float(n) / (max_completion_time - min_arrival_time);


cout<<endl<<endl;

cout<<"#P\t"<<"AT\t"<<"BT\t"<<"PRI\t"<<"ST\t"<<"CT\t"<<"TAT\t"<<"WT\t"<<"R
T\t"<<"\n"<<endl;

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

cout<<p[i].pid<<"\t"<<p[i].arrival_time<<"\t"<<p[i].burst_time<<"\t"<<p[i].priorit
y<<"\t"<<p[i].start_time<<"\t"<<p[i].completion_time<<"\t"<<p[i].turnaround_ti
me<<"\t"<<p[i].waiting_time<<"\t"<<p[i].response_time<<"\t"<<"\n"<<endl;

cout<<"Average Turnaround Time = "<<avg_turnaround_time<<endl;

cout<<"Average Waiting Time = "<<avg_waiting_time<<endl;

cout<<"Average Response Time = "<<avg_response_time<<endl;

cout<<"CPU Utilization = "<<cpu_utilisation<<"%"<<endl;

cout<<"Throughput = "<<throughput<<" process/unit time"<<endl;

}
Image:

Round Robin
Code:

#include<stdio.h>

#include<conio.h>

void main()

int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];

float avg_wt, avg_tat;

printf(" Total number of process in the system: ");

scanf("%d", &NOP);

y = NOP;

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

printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t");

scanf("%d", &at[i]);

printf(" \nBurst time is: \t");

scanf("%d", &bt[i]);

temp[i] = bt[i];

printf("Enter the Time Quantum for the process: \t");

scanf("%d", &quant);

printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");

for(sum=0, i = 0; y!=0; )

if(temp[i] <= quant && temp[i] > 0) // define the conditions

sum = sum + temp[i];

temp[i] = 0;

count=1;

else if(temp[i] > 0)


{

temp[i] = temp[i] - quant;

sum = sum + quant;

if(temp[i]==0 && count==1)

y--; //decrement the process no.

printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i],


sum-at[i]-bt[i]);

wt = wt+sum-at[i]-bt[i];

tat = tat+sum-at[i];

count =0;

if(i==NOP-1)

i=0;

else if(at[i+1]<=sum)

i++;

}
else

i=0;

// represents the average waiting time and Turn Around time

avg_wt = wt * 1.0/NOP;

avg_tat = tat * 1.0/NOP;

printf("\n Average Waiting Around Time: \t%f", avg_wt);

printf("\n Average Turnaround Time: \t%f", avg_tat);

getch();

Image:
Enter the Arrival and Burst time of the
Process[3] Arrival time is: 3

Burst time is: 3

Enter the Arrival and Burst time of the


Process[4] Arrival time is: o

Burst time is: 7

Enter the Arrival and Burst time of the


Process[5] Arrival time is: I0

Burst time is: 2

Enter the Arrival and Burst time of the


Process[o] Arrival time is: 15

Burst time is: 5


Ente n the T1me Quantum To n the p roe e s s :

P for es s LJo Bu n st T1me TAT Waiting Time


P row e s ?Jo [2] “1 4
s ?Jo [ 3] 3
P row e s
s
P roe e s ?Jo [ 5] 2 4
s
Prove ss ?Jo [o] 5
Process No[1] l0
Prove ss ?Jo [4] 7 15
Ave Page Fla It Ing Around T1me : 7. 1oooâ7
Ave Page Turna found TI me : 11. g ss

You might also like