Introduction
In this project you will gain some practice using the C++ fork
,
wait
and execv
system calls for creating new
processes and running programs. In addition, you will extend the functionality
of a C++ program that implements a command line shell.
Resources
Sample Programs:
string
functions in C++.
string
into a C-style string.
string
into its tokens.
fork
and wait
system calls in C++.
fork
,
execv
and wait
system calls in C++.
Links:
Compiling and Running C++ Programs
To compile a C++ program use the command line:
g++ <source file> -o <executable file>
For example, to compile the ForkDemo.cpp
program the command line would be:
g++ ForkDemo.cpp -o ForkDemo.exe
Note that unix does not require the .exe
extension on an executable file.
I've used it here just to clearly distinguish between the source and executable versions
of the programs.
To run a compiled C++ program, be sure you are in the directory containing the file and then use the command line:
./<executable file>
For example, to run the ForkDemo.exe
program that was compiled above,
you would use the command line:
./ForkDemo.exe
Part 1 - fork
, wait
and execv
In this part of the project you will write several programs that make use of the
fork
, wait
and execv
system calls in C++.
Every process should wait for all of its children to complete.
Note process D doesn't fork any other processes. It should just announce that it
is running, sleep for a little while and then announce that it is done.
If you have D sleep for long enough you will be able to use the ps
command
to verify that you have created the correct process tree.
The output from running program A should be similar to the following:
ps
command. You will need to use calls to
the sleep method in your processes so that you have time to observe the results
using ps
. You will also need to use some of the options to the
ps
command in order to see PPID
information as well
as all system processes (you will be particularly interested in the
init
process.) Do not turn in programs for this part.
In your answers just explain how you modified prgrams C and D, what
you did in order to answer the question and what you found.
Part 2 - Exending the SimpleShell
The program contaned in the file
SimpleShell.cpp contains the beginnings
of a shell program. As suggested by its name, the functionality of the SimpleShell
is very limited. It will read a single
line of input from the user and attempt to execute that line as a program using
fork
and execv
. So for example, if the user were
to enter the command:
/bin/ls -l
In response to this command, the SimpleShell will execute the
ls
program contained in the /bin
directory and pass it
the command line argument -l
. After executing the command, the SimpleShell
simply exits. You should play with the SimpleShell program for a little while until
you get a feel for what it can and cannot do. Some things you should be sure to try are:
entering a bogus command, changing directories, running a program (like ls
)
without specifying its full path, and running a program in the current directory.
Your job for this part of the project is to extend the functionality of the SimpleShell in the following ways:
exit
command.
&
functionality. That is, if a command that is entered
ends with an &
then the command should be run concurrently with
the shell. Otherwise, the shell should wait for the entered command to complete.
cd
command so that the user can change the
current working directory. Using cd
without any argument should
cause the current working directory to be set to the user's home directory as
indicated by the HOME
variable. To change the current working
directory you will need to make use of the chdir
system call. The
code below shows several examples of how to use this system call:
PATH
functionality. When the user enters a command with no
path specified (e.g. ls -l
) the shell should attempt to run a program with
the specified name in each of the directories listed in the PATH
. The
directories in the PATH
are to be tried in the order that they appear.
If the program is not found in any of the directories contained in the PATH
,
then an appropriate error message should be displayed. NOTE: If the user enters a
command containing either an absolute or a relative path, then the PATH
should not be seached.
To submit part 2 of this project, send all of the source code files for your shell (with comments please) to me as an e-mail attachment.
Bonus Features
Each of the following features can be implemented for bonus points:
PATH
and HOME
environment variables are read from a file when the shell is started.
Acknowledgment: this content is essentially identical to that developed by Professor Grant Braught for the Spring 2006 Operating Systems course, and I'm grateful for his permission to use it.