SNU-ARC / 2024_spring_sysprog_Lab4

8 stars 0 forks source link

What's New?

Announcement for evaluation

Due to the limits of VM hypervisors(e.g. VirtualBox and UTM) some errors can occasionally be generated while running reference binary. TAs will evaluate your code on stable environment.

Since TAs committed reference output files in reference/, students can compare their output with given reference output statically.

If you'd like to test with reference binary interactively, changing the number of vCPU to 1 reduce the occurrence of error.

Add grading rtest output

Lab 4: Shell Lab

The purpose of this lab is to become more familiar with the concepts of process control, signaling, pipes, and redirection. To achieve that goal, we implement our own shell with simple job control.

You will learn

Important Dates

Date Description
Tuesday, May 14, 18:30 Hand-out
Tuesday, May 21, 18:30 Lab session 1
Tuesday, May 28, 18:30 Lab session 2
Monday, June 03, 23:59 Submission deadline

Logistics

Hand-out

You can clone this repository directly on your VM instance or local computer and get to work. If you want to keep your own repository, you should keep the lab's visibility to private. Otherwise, others would see your work. Read the instructions here carefully. Then clone the lab to your local computer and get to work.

Changing visibility

After cloning the repository, you should change the push remote URL to your own repository.

  1. Create an empty repository that you're going to manage (again, keep it private)
  2. Copy the url of that repository
  3. On your terminal in the cloned directory, type git remote set-url --push origin <repo_url>
  4. Check with git remote -v if the push URL has changed to yours while the fetch URL remains the same (this repo)

Submission

You should upload your archive file(.tar) containing code (csapsh.c) and report (2024-12345.pdf) via eTL. To make an archive file, follow the example below on your own VM.

$ ls
2024-12345.pdf  csapsh  Makefile  obj  README.md  reference  src  tools  traces
$ tar -cvf 2024-12345.tar src/csapsh.c 2024-12345.pdf
src/csapsh.c
2024-12345.pdf
$ file 2024-12345.tar
2024-12345.tar: POSIX tar archive (GNU)
parameter Description
hostname ip address of VM
target_path absolute path of the file you want to copy (in VM)
download_path (relative) path where a file will be downloaded (in PC)

You can get your VM's hostname using hostname -I command, and the absolute path of a file using realpath <filename> command.

Report Guideline

Your report should include following contents.

  1. Description of your implementation
  2. Difficulties and thoughts during the implementation of this lab

The CSAP Shell

Overview

A shell is an interactive command-line interpreter that runs programs on behalf of the user. A shell repeatedly prints a prompt, waits for a command line on stdin, and then carries out some action, as directed by the contents of the command line.

The command line is a sequence of ASCII text words delimited by whitespace. The first word in the command line is either the name of a built-in command or the pathname of an executable file. The remaining words are command-line arguments. If the first word is a built-in command, the shell immediately executes the command in the current process. Otherwise, the word is assumed to be the pathname of an executable program. In this case, the shell forks a child process, then loads and runs the program in the context of the child. The child processes created as a result of interpreting a single command line are known collectively as a job. In general, a job can consist of multiple child processes connected by Unix pipes.

If the command line ends with an ampersand "&", then the job runs in the background, which means that the shell does not wait for the job to terminate before printing the prompt and awaiting the next command line. Otherwise, the job runs in the foreground, which means that the shell waits for the job to terminate before awaiting the next command line. Thus, at any point in time, at most one job can be running in the foreground. However, an arbitrary number of jobs can run in the background.

For example, typing the command line

csapsh> jobs

causes the shell to execute the built-in jobs command. Typing the command line

csapsh> ls -l -d /etc

runs the ls program in the foreground. By convention, the shell ensures that when a program begins executing its main routine

int main(int argc, char *argv[])

the argc and argv arguments have the following values:

For the ls -l -d /etc example above, argc and argv contain the values

If the command line is terminated with an ampersand character (&), the command is run in the background:

csapsh> ls -l -d /etc &

Unix shells support the notion of job control, which allows users to move jobs back and forth between background and foreground, and to change the process state (running, stopped, or terminated) of the processes in a job. Typing ctrl-c causes a SIGINT signal to be delivered to each process in the foreground job. The default action for SIGINT is to terminate the process. Similarly, typing ctrl-z causes a SIGTSTP signal to be delivered to each process in the foreground job. The default action for SIGTSTP is to place a process in the stopped state, where it remains until it is awakened by the receipt of a SIGCONT signal. Unix shells also provide various built-in commands that support job control. For example:

