1. Homepage
  2. Programming
  3. CSCI2250 Operating Systems Lab 2: New Yet Usable SHell

CSCI2250 Operating Systems Lab 2: New Yet Usable SHell

Engage in a Conversation
NYUCSCI2250Operating SystemsCShell

The shell is the main command-line interface between a user and the operating system, and it is an essential part of the daily lives of computer scientists, software engineers, system administrators, and such. It makes heavy use of many OS features. In this lab, you will build a simplied version of the Unix shell called the New Yet Usable SHell, or for short. CourseNana.COM

Please review the rst lecture of MIT’s The Missing Semester of Your CS Education if you are not familiar with the shell. CourseNana.COM

Through this lab, you will: CourseNana.COM

Familiarize yourself with the Linux programming environment and the shell, of course.
Learn how to write an interactive command-line program.
Learn how processes are created, destroyed, and managed.
CourseNana.COM

Learn how to handle signals and I/O redirection.
Get a better understanding of the OS and system calls.
Be a better C programmer and be better prepared for your future technical job interviews. In particular, the string parsing skill that you will practice in this lab is desired in many interview questions.
CourseNana.COM

The shell is essentially a command-line interpreter. It works as follows: CourseNana.COM

1. It prompts you to enter a command.
2. It interprets the command you entered.
CourseNana.COM

If you entered a built-in command (e.g., cd ), then the shell runs that command. CourseNana.COM

If you entered an external program (e.g., ), or multiple programs connected through pipes (e.g., ), then the shell creates child processes, executes these programs, and waits for all these processes to either terminate or be suspended. CourseNana.COM

If you entered something wrong, then the shell prints an error CourseNana.COM

message.
3. Rinse and repeat until you press to close or enter the
CourseNana.COM

built-in command , at which point the shell exits. CourseNana.COM

Your shell should follow these specications carefully. These specications may be slight different from the default Linux shell ( ) for simplicity. CourseNana.COM

The prompt is what the shell prints before waiting for you to enter a command. In this lab, your prompt must have exactly the following format: CourseNana.COM

An opening bracket [ .
The word .
A whitespace.
The
basename of the current working directory. A closing bracket ] . CourseNana.COM

A dollar sign $ . Another whitespace. CourseNana.COM

For example, if you are in , then the prompt should be: CourseNana.COM

If you are in the root directory ( / ), then the prompt should be: CourseNana.COM

/bin/ls CourseNana.COM

ls -l | less CourseNana.COM

[nyush lab2]$ █
/home/abc123/2250/lab2

[nyush /]$ █ CourseNana.COM

Note that the nal character in these examples represents your cursor; you should not print that character in your shell prompt. CourseNana.COM

Keep in mind that is line-buffered by default. Therefore, don’t forget to ush immediately after you print the prompt. Otherwise, your program may not work correctly with the autograder. CourseNana.COM

In each iteration, the user inputs a command terminated by the “enter” key (i.e., newline). A command may contain multiple programs separated by the pipe ( | ) symbol. A valid command must satisfy the following requirements: CourseNana.COM

If there are multiple programs in a command, only the rst program may redirect its input (using < ), and only the last program may redirect its output (using > or >> ). If there is only one program in a command, it may redirect both input and output. CourseNana.COM

In each command, there are no more than one input redirection and one output redirection.
Built-in commands (
e.g., cd ) cannot be I/O redirected or piped. CourseNana.COM

For simplicity, our test cases have the following assumptions: CourseNana.COM

Each command has no more than 1000 characters.
There is always
a single space separating lenames, arguments, and the pipe and redirection symbols ( | , < , > , >> ).
There are no spaces within a lename or an argument.
CourseNana.COM

For your reference, here is the grammar for valid commands (don’t worry if you can’t understand it; just look at the examples below): CourseNana.COM

Here are some examples of valid commands: A blank line. CourseNana.COM

