Q1. A program to implement the
FCFS algorithm.
#include<conio.h>
main()
{
int n,i,j,sum=0;
int arrival[10],service[10],start[10];
int finish[10],wait[10],turn[10];
float avgturn=0.0,avgwait=0.0;
start[0]=0;
clrscr();
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the arrival and service time of
%d process:",i+1);
scanf("%d%d",&arrival[i],&service[i]);
}
for(i=0;i<n;i++)
{
sum=0;
for(j=0;j<i;j++)
sum=sum+service[j];
start[i]=sum;
}
for(i=0;i<n;i++)
{
finish[i]=service[i]+start[i];
wait[i]=start[i];
turn[i]=service[i]+wait[i];
}
for(i=0;i<n;i++)
{
avgwait+=wait[i];
avgturn+=turn[i];
}
avgwait/=n;
avgturn/=n;
printf("\nArrival Service Start Finish Wait
Turn\n");
for(i=0;i<n;i++)
printf("%d\t%d\t%d\t%d\t%d\t%d\n",arrival[i],service[i],start[i],
finish[i],wait[i],turn[i]);
printf("\nAverage waiting time=%f",avgwait);
printf("\nAverage turn around time=%f",avgturn);
getch();
}
------------------------------------------------------------------------------------------------------------------------
Q2. Write a program to implement page
replacement using FIFO algorithm.
#include<stdio.h>
int
re[30],p[10],i,j,n,nr,line=6,c=0,a1=0;
main()
{
clrscr();
printf(" enter length of reference string:");
scanf("%d",&nr);
printf(" enter reference string:");
for(i=1;i<=nr;i++)
{
scanf("%d",&re[i]);
}
printf(" \n enter number of frames:");
fflush(stdin);
scanf("%d",&n);
for(i=1;i<=n;i++)
p[i]=-1;
for(i=1,j=1;i<=nr;i++)
{
a1=0;
if(j>n)
j=1;
for(c=1;c<=n;c++)
if(re[i]==p[c])
a1++;
if(a1==0)
{
p[j]=re[i];
j++;
}
display(n,p,i);
}
printf("\n");
getch();
}
display(int no,int p[],int i)
{
int k;
if(i==1)
{
printf("\t\t\t");
for(k=1;k<=no;k++)
printf("_ _");
}
printf("\n%8d",re[i]);
gotoxy(25,line++);
for(k=1;k<=no;k++)
{
printf("|");
printf("_");
if(p[k]!=-1)
printf("%d",p[k]);
else
printf(" ");
printf("_");
}
printf("|");
}
----------------------------------------------------------------------------------------------------------
Q3. Program to implement page
replacement using LFU algorithm.
#include<stdio.h>
int
i,j=1,s,k,l,re[30],p[10],ch,no,nr,c,a1=0,a,line=6,nk;
struct
{
int st,l,ps,pos;
}opr;
main()
{
clrscr();
printf("Enter length of reference string:");
scanf("%d",&nr);
printf(" Enter reference string:");
for(i=1;i<=nr;i++)
scanf("%d",&re[i]);
printf("\n Enter number of frames:");
scanf("%d",&no);
clrscr();
for(i=1;i<=no;i++)
p[i]=-1;
opr.st=100;
for(i=1;i<=nr;i++)
{
a1=0;
opr.st=100;
opr.pos=100;
for(c=1;c<=no;c++)
if(re[i]==p[c])
a1++;
if(a1==0)
{
if(j<=no)
{
p[j]=re[i];
j++;
}
else
{
for(k=1;k<=no;k++)
{
a=0;
for(ch=i-1;ch>0;ch--)
{
if(p[k]==re[ch])
{
a++;
nk=ch;
}
}
if(a>1)
{
if(opr.st>a)
{
opr.st=a;
opr.ps=k;
}
else
if(opr.st==a)
{
if(opr.pos>ch)
opr.ps=k;
}
}
else
if(a==1)
{
if(opr.pos>nk)
{
opr.pos=nk;
opr.ps=k;
opr.st=a;
}
}
}
p[(opr.ps)]=re[i];
}
}
display(no,p,i);
}
printf("\n");
getch();
}
display(int no,int p[],int i)
{
int k;
if(i==1)
{
printf("\t\t\t");
for(k=1;k<=no;k++)
printf("_ _");
}
printf("\n%d",re[i]);
gotoxy(25,line++);
for(k=1;k<=no;k++)
{
printf("|");
printf("_");
if(p[k]!=-1)
printf("%d",p[k]);
else
printf(" ");
printf("_");
}
printf("|");
}
-----------------------------------------------------------------------------------------------------------
Q4. Write a program to implement page
replacement using LRU algorithm.
#include<stdio.h>
int
i,j=1,k,l,re[30],p[10],ch,no,nr,c,al=0,a,line=6;
struct re
{
int st,l,ps;
}opr;
main()
{
clrscr();
printf("enter the length of the reference
string:");
scanf("%d",&nr);
printf("enter the reference string:");
for(i=1;i<=nr;i++)
scanf("%d",&re[i]);
printf("\n enter the number of frames:");
scanf("%d",&no);
clrscr();
for(i=1;i<=no;i++)
p[i]=-1;
opr.st=0;
for(i=1;i<=nr;i++)
{
al=0;
opr.st=100;
for(c=1;c<=no;c++)
if(re[i]==p[c])
al++;
if(al==0)
{
if(j<=no)
{
p[j]=re[i];
j++;
}
else
{
for(k=1;k<=no;k++)
{
for(ch=i-1;ch>=1;ch--)
{
a=0;
if(p[k]==re[ch])
{
a++;
break;
}
}
if(a!=0)
{
if(opr.st>ch)
{
opr.st=ch;
opr.l=re[ch];
opr.ps=k;
}
}
else if(a==0)
{
opr.ps=k;
break;
}
}
p[(opr.ps)]=re[i];
}
}
display(no,p,i);
}
printf("\n");
getch();
}
display(int no,int p[],int i)
{
int k;
if(i==1)
{
printf("\t\t\t");
for(k=1;k<=no;k++)
printf("__");
}
printf("\n%d",re[i]);
gotoxy(25,line++);
for(k=1;k<=no;k++)
{
printf("|");
printf("_");
if(p[k]!=-1)
printf("%d",p[k]);
else
printf(" ");
printf("_");
}
printf("|");
}
----------------------------------------------------------------------------------------------------------
Q5. Write a program to implement Round
Robin algorithm.
#include<stdio.h>
#include<conio.h>
struct process
{
char na[20];
int at,bt,ft,tat,rem;
float ntat;
}Q[5],temp;
int rr[20],q,x,k;
main()
{
int f,r,n,i,j,tt=0,qt,t,flag,wt=0;
float awt=0,antat=0,atat=0;
clrscr();
printf("Enter the no. of jobs:");
scanf("%d",&n);
for(r=0;r<n;r++)
{
printf("Enter process name,arrival time and
burst time:");
scanf("%s%d%d",Q[r].na,&Q[r].at,&Q[r].bt);
}
printf("Enter quantum:");
scanf("%d",&qt);
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(Q[i].at>Q[j].at)
{
temp=Q[i];
Q[i]=Q[j];
Q[j]=temp;
}
}
}
for(i=0;i<n;i++)
{
Q[i].rem=Q[i].bt;
Q[i].ft=0;
}
tt=0;
q=0;
rr[q]=0;
do
{
for(j=0;j<n;j++)
if(tt>=Q[j].at)
{
x=0;
for(k=0;k<=q;k++)
if(rr[k]==j)
x++;
if(x==0)
{
q++;
rr[q]=j;
}
}
if(q==0)
i=0;
if(Q[i].rem==0)
i++;
if(i>q)
i=(i-1)%q;
if(i<=q)
{
if(Q[i].rem>0)
{
if(Q[i].rem<qt)
{
tt+=Q[i].rem;
Q[i].rem=0;
}
else
{
tt+=qt;
Q[i].rem-=qt;
}
Q[i].ft=tt;
}
i++;
}
flag=0;
for(j=0;j<n;j++)
if(Q[j].rem>0)
flag++;
}while(flag!=0);
clrscr();
printf("\n\n\t\tROUND ROBIN ALGORITHM");
printf("\n***************************");
printf("\nprocesses Arrival time burst time finish
time tat wt ntat");
for(f=0;f<n;f++)
{
wt=Q[f].ft-Q[f].bt-Q[f].at;
Q[f].tat=Q[f].ft-Q[f].at;
Q[f].ntat=(float)Q[f].tat/Q[f].bt;
antat+=Q[f].ntat;
atat+=Q[f].tat;
awt+=wt;
printf("\n\t%s\t%d\t\t%d\t%d\t%d\t%d
%f",
Q[f].na,Q[f].at,Q[f].bt,Q[f].ft,Q[f].tat,wt,Q[f].ntat);
}
antat/=n;
atat/=n;
awt/=n;
printf("\nAverage tat is %f",atat);
printf("\nAverage normalised tat is %f",antat);
printf("\n average waiting time is %f",awt);
getch();
}
-----------------------------------------------------------------------------------------------------------
Q6. Write a program for Shortest Job First(SJF) CPU SCHEDULING
ALGORITHM.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,bt[10],st[10],et[10],wt[10],temp,tot;
/* n=number of jobs bt=burst time st=starting time
wt=waiting time*/
/*et=end time*/
float avg;
clrscr();
printf("ENTER THE NO.OF JOBS");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("\n \n ENTER %d PROCESS BURST
TIME",i);
scanf("%d",&bt[i]);
}
/* SWAPPING BURST TIMES*/
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(bt[i]>bt[j])
{
temp=bt[i];
bt[i]=bt[j];
bt[j]=temp;
}
}
/* IN CASE OF FIRST JOB*/
if(i==1)
{
st[1]=0;
et[1]=bt[1];
wt[1]=0;
}
/* FOR REMAINING CASES*/
else
{
st[i]=et[i-1];
et[i]=st[i]+bt[i];
wt[i]=st[i];
}
}
printf("\n\n BURST TIME \t STARTING TIME \t END TIME
\t WAIT TIME\n");
printf("\n
********************************************************\n");
for(i=1;i<=n;i++)
{
printf("\n %5d %15d %15d
%15d",bt[i],st[i],et[i],wt[i]);
}
printf("\n
********************************************************\n");
for(i=1,tot=0;i<=n;i++)
tot+=wt[i];
avg=(float)tot/n;
printf("\n\n\n AVERAGE WAITING TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=et[i];
avg=(float)tot/n;
printf("\n\n AVERAGE TURNAROUND TIME=%f",avg);
for(i=1,tot=0;i<=n;i++)
tot+=st[i];
avg=(float)tot/n;
printf("\n\n AVERAGE RESPONSE TIME=%f",avg);
getch();
}
Hey,
ReplyDeleteThanks for sharing this great blog. It contains a lot of information. It is easy to locate a Automated Software Testing in birmingham. But hard to choose the best business storage unit services like this. All your services look very professional. Keep posting.