osslip12


/*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
*/

//LRU
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>

typedef struct node
{
int pno;
struct node *llink;
struct node *rlink;
}NODE;

int noframes,noref,mat[10][20],frames[10];
NODE *first,*last;
NODE * getnode(int pno)
{
NODE *t;
t=(NODE *)malloc(sizeof(NODE));
t->pno=pno;
t->llink=NULL;
t->rlink=NULL;
return t;
}

void accept()
{
int i,pno;
NODE *p;
printf("Enter No Of Frames: ");
scanf("%d",&noframes);
printf("Enter No Of References: ");
scanf("%d",&noref);
for(i=0;i<noref;i++)
{
printf("[%d]=",i);
scanf("%d",&pno);
p=getnode(pno);
if(first==NULL)
first=p;
else
{
last->rlink=p;
p->llink=last;
}
last=p;
}
}

int search(int pno)
{
int i;
for(i=0;i<noframes;i++)
if(frames[i]==pno)
return i;
return -1;
}

int get_lru(int e,NODE *p)
{
int i,j,pos=999,posi;
NODE *t;
for(i=0;i<noframes;i++)
{
for(j=e-1,t=p->llink;j>=0;j--,t=t->llink)
{
if(frames[i]==t->pno)
{
if(pos>j)
{
pos=j;
posi=i;
}
break;
}
}
}
return posi;
}

void lru()
{
int i,j,sp=0,faults=0;
NODE *p;
for(i=0,p=first;i<noref && sp<noframes;i++,p=p->rlink)
{
if(search(p->pno)==-1)
{
frames[sp]=p->pno;
sp++;
faults++;

		for(j=0;j<noframes;j++)
			mat[j][i]=frames[j];
	}
}
	while(i<noref)
	{
		if(search(p->pno)==-1)
		{
			 sp=get_lru(i,p);
			 frames[sp]=p->pno;
			 faults++;
           			 for(j=0;j<noframes;j++)
    		         	mat[j][i]=frames[j];
           	        }

	i++;
	p=p->rlink;
}


for(p=first;p!=NULL;p=p->rlink)
printf("%3d",p->pno);
printf("\n\n");

for(j=0;j<noframes;j++)
{
	for(i=0;i<noref;i++)
	if (mat[j][i]!=0)
		printf("%3d",mat[j][i]);
	else
		printf("   ");
	printf("\n");
}
printf("Total No Of Page Faults are= %d",faults);

}

main()
{
accept();
lru();
}

/*Write a program to implement the shell. It should display the command
prompt “myshell.Tokenizethecommandlineandexecutethegivencommandbycreatingthechildprocess.Additionallyitshouldinterpretthefollowinglistcommandsasmyshell”. Tokenize the command line and execute the given command by creating the child process. Additionally it should interpret the following ‘list’ commands as myshell list f dirname :- To print names of all the files in current
directory.
myshell$ list n dirname :- To print the number of all entries in the current
directory */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<dirent.h> //opendir(),readdir(),closedir()
#include<sys/stat.h>
#include<string.h>
#include<fcntl.h> // open,read(),close() : file related function
void list(char p1,chardname)
{
DIR *dir;
struct dirent *entry; //used to hold entry of directory i.e file or dir
struct stat info; //hold inforrmation about file i.e inode, regular file
int cnt=0;

dir=opendir(dname);		
if(dir==NULL)
{
	printf("\n Directory %s Not Found....",dname);
}
else
{
	if(strcmp(p1,"F")==0)
	{
		while((entry=readdir(dir))!=NULL)
		{
			stat(entry->d_name,&info);
			if(info.st_mode & S_IFREG)	//s_mode =2 is for regular file regular file
				printf("%s\n",entry->d_name);
		}
	}
	else if(strcmp(p1,"N")==0)
	{
		cnt=0;
		while((entry=readdir(dir))!=NULL)
                    {
			cnt++;
                    }
		printf("\nTotal no. of entries in directory '%s' = %d ",dname,cnt);
	}
	else if(strcmp(p1,"I")==0)
	{
		while((entry=readdir(dir))!=NULL)
                    {
                            stat(entry->d_name,&info);
                            if(info.st_mode & S_IFREG)      //s_mode =2 is for regular file regular file
                            {
			        printf("File name =%s\t",entry->d_name);
				printf("Inode=%d\n",info.st_ino);
			}
                    }

	}
}

}
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(strcmp(tok1,"list")==0)
			{
				list(tok2,tok3);
			}
			else
			{
                            	if(fork()==0)
                            	{
                                    	execlp(tok1,tok1,tok2,tok3,NULL);
                            	}
                            	wait(0);
			}
                            break;
                    case 4 :
                            if(fork()==0)
                            {
                                    execlp(tok1,tok1,tok2,tok3,tok4,NULL);
                            }
                            wait(0);
                            break;

            }
    }

}