Question
Write a program in c What is a shell? Command line interpreter You type ls /etc The shell invokes the first parameter as a command,
Write a program in c
What is a shell?
Command line interpreter
You type ls /etc
The shell invokes the first parameter as a command, with the remainder as the parameters
eg: exec(ls,/et c)
Built - in commands
Most commands are separate executable programs
ls, rm, mv, make, gcc
Some commands are interpreted by the shell
cd, exit , pid, ppid .
Interactive vs Batch
Interactive
User types commands in, hits return to invoke them
Batch
shell reads from an input file
What is the difference?
where the commands come from
You need to implement the Interactive shell model.
Input/Output
C has 3 standard files prepared for you
stdin = input
stdout = output
stderr = error output
printf(foo) == fprint f (stdout,foo)
scanf(%s,str) == fscanf(stdin,%s, str)
fprintf(stderr,Panic!) prints an error message separately
Process Control
Your shell should execute the next command line after the previous one terminates
you must wait for any programs that you launch to finish
You dont have to provide the functionality of launch ing multiple simultaneous commands with ; separating them
Hints
A shell is a loop
read input
execute program
wait program
repeat
Useful routines
fgets() for string input
strtok() for parsing
exit () for exiting the shell
getpid () for find ing the current process ID
getppid () for find ing the parent process ID
getcwd() for getting the current working directory
getenv( )/setenv()
chdir() for changing directories
Executing commands
fork() creates a new process
execvp() runs a new program and does path processing
wait(), waitpid() waits for a child process to terminate
Requirements:
- p should allow the user to select an user - defined prompt. Otherwise, the default should be
my 257 sh> .
Shell functions to be implemented separately : exit, pid, ppid, cd.
For implementing exit from the shell, use the raise() signal handler.
cd prints the cu rrent working directory; whereas cd will change the current working directory.
All other shell commands will need a child process using fork() and then call ing execvp().
No input will be greater than 50 characters.
Only the interactive system need s to be implemented (batch system is not needed)
Background process execution (using &) is NOT required .
Each time a child process is created, e valuate its exit status and print it out.
^C should not take us out of the shell; use a signal handler .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started