OneCompiler

osslip19

115

/*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 simulation program for Round Robin scheduling for given time
quantum. 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 the Gantt chart, turnaround time and waiting time for each
process. Also display the average turnaround time and average waiting time.
*/

//RR
#include<stdio.h>
#include<string.h>

struct input
{
char jname[10];
int at,bt,tbt,ct;
}tab[5],t;

struct s
{
char jname[10];
int end,start;
}seq[20],seq1[20];

int j,k,n,time=0,finish=0,prev=0;

input()
{
int i;
printf("enter the no of jobs :");
scanf("%d",&n);
printf("Job\tBT\tAT\n");
for(i=0;i<n;i++)
{
scanf("%s%d%d",&tab[i].jname,&tab[i].bt,&tab[i].at);
tab[i].tbt=tab[i].bt;
}
}

sort()
{
int i;
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(tab[i].at>tab[j].at)
{
t=tab[i];
tab[i]=tab[j];
tab[j]=t;
}
}
}
}

print_input()
{
int i;
printf("\nJOB\tBT\tAT\n");
for(i=0;i<n;i++)
printf("%s\t%d\t%d\n",tab[i].jname,tab[i].bt,tab[i].at);
}

int arrived(int i)
{
if(tab[i].tbt!=0 && tab[i].at<=time)
return 1;
else
return 0;
}

process()
{
int i=0;
k=0;
while(finish<n)
{
if(arrived(i))
{
if(tab[i].tbt<=2)
{
finish++;
seq[k].start=prev;
time+=tab[i].tbt;
tab[i].ct=time;
seq[k].end=time;
strcpy(seq[k++].jname,tab[i].jname);
tab[i].tbt=0;
prev=time;
}
else
{
tab[i].tbt-=2;
seq[k].start=prev;
time+=2;
tab[i].ct=time;
seq[k].end=time;
strcpy(seq[k++].jname,tab[i].jname);
prev=time;
}
}
i++;
if(i==n)
{i=0;}
}
}

gant_chart()
{
int i=0;
printf("\nGant chart:\n");
for(i=0;i<k;i++)
printf("| %d_%s_%d ",seq[i].start,seq[i].jname,seq[i].end);
}

print_output()
{
int i,t1,t2;
float tat=0,twt=0;
printf("\nJob\tTAT\tWT");
for(i=0;i<n;i++)
{
t1=tab[i].ct-tab[i].at;
t2=tab[i].ct-tab[i].at-tab[i].bt;
printf("\n%s\t%d\t%d",tab[i].jname,t1,t2);
tat+=t1;twt+=t2;
}
printf("\nAvgTAT =%f\nAvgWT=%f",tat/n,twt/n);
}

main()
{
input();
sort();
print_input();
process();
print_output();
gant_chart