/usr/bin/ls -a -l
cat shell.c | grep main | less
cat < input.txt
cat > output.txt
cat >> output.txt
cat < input.txt > output.txt
cat < input.txt >> output.txt
cat > output.txt < input.txt
cat >> output.txt < input.txt
cat < input.txt | cat > output.txt
cat < input.txt | cat | cat >> output.txt
[command] := ""; or
          := [cd] [arg]; or
          := [exit]; or
          := [fg] [arg]; or
          := [jobs]; or
          := [cmd] '<' [filename] [recursive]; or
          := [cmd] '<' [filename] [terminate]; or
          := [cmd] [recursive]; or
          := [cmd] [terminate] '<' [filename]; or
          := [cmd] [terminate].
[recursive] := '|' [cmd] [recursive]; or
            := '|' [cmd] [terminate].
[terminate] := ""; or
            := '>' [filename]; or
            := '>>' [filename].
[cmd] := [cmdname] [arg]*
[cmdname] := A string without any space, tab, > (ASCII 62), < (ASCII
[arg] := A string without any space, tab, > (ASCII 62), < (ASCII 60)
[filename] := A string without any space, tab, > (ASCII 62), < (ASCI

Here are some examples of invalid commands: CourseNana.COM

cat << file.txt
cat < file.txt < file2.txt
cat < file.txt file2.txt
cat > file.txt > file2.txt
cat > file.txt >> file2.txt
cat > file.txt file2.txt
cat > file.txt | cat
cat | cat < file.txt
cd / > file.txt

If there is any error in parsing the command, then your shell should print the following error message to and prompt for the next command. CourseNana.COM

Note that there should be a newline at the end of the error message. For example: CourseNana.COM

(Again, the nal character represents your cursor.) CourseNana.COM

You can specify a program by either an absolute path, a relative path, or base name only. CourseNana.COM

Error: invalid command
[nyush lab2]$ cat <
Error: invalid command
[nyush lab2]$ █
  1. An absolute path begins with a slash ( / ). If the user species an absolute path, then your shell must run the program at that location. CourseNana.COM

  2. A relative path contains, but not begins with, a slash ( / ). If the user CourseNana.COM

    species a relative path, then your shell should locate the program by following the path from the current working directory. For example, CourseNana.COM

    is equivalent to . CourseNana.COM

  3. Otherwise, if the user species only the base name without any slash CourseNana.COM

    ( / ), then your shell must search for the program under . For example, when the user types ls , then your shell should try CourseNana.COM

    . If that fails, it is an error. In this case, your shell should not search the current working directory. For example, suppose there is a program named in the current working directory. Entering should result in an error, whereas runs the program. CourseNana.COM

In any case, if the program cannot be located, your shell should print the following error message to and prompt for the next command. CourseNana.COM

After creating the processes, your shell must wait until all the processes have stopped running—either terminated or suspended. Then, your shell should prompt the user for the next command. CourseNana.COM

Your shell must not leave any zombies in the system when it is ready to read the next command from the user. CourseNana.COM

If a user presses or , they don’t expect to terminate or suspend the shell. Therefore, your shell should ignore the following signals: , , and . All other signals not listed here should keep the default signal handlers. CourseNana.COM

Note that only the shell itself, not the child processes created by the shell, should ignore these signals. For example, CourseNana.COM

dir1/dir2/program

/usr/bin/ls CourseNana.COM

./dir1/dir2/program

/usr/bin CourseNana.COM

./hello CourseNana.COM

Error: invalid program

SIGQUIT CourseNana.COM

SIGTSTP CourseNana.COM

Here, the signal generated by terminates only the process cat , not the shell itself. CourseNana.COM

As a side note, if your shell ever hangs and you would like to kill the shell, you can still send it the or signal. To do so, you can connect to the running Docker container from another terminal window using: CourseNana.COM

...and then kill your shell using: CourseNana.COM

Sometimes, a user would read the input to a program from a le rather than the keyboard, or send the output of a program to a le rather than the screen. Your shell should be able to redirect the standard input
( ) and the standard output ( ). For simplicity, you are not required to redirect the standard error ( ). CourseNana.COM

Input redirection is achieved by a < symbol followed by a lename. For example: CourseNana.COM

If the le does not exist, your shell should print the following error message to and prompt for the next command. CourseNana.COM

SIGTERM CourseNana.COM

SIGKILL CourseNana.COM

docker exec -it 2250 bash
[root@... 2250]# killall nyush
[nyush lab2]$ cat < input.txt
[nyush lab2]$ cat
^C
[nyush lab2]$ █

Output redirection is achieved by > or >> followed by a lename. For example: CourseNana.COM

If the le does not exist, a new le should be created. If the le already exists, redirecting with > should overwrite the le (after truncating it), whereas redirecting with >> should append to the existing le. CourseNana.COM

A pipe ( | ) connects the standard output of the rst program to the standard input of the second program. For example: CourseNana.COM

The user may invoke n programs chained through (n - 1) pipes. Each pipe connects the output of the program immediately before the pipe to the input of the program immediately after the pipe. For example: CourseNana.COM

Here, the output of is the input of grep main , and the output of is the input of . CourseNana.COM

Every shell has a few built-in commands. When the user issues a command, the shell should rst check if it is a built-in command. If so, it should not be executed like other programs. CourseNana.COM

[nyush lab2]$ ls -l > output.txt
[nyush lab2]$ ls -l >> output.txt
[nyush lab2]$ cat shell.c | wc -l
[nyush lab2]$ cat shell.c | grep main | less

cat shell.c CourseNana.COM

grep main CourseNana.COM

Error: invalid file

In this lab, you will implement four built-in commands: cd , jobs , fg , and exit . CourseNana.COM

This command changes the current working directory of the shell. It takes exactly one argument: the directory, which may be an absolute or relative path. For example: CourseNana.COM

If cd is called with 0 or 2+ arguments, your shell should print the following error message to and prompt for the next command. CourseNana.COM

If the directory does not exist, your shell should print the following error message to and prompt for the next command. CourseNana.COM

This command prints a list of currently suspended jobs to STDOUT , one job per line. Each line has the following format: . For example: CourseNana.COM

cd <dir> CourseNana.COM

[nyush lab2]$ cd /usr/local
[nyush local]$ cd bin
[nyush bin]$ █
Error: invalid command
Error: invalid directory
[index] command
[nyush lab2]$ jobs
[1] ./hello
[2] /usr/bin/top -c
[3] cat > output.txt
[nyush lab2]$ █

(If there are no currently suspended jobs, this command should print nothing.) CourseNana.COM

A job is the whole command, including any arguments and I/O redirections. A job may be suspended by , the signal, or the signal. This list is sorted by the time each job is suspended (oldest rst), and the index starts from 1. CourseNana.COM

Note for Windows users CourseNana.COM

If you’re using recent versions of Windows, you might need to enable Legacy Console mode in order for to be properly handled by PowerShell. CourseNana.COM

For simplicity, we have the following assumptions: CourseNana.COM

There are no more than 100 suspended jobs at one time.
There are no pipes in any suspended jobs.
The only way to resume a suspended job is by using the
fg command (see below). We will not try to resume or terminate a suspended job by other means. We will not try to press or CourseNana.COM

while there are suspended jobs.
You don’t need to worry about “process groups.” (If you don’t know what process groups are, don’t worry.)
CourseNana.COM

The jobs command takes no arguments. If it is called with any arguments, your shell should print the following error message to CourseNana.COM

and prompt for the next command. CourseNana.COM

This command resumes a job in the foreground. It takes exactly one argument: the job index, which is the number inside the bracket printed by the command. For example: CourseNana.COM

SIGSTOP CourseNana.COM

SIGTSTP CourseNana.COM

Error: invalid command

fg <index> CourseNana.COM

The last command would resume in the foreground. Note that the job index of would become 2 as a result. Should the job be suspended again, it would be inserted to the end of the job list: CourseNana.COM

/usr/bin/top -c
cat > output.txt
/usr/bin/top -c
[nyush lab2]$ jobs
[1] ./hello
[2] cat > output.txt
[3] /usr/bin/top -c
[nyush lab2]$ █

If fg is called with 0 or 2+ arguments, your shell should print the following error message to and prompt for the next command. CourseNana.COM

If the job does not exist in the list of currently suspended jobs, your shell should print the following error message to and prompt for the next command. CourseNana.COM

This command terminates your shell. However, if there are currently suspended jobs, your shell should not terminate. Instead, it should print the following error message to and prompt for the next command. CourseNana.COM

Error: invalid command
Error: invalid job
Error: there are suspended jobs
[nyush lab2]$ jobs
[1] ./hello
[2] /usr/bin/top -c
[3] cat > output.txt
[nyush lab2]$ fg 2

The exit command takes no arguments. If it is called with any arguments, your shell should print the following error message to CourseNana.COM

and prompt for the next command. CourseNana.COM

Note that if the of your shell is closed (e.g., by pressing at the prompt), your shell should terminate regardless of whether there are suspended jobs. CourseNana.COM

We will grade your submission in an x86_64 Rocky Linux 8 container on Gradescope. We will compile your program using gcc 12.1.1 with the C17 standard and GNU extensions. CourseNana.COM

Error: invalid command

You must provide a
an executable le named
to Lab 1 for an example of the CourseNana.COM

, and by running , it should generate in the current working directory. (Refer CourseNana.COM

.) CourseNana.COM

Your program must not call the CourseNana.COM

function or execute . CourseNana.COM

Makefile CourseNana.COM

Makefile CourseNana.COM

system() CourseNana.COM

Otherwise, what is the whole point of this lab? CourseNana.COM

Beat up your own code extensively. Better yet, eat your own dog food. I would happily use as my main shell (at least for the duration of this lab), so why wouldn’t you? CourseNana.COM

We are providing a sample autograder with a few test cases. Please extract them inside the Docker container and follow the instructions in the le. (Refer to Lab 1 for how to extract a le.) CourseNana.COM

Note that these test cases are not exhaustive. The test cases on Gradescope are different from the ones provided and will not be disclosed. Do not try to hack or exploit the autograder. CourseNana.COM

/bin/sh CourseNana.COM

.tar.xz CourseNana.COM

You must submit a archive containing all les needed to compile in the root of the archive. You can create the archive le with the CourseNana.COM

following command in the Docker container: CourseNana.COM

(Omit *.h if you don’t have header les.) CourseNana.COM

Note that other le formats (e.g., rar ) will not be accepted. CourseNana.COM

You need to upload the archive to Gradescope. If you need to acknowledge any inuences per our academic integrity policy, write them as comments in your source code. CourseNana.COM

The total of this lab is 100 points, mapped to 15% of your nal grade of this course. CourseNana.COM

Compile successfully and can print the correct prompt. (40 points) Process creation and termination. (20 points) Simplebuilt-incommands(cd and )anderrorhandling.(10 points) CourseNana.COM

$ zip nyush.zip Makefile *.h *.c

Get in Touch with Our Experts

WeChat WeChat
Whatsapp WhatsApp
NYU代写,CSCI2250代写,Operating Systems代写,C代写,Shell代写,NYU代编,CSCI2250代编,Operating Systems代编,C代编,Shell代编,NYU代考,CSCI2250代考,Operating Systems代考,C代考,Shell代考,NYUhelp,CSCI2250help,Operating Systemshelp,Chelp,Shellhelp,NYU作业代写,CSCI2250作业代写,Operating Systems作业代写,C作业代写,Shell作业代写,NYU编程代写,CSCI2250编程代写,Operating Systems编程代写,C编程代写,Shell编程代写,NYUprogramming help,CSCI2250programming help,Operating Systemsprogramming help,Cprogramming help,Shellprogramming help,NYUassignment help,CSCI2250assignment help,Operating Systemsassignment help,Cassignment help,Shellassignment help,NYUsolution,CSCI2250solution,Operating Systemssolution,Csolution,Shellsolution,