A single command line can be split into multiple jobs. If the command line has multiple commands split with the ampersand "&", the shell will group the commands following the "&" into another job, while running the former job in the background.

csapsh> cp sysprog/sub.zip . & sleep 5 & echo Hello &

This command line will run three background jobs, each executing cp, sleep, and echo commands.

csapsh> sleep 5 & ls -al

This command line will run two jobs, one (with sleep command) in the background, and the other (with ls command) in the foreground.

ps -eo stat,command | grep ./mysplit > file2 & head -n 6 < file > file3 &

Note that these jobs can have multiple pipes and file I/O redirections.

The csapsh Specification

Your csapsh shell should have the following features:

Handout Overview

The handout contains the following files and directories

File/Directory Description
src/csapsh.c Skeleton for csapsh.c. Implement your solution by editing this file.
src/jobcontrol.c/h Implementation of job control APIs (add, delete, list, ...). Do not modify!
src/parser.c/h Implementation of command line parser. Do not modify!
reference/ Reference implementation
tools/ Tools to test your implementation step-by-step
traces/ Traces of command line used to test your implementation
Makefile Makefile driver program
README.md this file

Tools

The tools directory contains example binaries and a test driver file.

File/Directory Description
Makefile Makefile to build example binaries and run tests.
myint.c Example binary that prints counter every second and send SIGINT.
myprod.c Example binary that prints counter every second.
myspin.c Example binary that prints a string every second.
mysplit.c Example binary that forks a child process and prints a string every second.
mystop.c Example binary that prints counter every second and send SIGTSTP.
sdriver.pl Driver script to test csapsh with traces.

Checking Your work

We provide some tools to help you check your work.

csapsh in verbose mode

The csapsh itself has the feature to help your implementation. Running it in verbose mode (with -v option) will print helpful informations including the parsed command line arguments, job & signal infos at each functions and signal handlers. If you want to print more informations on what your shell is doing, you can use the VERBOSE macro as you need. This will be safer than using naive printfs, since mistakenly left printfs will affect your test results!

Reference solution

You can find a reference implementation of csapsh in reference/csapsh. Run this program to resolve any questions you have about how your shell should behave. Your shell should emit output that is identical to the reference solution (except for PIDs, of course, which change from run to run).

Shell driver

The sdriver.pl driver located in the tools/ directory executes a shell as a child process. The driver sends commands and signals to the shell as directed by a trace file, and captures and displays the output from the shell.

Use the -h argument to find out the usage of sdriver.pl:

$ ./sdriver.pl -h
Usage: ./sdriver.pl [-hv] -t <trace> -s <shellprog> -a <args>
Options:
  -h            Print this message
  -v            Be more verbose
  -t <trace>    Trace file
  -s <shell>    Shell program to test
  -a <args>     Shell arguments
  -g            Generate output for autograder

We also provide a number of trace files that you can use in conjunction with the shell driver to test the correctness of your shell. The lower-numbered trace files do very simple tests, and the higher-numbered tests do more complicated tests.

$ ./sdriver.pl -t ../traces/trace01.txt -s ../csapsh -a "-p"

(the -a "-p" argument tells your shell not to emit a prompt), or

$ make test01

Similarly, to compare your result with the reference shell, you can run the trace driver on the reference shell by typing:

$ ./sdriver.pl -t ../traces/trace01.txt -s ../reference/csapsh -a "-p"

or

$ make rtest01

The neat thing about the trace files is that they generate the same output you would have gotten had you run your shell interactively (except for an initial comment that identifies the trace).

Note that the shell does not output process group and process IDs when run with the driver. This is to make it easier for us to check your solution automatically.

Your Task

Your task is to implement csapsh according to the specification. Your code goes into csapsh.c that contains a functional skeleton of a simple Unix shell. To help you get started, we have already implemented the less interesting functions. Your assignment is to complete the remaining empty functions listed below. As a sanity check for you, we've listed the approximate number of lines of code for each of these functions in our reference solution (which includes lots of comments).

Remember that each time you modify your csapsh.c file, you need to type make to recompile it. To run your shell, type csapsh at the command line:

$ ./csapsh
csapsh> [type commands to your shell here]

Hints

**Happy coding!**