Docsity

Lab Exercise: Creating a Simple Shell Program in C++, Lab Reports of Operating Systems

Toggle sidebar
bg1
CECS 326 – Lab Exercise 9/8/05 – Due 9/13/05 (10 points)
Obviously when the shell starts a child process, it does not want to execute its own process image in the child process, but
rather the program that the user requests in the command. The way this is achieved is by executing an exec system call.
The man page for the exec calls may be obtained by typing “man execl” at your Linux prompt and will read as follows:
NAME
execl, execlp, execle, execv, execvp - execute a file
SYNOPSIS
#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...);
int execv(const char *path, char *const argv[]);
. . . [ see the man page for other forms of exec calls ]
DESCRIPTION
The exec family of functions replaces the current process image with a new process image.
The functions described in this manual page are front-ends for the function execve(2). (See
the manual page for execve for detailed information about the replacement of the current pro-
cess.)
The initial argument for these functions is the pathname of a file which is to be executed.
The const char *arg and subsequent ellipses in the execl, execlp, and execle functions can be
thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers
to null-terminated strings that represent the argument list available to the executed pro-
gram. The first argument, by convention, should point to the file name associated with the
file being executed. The list of arguments must be terminated by a NULL pointer.
. . . [ see the man page for the remainder ]
For this exercise you need to write a C++ program named myshell.cpp that mimics the basic functions of the shell on
Linux. Specifically your program should prompt the user for a command, read in the command the user enters along with
parameters, then execute the program named in the command using the parameters in the command.
1. Create a subdirectory named ex-9-8-05 within your htdocs/cecs326 subdirectory. Set its permission to world readable
and world executable.
2. Copy myecho that you developed in a previous lab as well as the three sample C++ programs exec1.cpp, exec2.cpp,
newpgm.cpp and forkexec1.cpp from ~lam/htdocs/cecs326/samples into this subdirectory. Compile exec1.cpp into
exec1, exec2.cpp into exec2, newpgm.cpp into newpgm, and forkexec1.cpp into forkexec1. Execute exec1, exec2 and
forkexec1 and see what happens. Understand what each program does. Add a prolog comment section in exec1.cpp,
exec2.cpp and forkexec1.cpp to explain what each program does.
3. Write a C++ program that will mimic the shell program as described above. Save the program in your ex-9-8-05
subdirectory as myshell.cpp. Compile your myshell.cpp into an executable named myshell. Make your myshell.cpp
world readable. Executing the program by typing myshell should lead to an interaction session as below (or close to
it. linux> below represents a Linux prompt, and myshell: represents prompt from your myshell program. The
interaction should be terminated by an empty string in the user input.):
linux>myshell
myshell:myecho first second third
First second third
myshell:echo 1 2 3 4
1 2 3 4
myshell:
myshell terminating ...
linux>
4. Hand in a hardcopy of the annotated exec1.cpp, exec2.cpp, forkexec1.cpp and your C++ program myshell.cpp with
sufficient comments, and demo the execution of your myshell program.

Partial preview of the text

Download Lab Exercise: Creating a Simple Shell Program in C++ and more Lab Reports Operating Systems in PDF only on Docsity!

CECS 326 – Lab Exercise 9/8/05 – Due 9/13/05 (10 points)

Obviously when the shell starts a child process, it does not want to execute its own process image in the child process, but rather the program that the user requests in the command. The way this is achieved is by executing an exec system call. The man page for the exec calls may be obtained by typing “man execl” at your Linux prompt and will read as follows: NAME execl, execlp, execle, execv, execvp - execute a file SYNOPSIS #include <unistd.h> extern char **environ; int execl(const char *path, const char *arg, ...); int execv(const char *path, char *const argv[]);

... [ see the man page for other forms of exec calls ] DESCRIPTION The exec family of functions replaces the current process image with a new process image. The functions described in this manual page are front-ends for the function execve(2). (See the manual page for execve for detailed information about the replacement of the current pro- cess.) The initial argument for these functions is the pathname of a file which is to be executed. The const char *arg and subsequent ellipses in the execl, execlp, and execle functions can be thought of as arg0, arg1, ..., argn. Together they describe a list of one or more pointers to null-terminated strings that represent the argument list available to the executed pro- gram. The first argument, by convention, should point to the file name associated with the file being executed. The list of arguments must be terminated by a NULL pointer. ... [ see the man page for the remainder ] For this exercise you need to write a C++ program named myshell.cpp that mimics the basic functions of the shell on Linux. Specifically your program should prompt the user for a command, read in the command the user enters along with parameters, then execute the program named in the command using the parameters in the command.

  1. Create a subdirectory named ex-9-8-05 within your htdocs/cecs326 subdirectory. Set its permission to world readable and world executable.
  2. Copy myecho that you developed in a previous lab as well as the three sample C++ programs exec1.cpp , exec2.cpp , newpgm.cpp and forkexec1.cpp from ~lam/htdocs/cecs326/samples into this subdirectory. Compile exec1.cpp into exec1 , exec2.cpp into exec2 , newpgm.cpp into newpgm , and forkexec1.cpp into forkexec1. Execute exec1 , exec2 and forkexec1 and see what happens. Understand what each program does. Add a prolog comment section in exec1.cpp , exec2.cpp and forkexec1.cpp to explain what each program does.
  3. Write a C++ program that will mimic the shell program as described above. Save the program in your ex-9-8- subdirectory as myshell.cpp. Compile your myshell.cpp into an executable named myshell. Make your myshell.cpp world readable. Executing the program by typing myshell should lead to an interaction session as below (or close to it. linux> below represents a Linux prompt, and myshell: represents prompt from your myshell program. The interaction should be terminated by an empty string in the user input.): linux>myshell myshell:myecho first second third First second third myshell:echo 1 2 3 4 1 2 3 4 myshell: myshell terminating ... linux>
  4. Hand in a hardcopy of the annotated exec1.cpp , exec2.cpp , forkexec1.cpp and your C++ program myshell.cpp with sufficient comments, and demo the execution of your myshell program.