OneCompiler

osslip15

140

/*Write a C 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.
*/

#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;

            }
    }

}
/* OUTPUT

  • [shalmali@localhost SHELL]$ ./a.out
  •  [shalmali@localhost SHELL]$ ./a.out
    
  •  MYSHELL $]list I .
    
  •  File name =seta2.c	Inode=73504
    
  •  File name =seta3.c	Inode=73495
    
  •  File name =.~lock.setaALL.odt#	Inode=73490
    
  •  File name =setc1.c	Inode=73502
    
  •  File name =setaALL.pdf	Inode=73500
    
  •  File name =setb2.c	Inode=73499
    
  •  File name =setaALL.odt	Inode=73494
    
  •  File name =a.txt	Inode=73501
    
  •  File name =setb3.c	Inode=73493
    
  •  File name =setb1.c	Inode=73496
    
  •  File name =seta1.c	Inode=73498
    
  •  File name =a.out	Inode=73489
    
  •  MYSHELL $]list F .
    
  •  seta2.c
    
  •  seta3.c
    
  •  .~lock.setaALL.odt#
    
  •  setc1.c
    
  •  setaALL.pdf
    
  •  setb2.c
    
  •  setaALL.odt
    
  •  a.txt
    
  •  setb3.c
    
  •  setb1.c
    
  •  seta1.c
    
  •  a.out
    
  •  MYSHELL $]list N .
    
  •  Total no. of entries in directory '.' = 14 
    
  •  MYSHELL $]^C
    
  • [shalmali@localhost SHELL]$
  • */

/*Write the program to simulate preemptive Shortest Job First (SJF) –
scheduling. The arrival time and first CPU-burst of different jobs should be
input to the system. Accept no. of Processes, arrival time and burst time. The
output should give Gantt chart, turnaround time and waiting time for each
process. Also find the average waiting time and turnaround time
*/

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 10

typedef struct PROCESS
{
char name[80] ;
int at,bt,ct,wt,tat,tbt;
}PROCESS ;

PROCESS p[MAX] ;
int time,processCount;
float totaltat,totalwt,avgtat,avgwt ;

void sort()
{
PROCESS p1 ;
int i,j;
for(i=0;i<processCount;i++)
{
for(j=i+1;j<processCount;j++)
{
if(p[j].at < p[i].at)
{
p1 = p[i] ;
p[i] = p[j] ;
p[j] = p1 ;
}
}
}
}

void readProcess()
{
int i ;
printf("\nEnter the number of processes: ") ;
scanf("%d",&processCount) ;
for(i=0;i<processCount;i++)
{
printf("\nEnter the process name: ") ;
scanf("%s",p[i].name) ;
printf("Enter the CPU Burst time: ") ;
scanf("%d",&p[i].bt) ;
printf("Enter the Arrival time: ") ;
scanf("%d",&p[i].at) ;
p[i].tbt = p[i].bt ;
}
sort() ;
}

int getProcess()
{
int i , min = 999 , p1 ;
for(i=0;i<processCount;i++)
{
if(p[i].at <= time && p[i].tbt!=0)
{
if(p[i].tbt < min)
{
min = p[i].tbt ;
p1 = i ;
}
}
}
return p1 ;
}

void scheduleProcess()
{
int i,count=0 ;
char currentprocess[10] , prevprocess[10] = "NULL" ;
printf("\n\n GanttChart:\n") ;
printf("\n") ;
while(1)
{
i = getProcess() ;
p[i].tbt-- ;
strcpy(currentprocess,p[i].name) ;
if(strcmp(currentprocess,prevprocess) !=0 )
{
printf("%d|%d %s ",time , time , currentprocess) ;
}
time++ ;
if(p[i].tbt==0)
{
p[i].ct = time ;
p[i].tat = time - p[i].at ;//finishtime-arrival time
p[i].wt = p[i].tat-p[i].bt ; //total existence-working time
count++ ;
totaltat += p[i].tat ;
totalwt+=p[i].wt ;
}
strcpy(prevprocess , currentprocess) ;
if(count==processCount)
break ;
}
printf("%d|",time) ;
printf("\n
\n") ;
avgtat =totaltat/processCount ;
avgwt = totalwt/processCount ;
}

void display()
{
int i;
printf("\n--------------------------------------------------------------------------------\n") ;
printf("Process ArrivalTime BurstTime CPUTime TurnAroundtime WaitTime\n");
printf("--------------------------------------------------------------------------------\n");
for(i=0 ; i<processCount ; i++)
printf("%s\t %d\t\t%d\t %d\t %d\t\t %d\n",p[i].name,p[i].at,p[i].bt,p[i].ct,p[i].tat,p[i].wt) ;
printf("--------------------------------------------------------------------------------") ;
printf("\n\nTotal Turn Around Time: %f",totaltat) ;
printf("\nTotal Wait Time: %f",totalwt) ;
printf("\n\nAverage Turn Around Time: %f",avgtat) ;
printf("\nAverage Wait Time: %f\n",avgwt) ;
}

int main()
{
//clrscr();
readProcess();
scheduleProcess();
display();
//getch();
return 0;
}