Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It accepts the command, tokenize the command line and execute it by creating the child process. Also implement the additional command ‘count’ as myshell$ count c filename: It will display the number of characters in given file myshell$ count w filename: It will display the number of words in given file myshell$ count l filename: It will display the number of lines in given file.


 Operations on Processes

What is Shell?

Shell is an interface between user and operating system. It is the command interpreter, which
accept the command name (program name) from user and executes that command/program.
Shell mostly accepts the commands given by user from keyboard. Shell gets started
automatically when Operating system is successfully started.
When shell is started successfully it generally display some prompt (such as #,$ etc) to indicate
the user that it is ready to accept and execute the command.
Shell executes the commands either synchronously or asynchronously.
When shell accepts the command then it locates the program file for that command, start its
execution, wait for the program associated with the command to complete its execution and then
display the prompt again to accept further command. This is called as Synchronous execution of
shell.
In asynchronous execution shell accept the command from user, start the execution of program
associated to the given command but does not wait for that program to finish its execution,
display prompt to accept next command.

How Shell Execute the command?

1. Accept the command from user.
2. Tokenize the different parts of command.
3. First part given on the given command line is always a command name.
4. Creates (Forks) a child process for executing the program associated with given command.
5. Once child process is created successfully then it loads (exec) the binary (executable) image
of given program in child process area.
6. Once the child process is loaded with given program it will start its execution while shell is
waiting (wait) for it (child) to complete the execution. Shell will wait until child finish its
execution.
7. Once child finish the execution then Shell wakeup, display the command prompt again and
accept the command and continue.


Set A

Q.1) Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It accepts the command, tokenize the command line and execute it by creating the child process. Also implement the additional command ‘count’ as
myshell$ count c filename: It will display the number of characters in given file
myshell$ count w filename: It will display the number of words in given file
myshell$ count l filename: It will display the number of lines in given file.





#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void make_toks(char *s, char *tok[])
{
int i=0;
char *p;
p = strtok(s," ");
while(p!=NULL)
{
 tok[i++]=p;
 p=strtok(NULL," ");
}
tok[i]=NULL;
}
void count(char *fn, char op)
{
int fh,cc=0,wc=0,lc=0;
char c;
fh = open(fn,O_RDONLY);
if(fh==-1)
{
 printf("File %s not found.\n",fn);
return;
}
while(read(fh,&c,1)>0)
{
if(c==' ') wc++;
else if(c=='\n')
 {
 wc++;
 lc++;
 }
 cc++;
}
close(fh);
switch(op)
{
case 'c':
 printf("No.of characters:%d\n",cc-1);
break;
case 'w':
 printf("No.of words:%d\n",wc);
break;
case 'l':
 printf("No.of lines:%d\n",lc+1);
break;
}
}
int main()
{
char buff[80],*args[10];
int pid;
while(1)
{
 printf("myshell$ ");
 fflush(stdin);
 fgets(buff,80,stdin);
 buff[strlen(buff)-1]='\0';
 make_toks(buff,args);
if(strcmp(args[0],"count")==0)
 count(args[2],args[1][0]);
else
 {
 pid = fork();
 if(pid>0)
 wait();
 else
 {
 if(execvp(args[0],args)==-1)
 printf("Bad command.\n");
 }
 }
}
return 0;
}


Output :


myshell$ count c info.txt  
No.of characters:45
myshell$ count w info.txt
No.of words:3
myshell$ count l info.txt
No.of lines:3
myshell$ 

<---info.txt--->

Hello world
Ramayan-Valmiki
Bhagwatgeeta-Vyasa

Related Question :

Write a C program that behaves like a shell which displays the command prompt ‘myshell$’. It
accepts the command, tokenize the command line and execute it by creating the child process.
Also implement the additional command ‘list’ as
myshell$ list f dirname: It will display filenames in a given directory.
myshell$ list n dirname: It will count the number of entries in a given directory.
myshell$ list i dirname: It will display filenames and their inode number for the files in a given
directory.


Post a Comment

2 Comments

  1. I have encountered the implicit declaration error for following functions :-
    read()
    close()
    fork()
    wait()
    execvp()

    can someone tell me how to resolve this error???

    ReplyDelete

Thanks,To visit this blog.