首页 > > 详细

CSE 2431 Lab 1: UNIX Shell (Part I)

 CSE 2431 Lab 1: UNIX Shell (Part I)
Instructor: Adam C. Champion, Ph.D.
Due: Friday, September 6, 2019, 11:59 p.m. (40 points)
Note: To ensure the submission process is working, please check as soon as possible whether
you have any problems with submitting a .zip or .tar.gz fifile via Carmen for Lab 0. Log in to
stdlinux.cse.ohio-state.edu and run the following commands for any fifile :
– For .zip fifile: zip
– For .tar.gz fifile:
(1) tar xcf
(2) gzip .tar
Group Size: 1, which means you are required to fifinish this lab assignment by yourself.
Goal: This lab helps you understand the concept of processes, how to create, and how to terminate
a process. In addition, you are expected to become familiar with system calls related to processes
and fifile operations.
Introduction: As the fifirst step of the project in Chapter 3, this lab assignment ask you to build a
simple shell interface that accepts user commands, creates a child process, and executes the user
commands in the child process. The shell interface provides users a prompt after which the next
command is entered. The example below illustrates the prompt sh> and the user’s next command:
cat prog.c. This command displays the fifile prog.c content on the terminal using the UNIX cat
command.
sh> cat prog.c
pid = fork()
exec() exit()
wait() parent (pid > 0)
child (pid = 0)
parent
resumes
parent
Figure 1: Process creation and termination. (Figure drawn from Figure 3.10 in Silberschatz et al.,
Operating Systems Concepts, 9th ed., Wiley, 2013).
One technique for implementing a shell interface is to have the parent process fifirst read what
the user enters on the command line (i.e., cat prog.c), and then creates a separate child process
that performs the command. Unless otherwise specifified, the parent process waits for the child to
exit before continuing. This is similar in functionality to what is illustrated in Figure 1. However,
UNIX shells typically also allow the child process to run in the background—or concurrently—
as well by specifying the ampersand (&) at the end of the command. By rewriting the above
command as
Page 1 of 3sh> cat prog.c &
the parent and child processes now run concurrently.
The separate child process is created using the fork() system call and the user’s command is
executed by using one of the system calls in the exec() family (for more details about the system
call, you can use the man command).
A Simple Shell: A C program that provides the basic operations of a command line shell is supplied
in the fifile shell.c, which you can download from the course webpage. This program comprises
two functions: main() and setup(). The setup() function reads in the user’s next command
(which can be up to 80 characters), and then parses it into separate tokens that are used to fifill the
argument vector for the command to be executed. (If the command is to be run in the background,
it will end with &, and setup() will update the parameter background so the main() function can
act accordingly. This program is terminated when the user enters and setup()
then invokes exit().)
The main() function presents the prompt COMMAND-> and then invokes setup(), which waits
for the user to enter a command. The contents of the command entered by the user are loaded
into the args array. For example, if the user enters ls -l at the COMMAND-> prompt, args[0]
will be set to the string ls and args[1] will be set to the string -l. (By “string,” we mean a
null-terminated, C-style string variable.)
This lab assignment asks you to create a child process and execute the command entered by a
user. To do this, you need to modify the main() function in shell.c so that upon returning from
setup(), a child process is forked. Then the child process executes the user-specifified command.
As noted above, the setup() function loads the contents of the args array with the command
specifified by the user. This args array will be passed to the execvp() function, which has the
following interface:
execvp(char *command, char *params[]);
Here, command represents the command to be executed and params stores the parameters to this
command. You can fifind more information on execvp() by issuing the command man execvp at
the command line.
Note: You should check the value of background to determine whether the parent process should
wait for the child to exit.
Test: We provide several test cases and a README in the fifile test-lab1.txt on the course website.
You need to follow the instructions in the fifile and compare your results with the expected output
in the fifile.
Submission: Put all your modififications in the fifile shell.c. Submit that fifile in a .tar.gz or .zip
fifile using the commands described on the fifirst page of this lab assignment.
Page 2 of 3Listing 1: Outline of shell.c
1 #include
2 #include
3
4 #define MAX_LINE 80
5
6 /* setup() reads in the next command line string stored in inputBuffer,
7 separating it into distinct tokens using whitespace as delimiters.
8 setup() modifies the args parameter so that it holds pointers to the
9 null-terminated strings that are the tokens in the most recent user
10 command line as well as a NULL pointer, indicating the end of the
11 argument list, which comes after the string pointers that have been
12 assigned to args. */
13
14 void setup(char inputBuffer[], char *args[],int *background)
15 {
16 /** full code available in the file shell.c */
17 }
18
19 int main(void)
20 {
21 char inputBuffer[MAX_LINE]; /* buffer to hold the command entered */
22 int background; /* equals 1 if a command is followed by '&' */
23 char *args[MAX_LINE+1]; /* command line arguments */
24
25 while (1) {
26 background = 0;
27 printf(" COMMAND->\n");
28 setup(inputBuffer,args,&background); /* get next command */
29
30 /* The steps are:
31 (1) fork a child process using fork()
32 (2) the child process will invoke execvp()
33 (3) if background == 0, the parent will wait,
34 otherwise returns to the setup() function. */
35 }
36 }
 
联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!