Starting from:

$30

CS361-Homework 3 Solved

One of the fundamental functionalities of Linux (and Linux-like OSs) is the ability to pipe commands. When the | character is used in a shell between two commands, the shell takes the output from the first command and “pipes” it to the second command as the input. You can try it yourself! If you run the command ls -la / | grep tmp on systems1, you should be able to see only the tmp directory output from ls -la. This is because you gave grep, a common search tool, the output of ls -la directly. There are many more applications of piping but that is for you to discover as necessary in your adventures/projects/jobs/etc etc etc.

The programming part
For this assignment you are to write a simple program that will act as a shell. The program should:

  Display a command prompt and read in a command line from the user (the prompt must be CS361 , otherwise it cannot be detected by the test script)   Your shell must support basic piping like the unix shell.

1.  $ command Run command, with stdin and stdout connected to their usual files. When command finishes, the parent should output pid:%d status:%d\n (with the proper relevant values inserted into the format string), and then print the prompt again and wait for more input.

2.  $ command1 | command2 Run command1 as in #1, but redirects the output of

CS 361 Summer 202command1 as input to comman0   d2. Note that therHome e should be Scheduletwo instances ofHomeworks pid:%d status:%d\n output.

3.  $ command1 ; command2 Run command1 as in #1, wait for it to finish, and then run command 2 as in #1.

Also…

 Your shell should handle the following signals:

SIGINT - Generated by Ctrl-C. This signal allows a user to terminate a running program. Your shell should not exit when user presses Ctrl-C or the process receives SIGINT but simply report that the SIGINT signal has been received by the shell. If the process receives SIGINT, you must print the string “caught sigint” on a line by itself, and then show the prompt again.

  SIGTSTP - Generated by Ctrl-Z. This signal allows a user to terminate a running program. Your shell should not exit when user presses Ctrl-Z or the process receives SIGTSTP but simply report that the SIGTSTP signal has been received by the shell. If your shell receives SIGTSTP, you must print the string “caught sigtstp” on a line by itself, and then show the prompt again.

  The shell does NOT need to support background processes, running more than one child at a time, or handling multiple chained pipes. Essentially, if anything not matching the use cases/requirements above is in question, you probably do not have to do it.

Due to the breadth of this assignment, it is difficult to clarify every case in writing. Please ask in office hours or on Piazza if you have questions or need clarification.

Tips for primary components
  Parse the command line into arguments, creating an array of character pointers, where array[0] points to the actual command and rest of the array elements point to the arguments to the command (Similar to main()’s argv[])

  Fork off a child and have the child load the requested program by passing the argument vector created in step 2 to exec() family of system calls. The parent should report the PID of the child before proceeding to the next step. (report means you are required to return strings contain “pid” and “status”, e.g. pid:11111 status:0)

  Wait for the child to complete executing and report why it ended (exited or uncaught signal) and its exit value if available.

  Repeat the first step forever until the user enters the command exit

Your personal repository

                  p                    p            y
 Note that there isCS 361 Summer 202 no skeleton code f0 or the homework, yHome ou can find Schedulethe repository foHomeworks r hw3 here (this is an empty repository, start by creating a file named “hw3.c” and add it to your github repository). Going to lab sections is highly advisable, as the TAs have been and will be explaining basics of getting started with a shell. Both the lab section code, as well as code found in the book or book slides, are “fair game” from which to begin your coding. Previous solutions, or other students’ work, are off limits and any cheating will be prosecuted to the fullest extent of university rules. There are solutions on the Internet. If we find you using them, we will give you an F in the class. Don’t look at them, don’t use them. It is not worth it.

More products