osslip16
*Write a programto implement the toy shell. It should display the command
prompt “myshell$”. Tokenize the command line and execute the given
command by creating the child process. Additionally it should interpret the
following commands.
count c filename :- To print number of characters in the file.
count w filename :- To print number of words in the file.
count l filename :- To print number of lines in the file. */
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<fcntl.h> // open,read(),close() : file related function
void count(char p1,charfname)
{
int handle,ccnt=0,wcnt=0,lcnt=0;
char ch;
handle=open(fname,O_RDONLY);
if(handle==-1)
{
printf("\n File %s Not Found....",fname);
}
else
{
while(read(handle,&ch,1)) //on failure read() function returns 0
{
if(ch==' '||ch=='\t')
wcnt++;
else if(ch=='\n')
{
lcnt++;
wcnt++;
}
ccnt++;
}
close(handle);
if(strcmp(p1,"C")==0)
{
printf("\nTotal No. of character in file = %d",ccnt);
}
else if(strcmp(p1,"W")==0)
{
printf("\nTotal No. of words in file = %d",wcnt);
}
else if(strcmp(p1,"L")==0)
{
printf("\nTotal No. of Lines in file = %d",lcnt);
}
else
{
printf("\nInvalid Option......\n");
}
}
}
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,"count")==0)
{
count(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;
}
}
}
/*Write a C program to simulate Non-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<stdlib.h>
#define MAX 10
typedef struct PROCESS
{
char name[20] ;
int at,bt,ct,tat,wt,tbt ;
}PROCESS ;
PROCESS p[MAX] ;
int time,processCount;
float avgtat,avgwt,totaltat,totalwt ;
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 ;//extra line from fcfs
}
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].bt < min)
{
min = p[i].bt ;
p1 = i ;
}
}
}
return p1 ;
}
void scheduleProcess()
{
int i,count = 0 ;
printf("\n\n GanttChart:\n") ;
printf("________________________________________________\n") ;
printf("|%d ",time) ;
while(1)
{
i = getProcess() ;
p[i].wt = time - p[i].at ;//current time-arrival time
time = time + p[i].bt ;
p[i].tbt = 0 ;
p[i].ct = time ;//extra line from fcfs
p[i].tat = p[i].ct - p[i].at ; //finishtime-arrival time
//p[i].tat=p[i].bt+p[i].wt; //working time+waiting time
totaltat+=p[i].tat ;
totalwt+=p[i].wt ;
count++ ;
printf("%s ",p[i].name) ;
printf("%d|%d ",time,time) ;
if(count==processCount)
break ;
}
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();
//getch();
display();
//getch();
return 0;
}