OneCompiler

osslip6

97

/*Write the simulation program for demand paging and show the page
scheduling and total number of page faults according the MRU page
replacement algorithm. Assume the memory of n frames.
Reference String : 8, 5, 7, 8, 5, 7, 2, 3, 7, 3, 5, 9, 4, 6, 2
*/

//MFU
#include <stdio.h>
#include <stdlib.h>

int find(int no_of_frames, int *frames, int tofound)
{
int index = -1;
for (int i = 0; i < no_of_frames; i++)
{
if (frames[i] == tofound)
{
index = i;
}
}
return index;
}

int traverse(int no_of_pages, int *pageString, int i, int tofound)
{
int index = -1;
for (int j = i - 1; j >= 0; j--)
{
if (pageString[j] == tofound)
{
return j;
}
}
return index;
}

int find_MRU(int *pageString, int i, int no_of_pages, int no_of_frames, int *frames)
{
int *flag;
flag = (int *)calloc(no_of_frames, sizeof(int));
for (int j = 0; j < no_of_frames; j++)
{
flag[j] = 0;
}
int index = -1;
int idx = -1;
int max = -1; //just some high value later to be replaced
for (int j = 0; j < no_of_frames; j++)
{
idx = traverse(no_of_pages, pageString, i, frames[j]);
if (idx != -1)
{
if (idx > max)
{
max = idx;
index = j;
}
flag[j] = 1;
}
}
free(flag);
return index;
}

int main()
{
int no_of_frames, no_of_pages;
printf("Enter the no of frames:\n");
scanf("%d", &no_of_frames);
printf("Enter the no of pages:\n");
scanf("%d", &no_of_pages);
printf("Enter the pageString\n");
int *pageString;
pageString = (int *)calloc(no_of_pages, sizeof(int));
for (int i = 0; i < no_of_pages; i++)
{
scanf("%d", &pageString[i]);
}
int *frames;
frames = (int *)calloc(no_of_frames, sizeof(int));
for (int i = 0; i < no_of_frames; i++)
{
frames[i] = -1;
}
int index = 0;
int no_of_page_faults = 0;
int no_of_page_hits = 0;
int idx;
int count = 0;
for (int i = 0; i < no_of_pages; i++)
{
if (count < no_of_frames)
{
idx = find(no_of_frames, frames, pageString[i]);
if (idx != -1)
{
no_of_page_hits++;
printf("Page Hit : Succesfully found Page %d at %d Frame\n", pageString[i], idx + 1);
}
else
{
frames[count] = pageString[i];
printf("Page Miss : Storing %d Page no in %d Frame:\n", pageString[i], count + 1);
count++;
no_of_page_faults++;
}
}
else
{
idx = find(no_of_frames, frames, pageString[i]);
if (idx != -1)
{
no_of_page_hits++;
printf("Page Hit : Succesfully found Page %d at %d Frame\n", pageString[i], idx + 1);
}
else
{
index = find_MRU(pageString, i, no_of_pages, no_of_frames, frames);
printf("Page Miss : Replacing %d Frame Page with %d Page no:\n", index + 1, pageString[i]);
no_of_page_faults++;
frames[index] = pageString[i];
}
}
}
printf("The total number of page faults are : %d\n", no_of_page_faults);
printf("The total number of page hits are : %d\n", no_of_page_hits);
return 0;
}

/*Write a programto implement the shell. It should display the command prompt
“myshell.Tokenizethecommandlineandexecutethegivencommandbycreatingthechildprocess.Additionallyitshouldinterpretthefollowingcommands.myshell”. Tokenize the command line and execute the given command by creating the child process. Additionally it should interpret the following commands. 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;

            }
    }

}