#include <stdio.h>
#include <stdlib.h>
int main( )
{
int refstr[ 50 ] , ch, frame[ 10 ] , distance[ 10 ] , present= 0 , i= 0 , j= 0 , k= 0 , l= 0 , found= 0 , flag= 0 , len= 0 , val, framesize= 0 , toberemoved= 0 , pagefault= 0 ;
printf ( "\n enter the referrence string (-1 to terminate):" ) ; //read the referrence string
while ( flag!= 1 )
{
scanf ( "%d" ,& refstr
[ len
] ) ;
if ( refstr[ len] ==- 1 )
flag= 1 ;
len++;
} //while ends
len--;
printf ( "\n Enter frame size : " ) ;
while ( 1 )
{
printf ( "\n \n \t \t MAIN MENU\n 1.OPTIMAL\n 2.LRU\n 3.FCFS\n 4.Exit\n " ) ; switch ( ch)
{
case 1 :
/*optimal starts*/
pagefault= 0 ;
for ( i= 0 ; i< framesize; i++ )
frame[ i] =- 1 ;
// frame=new int[framesize];
for ( i= 0 ; i< framesize; i++ )
{
frame[ i] = refstr[ i] ;
pagefault++;
}
k= 0 ;
//i now starts with framesize,say the frame size is three then now,
//frame already has the first three elements of the referrence string
while ( i< len)
{
found= 0 ;
for ( j= 0 ; j< framesize; j++ )
{
if ( refstr[ i] == frame[ j] )
{
found= 1 ;
}
}
if ( found!= 1 )
{
for ( j= 0 ; j< framesize; j++ )
distance[ j] = 100 ;
int max= 0 ;
toberemoved= 0 ;
for ( j= 0 ; j< framesize; j++ )
{
for ( k= i+ 1 ; k< len; k++ )
{
if ( frame[ j] == refstr[ k] )
{
distance[ j] = k;
break ;
}
}
}
for ( j= 1 ; j< framesize; j++ )
{
if ( distance[ j] > distance[ max] )
max= j;
}
toberemoved= max;
frame[ toberemoved] = refstr[ i] ;
pagefault++;
// if(k==len)
// k=0;
}
// k++;
for ( j= 0 ; j< framesize; j++ )
i++;
} //while ends
printf ( "\n pagefaults = %d" , pagefault
) ; /*optimal ends*/
/*lru starts*/
break ;
case 2 :
for ( i= 0 ; i< framesize; i++ )
{
frame[ i] = refstr[ i] ;
distance[ i] = i;
pagefault++;
}
k= 0 ;
//i=0;
//i now starts with framesize
while ( i< len)
{
found= 0 ;
for ( j= 0 ; j< framesize; j++ )
{
if ( refstr[ i] == frame[ j] )
{
found= 1 ;
distance[ j] = i; //if the variable is found, then the distance tag should be replaced with the new tag
}
}
if ( found!= 1 )
{
int min= 0 ;
toberemoved= 0 ;
for ( j= 1 ; j< framesize; j++ )
{
if ( distance[ j] < distance[ min] )
min= j;
}
toberemoved= min;
distance[ min] = i;
frame[ toberemoved] = refstr[ i] ;
pagefault++;
// if(k==len)
// k=0;
} //if ends
// k++;
for ( j= 0 ; j< framesize; j++ )
i++;
}
printf ( "\n pagefaults = %d" , pagefault
) ; /*lru ends*/
break ;
case 3 :
/*fcfs starts*/
// fcfs:
pagefault= 0 ;
toberemoved= 0 ;
// distance=new int[framesize];
for ( i= 0 ; i< framesize; i++ )
{
frame[ i] = refstr[ i] ;
pagefault++;
}
// printf("%d %d",i,len);
while ( i< len)
{
// printf("%d",refstr[i++]);
found= 0 ;
for ( j= 0 ; j< framesize; j++ )
{
if ( refstr[ i] == frame[ j] )
found= 1 ;
}
if ( found!= 1 )
{
frame[ toberemoved] = refstr[ i] ;
toberemoved++;
if ( toberemoved== framesize)
{
toberemoved= 0 ;
}
pagefault++;
}
for ( j= 0 ; j< framesize; j++ )
i++;
} //while ends
printf ( "\n pagefaults = %d" , pagefault
) ; break ;
case 4 :
} //switch ends
}
//getch();
return 0 ;
} //main ends
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmludCBtYWluKCkKewogICAgaW50IHJlZnN0cls1MF0sY2gsZnJhbWVbMTBdLGRpc3RhbmNlWzEwXSxwcmVzZW50PTAsaT0wLGo9MCxrPTAsbD0wLGZvdW5kPTAsZmxhZz0wLGxlbj0wLHZhbCxmcmFtZXNpemU9MCx0b2JlcmVtb3ZlZD0wLHBhZ2VmYXVsdD0wOwoKICAgIHByaW50ZigiXG5lbnRlciB0aGUgcmVmZXJyZW5jZSBzdHJpbmcgKC0xIHRvIHRlcm1pbmF0ZSk6Iik7Ci8vcmVhZCB0aGUgcmVmZXJyZW5jZSBzdHJpbmcKICAgIHdoaWxlKGZsYWchPTEpCiAgICB7CiAgICAgICAgc2NhbmYoIiVkIiwmcmVmc3RyW2xlbl0pOwoKICAgICAgICBpZihyZWZzdHJbbGVuXT09LTEpCiAgICAgICAgZmxhZz0xOwogICAgICAgIGxlbisrOwoKICAgIH0vL3doaWxlIGVuZHMKICAgIGxlbi0tOwogICAgcHJpbnRmKCJcbkVudGVyIGZyYW1lIHNpemUgOiAiKTsKICAgIHNjYW5mKCIlZCIsJmZyYW1lc2l6ZSk7CgogICAgd2hpbGUoMSkKICAgIHsKICAgICAgICBwcmludGYoIlxuXG5cdFx0TUFJTiBNRU5VXG4xLk9QVElNQUxcbjIuTFJVXG4zLkZDRlNcbjQuRXhpdFxuICIpOwogICAgICAgIHNjYW5mKCIlZCIsJmNoKTsKICAgICAgICBzd2l0Y2goY2gpCiAgICAgICAgewoKICAgICAgICAgICAgY2FzZSAxOgoKLypvcHRpbWFsIHN0YXJ0cyovCiAgICAgICAgICAgICAgICBwYWdlZmF1bHQ9MDsKICAgICAgICAgICAgICAgIGZvcihpPTA7aTxmcmFtZXNpemU7aSsrKQogICAgICAgICAgICAgICAgZnJhbWVbaV09LTE7Ci8vCWZyYW1lPW5ldyBpbnRbZnJhbWVzaXplXTsKICAgICAgICAgICAgICAgIGZvcihpPTA7aTxmcmFtZXNpemU7aSsrKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGZyYW1lW2ldPXJlZnN0cltpXTsKICAgICAgICAgICAgICAgICAgICBwYWdlZmF1bHQrKzsKICAgICAgICAgICAgICAgICAgICBwcmludGYoIiVkICIsZnJhbWVbaV0pOwogICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgcHJpbnRmKCJcbiIpOwogICAgICAgICAgICAgICAgaz0wOwoKLy9pIG5vdyBzdGFydHMgd2l0aCBmcmFtZXNpemUsc2F5IHRoZSBmcmFtZSBzaXplIGlzIHRocmVlIHRoZW4gbm93LAovL2ZyYW1lIGFscmVhZHkgaGFzIHRoZSBmaXJzdCB0aHJlZSBlbGVtZW50cyBvZiB0aGUgcmVmZXJyZW5jZSBzdHJpbmcKICAgICAgICAgICAgICAgIHdoaWxlKGk8bGVuKQogICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgIGZvdW5kPTA7CiAgICAgICAgICAgICAgICAgICAgZm9yKGo9MDtqPGZyYW1lc2l6ZTtqKyspCiAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICBpZihyZWZzdHJbaV09PWZyYW1lW2pdKQogICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICBmb3VuZD0xOwogICAgICAgICAgICAgICAgICAgICAgICB9CgogICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICBpZihmb3VuZCE9MSkKICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgIGZvcihqPTA7ajxmcmFtZXNpemU7aisrKQogICAgICAgICAgICAgICAgICAgICAgICBkaXN0YW5jZVtqXT0xMDA7CiAgICAgICAgICAgICAgICAgICAgICAgIGludCBtYXg9MDsKICAgICAgICAgICAgICAgICAgICAgICAgdG9iZXJlbW92ZWQ9MDsKICAgICAgICAgICAgICAgICAgICAgICAgZm9yKGo9MDtqPGZyYW1lc2l6ZTtqKyspCiAgICAgICAgICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICAgICAgICAgIGZvcihrPWkrMTtrPGxlbjtrKyspCiAgICAgICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoZnJhbWVbal09PXJlZnN0cltrXSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGRpc3RhbmNlW2pdPWs7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICAgICAgICAgICAgICBmb3Ioaj0xO2o8ZnJhbWVzaXplO2orKykKICAgICAgICAgICAgICAgICAgICAgICAgewogICAgICAgICAgICAgICAgICAgICAgICAgICAgaWYoZGlzdGFuY2Vbal0+ZGlzdGFuY2VbbWF4XSkKICAgICAgICAgICAgICAgICAgICAgICAgICAgIG1heD1qOwogICAgICAgICAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgICAgICAgICAgICAgIHRvYmVyZW1vdmVkPW1heDsKICAgICAgICAgICAgICAgICAgICAgICAgZnJhbWVbdG9iZXJlbW92ZWRdPXJlZnN0cltpXTsKICAgICAgICAgICAgICAgICAgICAgICAgcGFnZWZhdWx0Kys7Ci8vCWlmKGs9PWxlbikKLy8Jaz0wOwoKICAgICAgICAgICAgICAgICAgICB9CgovLyBrKys7CiAgICAgICAgICAgICAgICAgICAgZm9yKGo9MDtqPGZyYW1lc2l6ZTtqKyspCiAgICAgICAgICAgICAgICAgICAgICAgIHByaW50ZigiJWQgIixmcmFtZVtqXSkgOwogICAgICAgICAgICAgICAgICAgIHByaW50ZigiXG4iKTsKCiAgICAgICAgICAgICAgICAgICAgaSsrOwogICAgICAgICAgICAgICAgfS8vd2hpbGUgZW5kcwogICAgICAgICAgICAgICAgcHJpbnRmKCJcbnBhZ2VmYXVsdHMgPSAlZCIscGFnZWZhdWx0KTsKLypvcHRpbWFsIGVuZHMqLwoKCi8qbHJ1IHN0YXJ0cyovCiAgICAgICAgICAgICAgICBicmVhazsKCiAgICBjYXNlIDI6CgogICAgICAgIGZvcihpPTA7aTxmcmFtZXNpemU7aSsrKQogICAgICAgIHsKICAgICAgICAgICAgZnJhbWVbaV09cmVmc3RyW2ldOwogICAgICAgICAgICBkaXN0YW5jZVtpXT1pOwogICAgICAgICAgICBwYWdlZmF1bHQrKzsKICAgICAgICAgICAgcHJpbnRmKCIlZCAiLGZyYW1lW2ldKTsKICAgICAgICB9CiAgICAgICAgaz0wOwogICAgICAgIHByaW50ZigiXG4iKTsKLy9pPTA7Ci8vaSBub3cgc3RhcnRzIHdpdGggZnJhbWVzaXplCiAgICAgICAgd2hpbGUoaTxsZW4pCiAgICAgICAgewogICAgICAgICAgICBmb3VuZD0wOwogICAgICAgICAgICBmb3Ioaj0wO2o8ZnJhbWVzaXplO2orKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYocmVmc3RyW2ldPT1mcmFtZVtqXSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICBmb3VuZD0xOwogICAgICAgICAgICAgICAgICAgIGRpc3RhbmNlW2pdPWk7Ly9pZiB0aGUgdmFyaWFibGUgaXMgZm91bmQsIHRoZW4gdGhlIGRpc3RhbmNlIHRhZyBzaG91bGQgYmUgcmVwbGFjZWQgd2l0aCB0aGUgbmV3IHRhZwoKICAgICAgICAgICAgICAgIH0KCiAgICAgICAgICAgIH0KICAgICAgICAgICAgaWYoZm91bmQhPTEpCiAgICAgICAgICAgIHsKCiAgICAgICAgICAgIGludCBtaW49MDsKICAgICAgICAgICAgdG9iZXJlbW92ZWQ9MDsKICAgICAgICAgICAgZm9yKGo9MTtqPGZyYW1lc2l6ZTtqKyspCiAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgIGlmKGRpc3RhbmNlW2pdPGRpc3RhbmNlW21pbl0pCiAgICAgICAgICAgICAgICBtaW49ajsKICAgICAgICAgICAgfQogICAgICAgICAgICB0b2JlcmVtb3ZlZD1taW47CiAgICAgICAgICAgIGRpc3RhbmNlW21pbl09aTsKICAgICAgICAgICAgZnJhbWVbdG9iZXJlbW92ZWRdPXJlZnN0cltpXTsKICAgICAgICAgICAgcGFnZWZhdWx0Kys7Ci8vCWlmKGs9PWxlbikKLy8Jaz0wOwoKICAgICAgICAgICAgfS8vaWYgZW5kcwoKLy8gaysrOwogICAgICAgICAgICBmb3Ioaj0wO2o8ZnJhbWVzaXplO2orKykKICAgICAgICAgICAgICAgIHByaW50ZigiJWQgIixmcmFtZVtqXSkgOwogICAgICAgICAgICBwcmludGYoIlxuIik7CgogICAgICAgICAgICBpKys7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG5wYWdlZmF1bHRzID0gJWQiLHBhZ2VmYXVsdCk7Ci8qbHJ1IGVuZHMqLwogICAgICAgIGJyZWFrOwogICAgY2FzZSAzOgoKLypmY2ZzIHN0YXJ0cyovCgoKLy8gZmNmczoKCiAgICAgICAgcGFnZWZhdWx0PTA7CiAgICAgICAgdG9iZXJlbW92ZWQ9MDsKLy8JZGlzdGFuY2U9bmV3IGludFtmcmFtZXNpemVdOwogICAgICAgIGZvcihpPTA7aTxmcmFtZXNpemU7aSsrKQogICAgICAgIHsKICAgICAgICAgICAgZnJhbWVbaV09cmVmc3RyW2ldOwogICAgICAgICAgICBwYWdlZmF1bHQrKzsKICAgICAgICAgICAgcHJpbnRmKCIlZCAiLGZyYW1lW2ldKTsKICAgICAgICB9CiAgICAgICAgcHJpbnRmKCJcbiIpOwoKLy8JcHJpbnRmKCIlZCAlZCIsaSxsZW4pOwogICAgICAgIHdoaWxlKGk8bGVuKQogICAgICAgIHsKLy8gcHJpbnRmKCIlZCIscmVmc3RyW2krK10pOwogICAgICAgICAgICBmb3VuZD0wOwogICAgICAgICAgICBmb3Ioaj0wO2o8ZnJhbWVzaXplO2orKykKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgaWYocmVmc3RyW2ldPT1mcmFtZVtqXSkKICAgICAgICAgICAgICAgIGZvdW5kPTE7CgogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmKGZvdW5kIT0xKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBmcmFtZVt0b2JlcmVtb3ZlZF09cmVmc3RyW2ldOwogICAgICAgICAgICAgICAgdG9iZXJlbW92ZWQrKzsKICAgICAgICAgICAgICAgIGlmKHRvYmVyZW1vdmVkPT1mcmFtZXNpemUpCiAgICAgICAgICAgICAgICB7CiAgICAgICAgICAgICAgICAgICAgdG9iZXJlbW92ZWQ9MDsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgICAgIHBhZ2VmYXVsdCsrOwoKICAgICAgICAgICAgfQogICAgICAgICAgICBmb3Ioaj0wO2o8ZnJhbWVzaXplO2orKykKICAgICAgICAgICAgICAgIHByaW50ZigiJWQgIixmcmFtZVtqXSkgOwogICAgICAgICAgICBwcmludGYoIlxuIik7CiAgICAgICAgICAgIGkrKzsKICAgICAgICB9Ly93aGlsZSBlbmRzCiAgICAgICAgcHJpbnRmKCJcbnBhZ2VmYXVsdHMgPSAlZCIscGFnZWZhdWx0KTsKICAgICAgICBicmVhazsKICAgIGNhc2UgNDoKICAgICAgICBleGl0KDApOwogICAgfS8vc3dpdGNoIGVuZHMKfQovL2dldGNoKCk7CnJldHVybiAwOwp9Ly9tYWluIGVuZHMK
compilation info
prog.c: In function ‘main’:
prog.c:6: warning: unused variable ‘val’
prog.c:6: warning: unused variable ‘l’
prog.c:6: warning: unused variable ‘present’
prog.c:12: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:21: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
prog.c:26: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
stdout
enter the referrence string (-1 to terminate):
Enter frame size :
MAIN MENU
1.OPTIMAL
2.LRU
3.FCFS
4.Exit
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
5 2 3 4
pagefaults = 5
MAIN MENU
1.OPTIMAL
2.LRU
3.FCFS
4.Exit
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
5 2 3 4
pagefaults = 10
MAIN MENU
1.OPTIMAL
2.LRU
3.FCFS
4.Exit
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
5 2 3 4
pagefaults = 5
MAIN MENU
1.OPTIMAL
2.LRU
3.FCFS
4.Exit