Experiment No. - 1: Aim: Software Used: Language Used: Code

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

ETU807 OSOS LAB STUDENT ID: 17004027

EXPERIMENT NO. – 1
AIM: Write a C program to implement the First Come First Serve Algorithm.
SOFTWARE USED: Dev C++ (version 5.11)
LANGUAGE USED: C
CODE:
#include <stdio.h>
int main()
{
int bt[10],at[10], tat[10],wt[10]={0},ct[10]={0};
int n,i,j,k, sum = 0;
float totalTAT=0, totalWT=0;
printf("Name of student = SARITA G. BIJAWE \n Student ID =
17004027 \n\n");
printf("Enter no. of processes ");
scanf("%d",&n);
for(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");
}
for(j=0;j<n;j++)
{
sum+=bt[j];
ct[j]+=sum;
}
for(k=0;k<n;k++)
{
tat[k]=ct[k]-at[k];
totalTAT+=tat[k];
}
for(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(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]);
ETU807 OSOS LAB STUDENT ID: 17004027

}
printf("\n\nAverage TurnAround Time = %f\n",totalTAT/n);
printf("Average WT = %f\n\n",totalWT/n);
return 0;
}
OUTPUT:

CONCLUSION: The First Come First Serve CPU scheduling Algorithm has been successfully
simulated in this program. The FCFS scheduling is performed on the basis of arrival time of
the processes irrespective of their other parameters.
ETU807 OSOS LAB STUDENT ID: 17004027

EXPERIMENT NO. – 2
AIM: Write a C program to implement the Shortest Job First Algorithm.
SOFTWARE USED: Dev C++ (version 5.11)
LANGUAGE USED: C
CODE:
#include<stdio.h>
int main()
{
int i,n,p[10]={1,2,3,4,5,6,7,8,9,10},min,k=1,btime=0;
int bt[10],temp,j,at[10],wt[10],tt[10],ta=0,sum=0;
float wavg=0,tavg=0,tsum=0,wsum=0;
printf("Name of student = SARITA G. BIJAWE \n Student ID =
17004027 \n\n");

printf("\nEnter the No. of processes :");


scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\tEnter the burst time of process[%d] :",i+1);
scanf(" %d",&bt[i]);
printf("\tEnter the arrival time of process[%d] :",i+1);
scanf(" %d",&at[i]);
}
/*Sorting According to Arrival Time*/
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(at[i]<at[j])
{
temp=p[j];
p[j]=p[i];
p[i]=temp;
temp=at[j];
at[j]=at[i];
at[i]=temp;
temp=bt[j];
bt[j]=bt[i];
bt[i]=temp;
}
}
}
/*Arranging the table according to Burst time,
Execution time and Arrival Time
ETU807 OSOS LAB STUDENT ID: 17004027

Arrival time <= Execution time


*/

for(j=0;j<n;j++)
{
btime=btime+bt[j];
min=bt[k];
for(i=k;i<n;i++)
{
if (btime>=at[i] && bt[i]<min)
{
temp=p[k];
p[k]=p[i];
p[i]=temp;
temp=at[k];
at[k]=at[i];
at[i]=temp;
temp=bt[k];
bt[k]=bt[i];
bt[i]=temp;
}
}
k++;
}
wt[0]=0;
for(i=1;i<n;i++)
{
sum=sum+bt[i-1];
wt[i]=sum-at[i];
wsum=wsum+wt[i];
}
wavg=(wsum/n);
for(i=0;i<n;i++)
{
ta=ta+bt[i];
tt[i]=ta-at[i];
tsum=tsum+tt[i];
}
tavg=(tsum/n);
printf("************************");
printf("\n RESULT:-");
printf("\nProcess\t Burst\t Arrival\t Waiting\t Turn-around"
);
for(i=0;i<n;i++)
{
printf("\n p%d\t %d\t %d\t\t
%d\t\t\t%d",p[i],bt[i],at[i],wt[i],tt[i]);
}
printf("\n\nAVERAGE WAITING TIME : %f",wavg);
ETU807 OSOS LAB STUDENT ID: 17004027

printf("\nAVERAGE TURN AROUND TIME : %f",tavg);


return 0;
}
OUTPUT:

CONCLUSION: The Shortest Job First CPU scheduling Algorithm has been successfully
simulated in this program. The SJF scheduling is performed based on the duration of burst
time of the process and if needed FCFS approach is also used in case of same burst time.
ETU807 OSOS LAB STUDENT ID: 17004027

EXPERIMENT NO. – 3
AIM: Write a C program to implement the Non Pre-emptive Priority Algorithm.
SOFTWARE USED: Dev C++ (version 5.11)
LANGUAGE USED: C
CODE:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0;
int pos,temp,avg_wt,avg_tat;
printf("Name of student = SARITA G. BIJAWE \n Student ID =
17004027 \n\n");

printf("Enter Total Number of Process:");


scanf("%d",&n);
printf("\nEnter Burst Time and Priority\n");
for(i=0;i<n;i++)
{
printf("\nP[%d]\n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //contains process number
}
//sorting burst time, priority and process number in
ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;

temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;

temp=p[i];
p[i]=p[pos];
ETU807 OSOS LAB STUDENT ID: 17004027

p[pos]=temp;
}

wt[0]=0; //waiting time for first process is zero


//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}

avg_wt=total/n; //average waiting time


total=0;

printf("\nProcess\t Burst Time \tWaiting


Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("\nP[%d]\t\t %d\t\t
%d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround time
printf("\n\nAverage Waiting Time=%d",avg_wt);
printf("\nAverage Turnaround Time=%d\n",avg_tat);
return 0;
}

OUTPUT:
ETU807 OSOS LAB STUDENT ID: 17004027

CONCLUSION: The Non pre-emptive Priority CPU scheduling Algorithm has been
successfully simulated in this program. The Priority scheduling is performed based on the
priority assigned to the process and if needed FCFS approach is also used in case of same
priority.
ETU807 OSOS LAB STUDENT ID: 17004027

EXPERIMENT NO. – 4
AIM: Write a C program to implement the Non Pre-emptive Round Robin Algorithm.
SOFTWARE USED: Dev C++ (version 5.11)
LANGUAGE USED: C
CODE:
#include<stdio.h>
int main()
{
int count,j,n,time,remain,flag=0,time_quantum;
int wait_time=0,turnaround_time=0,at[10],bt[10],rt[10];
printf("Name of student = SARITA G. BIJAWE \n Student ID =
17004027 \n\n");
printf("Enter Total Process:\t ");
scanf("%d",&n);
remain=n;
for(count=0;count<n;count++)
{
printf("Enter Arrival Time and Burst Time for Process
Process Number %d :",count+1);
scanf("%d",&at[count]);
scanf("%d",&bt[count]);
rt[count]=bt[count];
}
printf("Enter Time Quantum:\t");
scanf("%d",&time_quantum);
printf("\n\nProcess\t|Turnaround Time|Waiting Time\n\n");
for(time=0,count=0;remain!=0;)
{
if(rt[count]<=time_quantum && rt[count]>0)
{
time+=rt[count];
rt[count]=0;
flag=1;
}
else if(rt[count]>0)
{
rt[count]-=time_quantum;
time+=time_quantum;
}
if(rt[count]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",count+1,time-
at[count],time-at[count]-bt[count]);
wait_time+=time-at[count]-bt[count];
ETU807 OSOS LAB STUDENT ID: 17004027

turnaround_time+=time-at[count];
flag=0;
}
if(count==n-1)
count=0;
else if(at[count+1]<=time)
count++;
else
count=0;
}
printf("\nAverage Waiting Time= %f\n",wait_time*1.0/n);
printf("Avg Turnaround Time = %f",turnaround_time*1.0/n);

return 0;
}

OUTPUT:
ETU807 OSOS LAB STUDENT ID: 17004027

CONCLUSION: The Non pre-emptive Round Robin CPU scheduling Algorithm has been
successfully simulated in this program. The Round Robin scheduling is performed by giving
equal chance to every process using the assigned Time Slice value.
ETU807 OSOS LAB STUDENT ID: 17004027

EXPERIMENT NO. – 5
AIM: Write a C program to implement the Pre-emptive Priority Scheduling Algorithm.
SOFTWARE USED: Dev C++ (version 5.11)
LANGUAGE USED: C
CODE:
#include<stdio.h>

struct process
{
char process_name;
int arrival_time, burst_time, ct, waiting_time,
turnaround_time, priority;
int status;
}process_queue[10];

int limit;

void Arrival_Time_Sorting()
{
struct process temp;
int i, j;
for(i = 0; i < limit - 1; i++)
{
for(j = i + 1; j < limit; j++)
{
if(process_queue[i].arrival_time >
process_queue[j].arrival_time)
{
temp = process_queue[i];
process_queue[i] = process_queue[j];
process_queue[j] = temp;
}
}
}
}

void main()
{
int i, time = 0, burst_time = 0, largest;
char c;
float wait_time = 0, turnaround_time = 0,
average_waiting_time, average_turnaround_time;
printf("Name of student = SARITA G. BIJAWE \n Student ID =
17004027 \n\n");
ETU807 OSOS LAB STUDENT ID: 17004027

printf("\nEnter Total Number of Processes:\t");


scanf("%d", &limit);
for(i = 0, c = 'A'; i < limit; i++, c++)
{
process_queue[i].process_name = c;
printf("\nEnter Details For Process[%C]:\n",
process_queue[i].process_name);
printf("Enter Arrival Time:\t");
scanf("%d", &process_queue[i].arrival_time );
printf("Enter Burst Time:\t");
scanf("%d", &process_queue[i].burst_time);
printf("Enter Priority:\t");
scanf("%d", &process_queue[i].priority);
process_queue[i].status = 0;
burst_time = burst_time +
process_queue[i].burst_time;
}
Arrival_Time_Sorting();
process_queue[9].priority = -9999;
printf("\nProcess Name\tArrival Time\tBurst
Time\tPriority\tWaiting Time");
for(time = process_queue[0].arrival_time; time <
burst_time;)
{
largest = 9;
for(i = 0; i < limit; i++)
{
if(process_queue[i].arrival_time <= time &&
process_queue[i].status != 1 && process_queue[i].priority >
process_queue[largest].priority)
{
largest = i;
}
}
time = time + process_queue[largest].burst_time;
process_queue[largest].ct = time;
process_queue[largest].waiting_time =
process_queue[largest].ct -
process_queue[largest].arrival_time -
process_queue[largest].burst_time;
process_queue[largest].turnaround_time =
process_queue[largest].ct -
process_queue[largest].arrival_time;
process_queue[largest].status = 1;
wait_time = wait_time +
process_queue[largest].waiting_time;
turnaround_time = turnaround_time +
process_queue[largest].turnaround_time;
ETU807 OSOS LAB STUDENT ID: 17004027

printf("\n%c\t\t%d\t\t%d\t\t%d\t\t%d",
process_queue[largest].process_name,
process_queue[largest].arrival_time,
process_queue[largest].burst_time,
process_queue[largest].priority,
process_queue[largest].waiting_time);
}
average_waiting_time = wait_time / limit;
average_turnaround_time = turnaround_time / limit;
printf("\n\nAverage waiting time:\t%f\n",
average_waiting_time);
printf("Average Turnaround Time:\t%f\n",
average_turnaround_time);
}

OUTPUT:
ETU807 OSOS LAB STUDENT ID: 17004027

CONCLUSION: The Pre-emptive Priority scheduling algorithm is successfully


implemented in this program. In Pre-emptive Priority Scheduling, at the time of
arrival of a process in the ready queue, its Priority is compared with the priority of
the other processes present in the ready queue as well as with the one which is
being executed by the CPU at that point of time. The One with the highest priority
among all the available processes will be given the CPU next.
ETU807 OSOS LAB STUDENT ID: 17004027

EXPERIMENT NO. – 6
AIM: Write a C program to implement the Bankers algorithm for the purpose of deadlock
avoidance.

SOFTWARE USED: Dev C++ (version 5.11)


LANGUAGE USED: C
CODE:
#include <stdio.h>
#include <conio.h>

int main()
{
int Max[10][10], need[10][10], alloc[10][10], avail[10],
completed[10], safeSequence[10];
int p, r, i, j, process, count;
count = 0;
printf("Name of student = SARITA G. BIJAWE \n Student ID =
17004027 \n\n");

printf("Enter the no of processes : ");


scanf("%d", &p);

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


completed[i] = 0;

printf("\n\nEnter the no of resources : ");


scanf("%d", &r);

printf("\n\nEnter the Max Matrix for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ", i + 1);
for(j = 0; j < r; j++)
scanf("%d", &Max[i][j]);
}

printf("\n\nEnter the allocation for each process : ");


for(i = 0; i < p; i++)
{
printf("\nFor process %d : ",i + 1);
for(j = 0; j < r; j++)
scanf("%d", &alloc[i][j]);
}
ETU807 OSOS LAB STUDENT ID: 17004027

printf("\n\nEnter the Available Resources : ");


for(i = 0; i < r; i++)
scanf("%d", &avail[i]);

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


for(j = 0; j < r; j++)
need[i][j] = Max[i][j] - alloc[i][j];

do
{
printf("\n Max matrix:\tAllocation matrix:\n");
for(i = 0; i < p; i++)
{
for( j = 0; j < r; j++)
printf("%d ", Max[i][j]);
printf("\t\t");
for( j = 0; j < r; j++)
printf("%d ", alloc[i][j]);
printf("\n");
}

process = -1;

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


{
if(completed[i] == 0)//if not completed
{
process = i ;
for(j = 0; j < r; j++)
{
if(avail[j] < need[i][j])
{
process = -1;
break;
}
}
}
if(process != -1)
break;
}

if(process != -1)
{
printf("\nProcess %d runs to completion!",
process + 1);
safeSequence[count] = process + 1;
count++;
for(j = 0; j < r; j++)
ETU807 OSOS LAB STUDENT ID: 17004027

{
avail[j] += alloc[process][j];
alloc[process][j] = 0;
Max[process][j] = 0;
completed[process] = 1;
}
}
}while(count != p && process != -1);

if(count == p)
{
printf("\nThe system is in a safe state!!\n");
printf("Safe Sequence : < ");
for( i = 0; i < p; i++)
printf("%d ", safeSequence[i]);
printf(">\n");
}
else
printf("\nThe system is in an unsafe state!!");
getch();
}

OUTPUT:
ETU807 OSOS LAB STUDENT ID: 17004027

CONCLUSION: The Bankers Algorithm for Deadlock Avoidance has been simulated
successfully in this program. It is applicable to a system with multiple instances of each
resource type. To decide whether the current request can be satisfied or must be delayed,
the system must consider the resources currently available, the resources currently
allocated to each process, and the future requests and releases of each process.
ETU807 OSOS LAB STUDENT ID: 17004027

EXPERIMENT NO. – 7
AIM: Write a C program to implement the FIFO Page Replacement algorithm.
SOFTWARE USED: Dev C++ (version 5.11)
LANGUAGE USED: C
CODE:
#include<stdio.h>
#include<conio.h>
int main()
{
int i,j,k,f, pf=0, count=0,rs[25],m[10],n;
printf("Name of student = SARITA G. BIJAWE \n Student ID =
17004027 \n\n");

printf("\nEnter the length of reference string ");


scanf("%d",&n);
printf("\nEnter the reference string");
for(i=0;i<n;i++)
scanf("%d",&rs[i]);
printf("\nEnter no. of frames ");
scanf("%d",&f);
for(i=0;i<f;i++)
m[i]=-1;
printf("\nThe Page Replacement Process :\n");
for(i=0;i<n;i++)
{
for(k=0;k<f;k++)
{
}
if(k==f)
{
}
if(m[k]==rs[i])
break;
m[count++]=rs[i];
pf++;
for(j=0;j<f;j++)
printf("\t%d",m[j]);
if(k==f)
printf("\tPF No. %d",pf);
printf("\n");
if(count==f)
count=0;
}
printf("\nThe number of Page Faultd using FIFO are %d",pf);
getch();
ETU807 OSOS LAB STUDENT ID: 17004027

}
OUTPUT:

CONCLUSION: The FIFO Page replacement algorithm has been successfully simulated in
this program. It is the simplest page replacement method in which the operating system
maintains all the pages in a queue, oldest pages are kept in the front and newest are kept at
the end. On a page fault, these pages from the front are removed first, and the pages in
demand are added.
ETU807 OSOS LAB STUDENT ID: 17004027

EXPERIMENT NO. – 8
AIM: Write a C program to implement the Least Recently Used(LRU) Page Replacement
algorithm.

SOFTWARE USED: Dev C++ (version 5.11)


LANGUAGE USED: C
CODE:
#include<stdio.h>

int main()
{
int i,j,k,min,rs[25],m[10],count[10],flag[25],n,f,pf=0,next=1;
printf("Name of student = SARITA G. BIJAWE \n Student ID =
17004027 \n\n");

printf("Enter the length of reference string -- ");


scanf("%d",&n);
printf("Enter the reference string ");
for(i=0;i<n;i++)
{
scanf("%d",&rs[i]); flag[i]=0;
}
printf("Enter the number of frames ");
scanf("%d",&f);
for(i=0;i<f;i++)
{
count[i]=0; m[i]=-1;
}
printf("\nThe PageReplacement processis \n");
for(i=0;i<n;i++)
{
for(j=0;j<f;j++)
{
if(m[j]==rs[i])
{
flag[i]=1;
count[j]=next;
next++;
}
}
if(flag[i]==0)
{
if(i<f)
{
m[i]=rs[i];
ETU807 OSOS LAB STUDENT ID: 17004027

count[i]=next;
next++;
}
else
{
min=0;
for(j=1;j<f;j++)
if(count[min]>count[j])min=j;
m[min]=rs[i];
count[min]=next;
next++;
}
pf++;
}
for(j=0;j<f;j++)
printf("%d\t", m[j]);
if(flag[i]==0)
printf("PF No. %d" , pf);
printf("\n");
}
printf("\nThe number of page faults using LRU are %d",pf);
}

OUTPUT:
ETU807 OSOS LAB STUDENT ID: 17004027

CONCLUSION: The Page replacement using LRU algorithm is successfully implemented in


this program. When a page must be replaced, LRU chooses the page that has not been used
for the longest period of time.

You might also like