osslip8
*Write the simulation program for demand paging and show the page
scheduling and total number of page faults according the LRU page
replacement algorithm. Assume the memory of n frames.
Reference String : 3, 4, 5, 6, 3, 4, 7, 3, 4, 5, 6, 7, 2, 4, 6
*/
#include <stdio.h>
//user-defined function
int findLRU(int time[], int n)
{
int i, minimum = time[0], pos = 0;
for (i = 1; i < n; ++i)
{
if (time[i] < minimum)
{
minimum = time[i];
pos = i;
}
}
return pos;
}
//main function
int main()
{
int no_of_frames, no_of_pages, frames[10], pages[30], counter = 0, time[10], flag1, flag2, i, j, pos, faults = 0;
printf("Enter number of frames: ");
scanf("%d", &no_of_frames);
printf("Enter number of pages: ");
scanf("%d", &no_of_pages);
printf("Enter reference string: ");
for (i = 0; i < no_of_pages; ++i)
{
scanf("%d", &pages[i]);
}
for (i = 0; i < no_of_frames; ++i)
{
frames[i] = -1;
}
for (i = 0; i < no_of_pages; ++i)
{
flag1 = flag2 = 0;
for (j = 0; j < no_of_frames; ++j)
{
if (frames[j] == pages[i])
{
counter++;
time[j] = counter;
flag1 = flag2 = 1;
break;
}
}
if (flag1 == 0)
{
for (j = 0; j < no_of_frames; ++j)
{
if (frames[j] == -1)
{
counter++;
faults++;
frames[j] = pages[i];
time[j] = counter;
flag2 = 1;
break;
}
}
}
if (flag2 == 0)
{
pos = findLRU(time, no_of_frames);
counter++;
faults++;
frames[pos] = pages[i];
time[pos] = counter;
}
printf("\n");
for (j = 0; j < no_of_frames; ++j)
{
printf("%d\t", frames[j]);
}
}
printf("\nTotal Page Faults = %d", faults);
return 0;
}
/*
Write a programto implement the shell. It should display the command prompt
“myshell search f filename pattern :- To display first occurrence of
pattern in the file.
myshell$ search c filename pattern :- To count the number of occurrence
of pattern in the file.
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h> // open,read(),close() : file related function
void search(char *p1,char pat,charfname)
{
int handle,i=0,cnt=0;
char ch,data[100],*ptr;
handle=open(fname,O_RDONLY);
if(handle==-1)
{
printf("\n File %s Not Found....",fname);
}
else
{
if(strcmp(p1,"F")==0)
{
i=0;
while(read(handle,&ch,1)) //on failure read() function returns 0
{
data[i]=ch;
i++;
if(ch=='\n')
{
data[i]='\0';
if(strstr(data,pat)!=NULL)
{
printf("\nFIRST OCCURANCE OF PATTERN IN LINE IS GIVEN BELOW\n");
puts(data);
break;
}
i=0;
}
}
close(handle);
}
else if(strcmp(p1,"C")==0)
{
cnt=0;
i=0;
while(read(handle,&ch,1)) //on failure read() function returns 0
{
data[i]=ch;
i++;
if(ch=='\n')
{
data[i]='\0';
ptr=data;
while((ptr=strstr(ptr,pat))!=NULL)
{
cnt++;
ptr++;
}
i=0;
}
}
printf("\nNo of occurances of '%s' = %d",pat,cnt);
close(handle);
}
else if(strcmp(p1,"A")==0)
{
printf("\nDispaying All Occurances of '%s'\n",pat);
i=0;
while(read(handle,&ch,1)) //on failure read() function returns 0
{
data[i]=ch;
i++;
if(ch=='\n')
{
data[i]='\0';
if((ptr=strstr(data,pat))!=NULL)
{
puts(data);
}
i=0;
}
}
}
}//else
}
int main()
{
char cmd[80],tok1[10],tok2[10],tok3[10],tok4[10];
int n;
while(1)
{
printf("\nMYSHELL $]");
fgets(cmd,80,stdin);
n=sscanf(cmd,"%s%s%s%s",tok1,tok2,tok3,tok4); //n=no of tokens are formed from given command
switch(n)
{
case 1:
if(fork()==0)
{
execlp(tok1,tok1,NULL); //paramenters- nameOfProcess,parametersOfProcess
}
wait(0);
break;
case 2 :
if(fork()==0)
{
execlp(tok1,tok1,tok2,NULL);
}
wait(0);
break;
case 3:
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,NULL);
}
wait(0);
break;
case 4 :
if(strcmp(tok1,"search")==0)
{
search(tok2,tok3,tok4);
}
else
{
if(fork()==0)
{
execlp(tok1,tok1,tok2,tok3,tok4,NULL);
}
wait(0);
}
break;
}
}
}