OS Record Pages 2
OS Record Pages 2
LIST OF PROGRAMS
I) FIFO
14 II) LRU-Least recently used
15 III) Optimal
#include<stdio.h>
int main()
{
int p[10],at[10],bt[10],ct[10],tat[10],wt[10],i,j,temp=0,n;
oat awt=0,atat=0;
prin ("enter no of proccess you want:");
scanf("%d",&n);
prin ("enter %d process:",n);
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
prin ("enter %d arrival me:",n);
for(i=0;i<n;i++)
{
scanf("%d",&at[i]);
}
prin ("enter %d burst me:",n);
for(i=0;i<n;i++)
{
scanf("%d",&bt[i]);
}
// sor ng at,bt, and process according to at
for(i=0;i<n;i++)
{
for(j=0;j<(n-i);j++)
{
if(at[j]>at[j+1])
{
temp=p[j+1];
p[j+1]=p[j];
p[j]=temp;
temp=at[j+1];
at[j+1]=at[j];
at[j]=temp;
temp=bt[j+1];
bt[j+1]=bt[j];
bt[j]=temp;
}
}
}
/* calcula ng 1st ct */
ct[0]=at[0]+bt[0];
/* calcula ng 2 to n ct */
for(i=1;i<n;i++)
{
//when proess is ideal in between i and i+1
temp=0;
if(ct[i-1]<at[i])
{
temp=at[i]-ct[i-1];
}
ct[i]=ct[i-1]+bt[i]+temp;
}
/* calcula ng tat and wt */
prin ("\np\t A.T\t B.T\t C.T\t TAT\t WT");
for(i=0;i<n;i++)
{
tat[i]=ct[i]-at[i];
wt[i]=tat[i]-bt[i];
atat+=tat[i];
awt+=wt[i];
}
atat=atat/n;
awt=awt/n;
for(i=0;i<n;i++)
{
prin ("\nP%d\t %d\t %d\t %d \t %d \t %d",p[i],at[i],bt[i],ct[i],tat[i],wt[i]);
}
prin ("\naverage turnaround me is %f",atat);
Output:
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
oat avg_wt,avg_tat;
prin (“Implementa on of SJF:\n”);
prin ("Enter number of process:");
scanf("%d",&n);
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
//calculate wai ng me
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
Output:
#include <stdio.h>
struct prg {
int ariv;
int prgno;
int bt;
int bt1;
int wt;
int tt;
int check;
};
float average(struct prg a[], int n) {
int i;
float sum = 0;
for (i = 0; i < n; i++) {
sum = sum +(float) a[i].wt;
}
return sum / n;
}
void main() {
struct prg p[10], temp;
int n,shot, count = 0, i = 0, j, flg = 0, sum = 0, time = 0, fg = 0, current
= 0;
printf("enter no of processes:");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter process number:");
scanf("%d", &p[i].prgno);
printf("arrivel:");
scanf("%d", &p[i].ariv);
printf("burst time:");
scanf("%d", &p[i].bt);
p[i].bt1 = p[i].bt;
p[i].check = 0;
}
if(p[shot].ariv>time){
shot++;
}else{
p[shot].bt=p[shot].bt-1;
if(p[shot].bt==0){
p[shot].tt=time-p[shot].ariv;
p[shot].wt=p[shot].tt-p[shot].bt1;
count++;
}
for(i=0;i<n;i++){
flg=0;
for(j = 0; j < n - i - 1; j++) {
if (p[j].bt > p[j + 1].bt) {
temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
flg = 1;
}
}
if (flg == 0)
break;
}shot=0;
while(p[shot].bt==0){
shot++;
}
}time++;
}
printf("process:\ttotal\twait\n");
for (i = 0; i < n; i++) {
printf("process %d:\t\t%d\t%d\n", p[i].prgno, p[i].tt, p[i].wt);
}
printf("the average waiting time is : %f", average(p, n));
}
OUTPUT:
#include<stdio.h>
main()
{
int st[10],bt[10],wt[10],tat[10],n,tq;
int i,count=0,swt=0,stat=0,temp,sq=0;
oat awt,atat;
prin ("Implementa on of Round Robin algorithm\n");
prin ("enter the number of processes:");
scanf("%d",&n);
prin ("enter the burst me of each process \n");
for(i=0;i<n;i++)
{
prin ("p %d :",i+1);
scanf("%d",&bt[i]);
st[i]=bt[i];
}
prin ("enter the me quantum");
scanf("%d",&tq);
while(1)
{
for(i=0,count=0;i<n;i++)
{
temp=tq;
if(st[i]==0)
{
count++;
con nue;
}
if(st[i]>tq)
st[i]=st[i]-tq;
else
if(st[i]>=0)
{
temp=st[i];
st[i]=0;
}
sq=sq+temp;
tat[i]=sq;
}
if(n==count)
break;
}
for(i=0;i<n;i++)
{
wt[i]=tat[i]-bt[i];
swt=swt+wt[i];
stat=stat+tat[i];
}
awt=( oat)swt/n;
atat=( oat)stat/n;
prin ("process no\t burst me\t wai ng me\t turnaround me\n");
for(i=0;i<n;i++)
prin ("%d\t\t %d\t\t %d\t\t %d\n",i+1,bt[i],wt[i],tat[i]);
prin ("avg wt me=%f,avg turn around me=%f",awt,atat);
}
Output:
#include<stdio.h>
int main()
{
int bt[20],p[20],wt[20],tat[20],pri[20],i,j,k,n,total=0,pos,temp;
oat avg_wt,avg_tat;
prin ("Implementa on of Priority algorithm\n");
prin ("Enter number of process:");
scanf("%d",&n);
prin ("\nEnter Burst Time:\n");
for(i=0;i<n;i++)
{
prin ("p%d:",i+1);
scanf("%d",&bt[i]);
p[i]=i+1; //contains process number
}
prin (" enter priority of the process ");
for(i=0;i<n;i++)
{
p[i] = i;
//prin ("Priority of Process");
prin ("p%d ",i+1);
scanf("%d",&pri[i]);
}
for(i=0;i<n;i++)
for(k=i+1;k<n;k++)
if(pri[i] > pri[k])
{
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bt[i];
bt[i]=bt[k];
bt[k]=temp;
temp=pri[i];
pri[i]=pri[k];
pri[k]=temp;
}
wt[0]=0; //wai ng me for rst process will be zero
//calculate wai ng me
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=( oat)total/n; //average wai ng me
total=0;
prin ("\nProcess\t Burst Time \tPriority \tWai ng Time\tTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround me
total+=tat[i];
prin ("\np%d\t\t %d\t\t %d\t\t %d\t\t\t%d",p[i],bt[i],pri[i],wt[i],tat[i]);
}
avg_tat=( oat)total/n; //average turnaround me
prin ("\n\nAverage Wai ng Time=%f",avg_wt);
prin ("\nAverage Turnaround Time=%f\n",avg_tat);
}
Output:
#include<stdio.h>
#include<sys/types.h>
#include<sys/fcntl.h>
#include<sys/stat.h>
#include<sys/unistd.h>
#include<string.h>
int main(){
int sz;
char C[100];
int fd=open("foo1.txt",O_RDONLY);
if(fd>0){
sz=read(fd,C,10);
C[sz]='\0';
prin ("called read(%d,C,10) it reads 10 bytes into bu er \"%s\"",fd,C);
close(fd);
}
return 0;
}
Output:
7.Write the c program using the I/O system calls of UNIX/LINUX opera ng
system
#include<stdio.h>
#include<sys/types.h>
#include<sys/fcntl.h>
#include<sys/stat.h>
#include<sys/unistd.h>
#include<string.h>
int main(){
int sz;
char bu [100];
int fd=open("foo1.txt",O_RDWR);
if(fd>0){
int size=strlen("new lines");
sz=write(fd, "new lines", strlen("new lines"));
prin ("called write(%d, %s, %d)\n", fd, "new lines", size);
sz=read(fd,bu ,10);
bu [sz]=0;
prin ("called read(%d,bu ,10) it reads 10 bytes into bu er=\"%s\"",fd,bu );
close(fd);
}
return 0;
}
Output:
File before execu on:
#include<stdio.h>
#include<string.h>
#de ne max 100
int main(){
int alloc[max][max],maxi[max][max],need[max][max],avail[max],ans[max];
int p,r,index=0,i,j,k;
if ( ag==0)
{
prin ("The Safe Sequence Is :");
for ( i = 0; i < p-1; i++)
{
prin ("P%d->",ans[i]);
}
prin ("P%d",ans[p-1]);
}
return 0;
}
Output:
#include<stdio.h>
#include<stdlib.h>
int mutex = 1, full = 0, empty = 3, x = 0;
int main () {
int n;
void producer ();
void consumer ();
int wait (int);
int signal (int);
prin ("\n1.Producer\n2.Consumer\n3.Exit");
while (1)
{
prin ("\nEnter your choice:");
scanf ("%d", &n);
switch (n) {
case 1:
if ((mutex == 1) && (empty != 0))
producer ();
else
prin ("Bu er is full!!");
break;
case 2:
if ((mutex == 1) && (full != 0))
consumer ();
else
prin ("Bu er is empty!!");
break;
case 3:
exit (0);
break; }
}
return 0;
}
int wait (int s)
{
return (--s);
}
int signal (int s)
{
return (++s);
}
void producer ()
{
mutex = wait (mutex);
full = signal (full);
empty = wait (empty);
x++;
prin ("\nProducer produces the item %d", x);
mutex = signal (mutex);
}
void consumer ()
{
mutex = wait (mutex);
full = wait (full);
empty = signal (empty);
prin ("\nConsumer consumes item %d", x);
x--;
mutex = signal (mutex);
}
Output:
#include<stdio.h>
int main()
{
int ms, ps, nop, np, rempages, i, j, x, y, pa, o set;
int s[10], fno[10][20];
prin ("\nEnter the memory size -- ");
scanf("%d",&ms);
prin ("\nEnter the page size -- ");
scanf("%d",&ps);
nop = ms/ps;
prin ("\nThe no. of pages available in memory are -- %d ",nop);
prin ("\nEnter number of processes -- ");
scanf("%d",&np);
rempages = nop;
for(i=1;i<=np;i++)
{
prin ("\nEnter no. of pages required for p[%d]-- ",i);
scanf("%d",&s[i]);
if(s[i] >rempages)
{
prin ("\nMemory is Full");
break;
}
rempages = rempages - s[i];
else
{ pa=fno[x][y]*ps+o set;
prin ("\nThe Physical Address is -- %d",pa);
}
return 0;
//getch();
}
Output:
OUTPUT:
#include <stdio.h>
int main()
{
int incomingStream[] = {4 , 1 , 2 , 4 , 5};
int pageFaults = 0;
int frames = 3;
int m, n, s, pages;
pages = sizeof(incomingStream)/sizeof(incomingStream[0]);
prin (" Incoming \t\t Frame 1 \t\t Frame 2 \t\t Frame 3 ");
int temp[ frames ];
for(m = 0; m < frames; m++)
{
temp[m] = -1;
}
for(m = 0; m < pages; m++)
{
s = 0;
for(n = 0; n < frames; n++)
{
if(incomingStream[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = incomingStream[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = incomingStream[m];
}
prin ("\n");
prin ("%d\t\t\t",incomingStream[m]);
for(n = 0; n < frames; n++)
{
if(temp[n] != -1)
prin (" %d\t\t\t", temp[n]);
else
prin (" - \t\t\t");
}
}
prin ("\nTotal Page Faults:\t%d\n", pageFaults);
return 0;
}
Output:
#include <stdio.h>
int main() {
scanf("%d", &n);
int p[n];
scanf("%d",&c);
while (c--) {
ag = 0;
scanf("%d", &a);
if (p[i] == a)
ag = 1;
if ( ag == 0) {
if (count < n)
count++;
top = (top + 1) % n;
p[top] = a;
fault++;
} else
OUTPUT:
II)LRU
#include <stdio.h>
int number;
int me;
} det;
det prog;
} pros;
int main() {
scanf("%d", &n);
pros p[n];
scanf("%d",&c);
while (c --) {
ag = 0;
scanf("%d", &a);
if (p[i].prog.number == a) {
p[i].prog. me++;
ag = 1;
if ( ag == 0) {
if (count < n) {
top = top + 1;
p[top].prog.number = a;
p[top].prog. me = 1;
count++;
} else {
min = 0;
min = i;
p[min].prog.number = a;
p[min].prog. me = 1;
fault++;
} else
OUTPUT:
III)OPTIMAL
#include <stdio.h>
int i;
if (a[i] == key)
return 1;
return 0;
int i = 0, m = 0;
if (ar[i] == key) {
return i;
return 0;
int main() {
int a[100];
scanf("%d", &nf);
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
int frame[nf];
hit++;
} else {
fault++;
frame[count] = a[i];
count++;
} else {
hit++;
} else {
fault++;
ag = 0;
max = 0;
if (largest(a, n, i, frame[j]) == 0) {
frame[j] = a[i];
ag = 1;
break;
maxidx = j;
if (! ag) {
frame[maxidx] = a[i];
prin ("\n");
OUTPUT: