osslip9
/*Write the simulation program for demand paging and show the page
scheduling and total number of page faults according the FIFO page
replacement algorithm. Assume the memory of n frames.
Reference String : 2, 4, 5, 6, 9, 4, 7, 3, 4, 5, 6, 7, 2, 4, 7, 1
*/
#include <stdio.h>
int main()
{
int referenceString[10], pageFaults = 0, m, n, s, pages, frames;
printf("\nEnter the number of Pages:\t");
scanf("%d", &pages);
printf("\nEnter reference string values:\n");
for( m = 0; m < pages; m++)
{
printf("Value No. [%d]:\t", m + 1);
scanf("%d", &referenceString[m]);
}
printf("\n What are the total number of frames:\t");
{
scanf("%d", &frames);
}
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(referenceString[m] == temp[n])
{
s++;
pageFaults--;
}
}
pageFaults++;
if((pageFaults <= frames) && (s == 0))
{
temp[m] = referenceString[m];
}
else if(s == 0)
{
temp[(pageFaults - 1) % frames] = referenceString[m];
}
printf("\n");
for(n = 0; n < frames; n++)
{
printf("%d\t", temp[n]);
}
}
printf("\nTotal Page Faults:\t%d\n", pageFaults);
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 a filename pattern :- To search all the 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;
}
}
}