Answer to Question 1 (11 points, 20 minutes)

Like your jsh, the Bourne shell starts with standard output and standard error going to the screen. For each command, it calls fork(), then makes redirections (using open()/dup2() for file redirection and dup2() for the x>&y redirections), and then it calls execve().

So:


  • a: prog

    Standard output and standard error go to the screen. The program will open file descriptor 3 which goes to the f1 file. Thus:


  • b: prog > output 2>&1

    Standard output and standard error start by going to the screen. Next, standard output is redirected to go to output, and then dup2(1,2) is called, which redirects standard error to output. Thus:


  • c: prog 2>&1 > output

    Standard output and standard error start by going to the screen. Next, dup2(1,2) is called, which redirects standard error to the screen (this really makes no change), and then standard output is redirected to go to output. Thus:


  • d: prog > output 1>&2

    Standard output and standard error start by going to the screen. Next, standard output is redirected to go to output, and then dup2(2,1) is called, which makes standard output go to the screen again. Thus:


  • e: prog 3> output 1>&3

    Standard output and standard error start by going to the screen. Next, file descriptor 3 is opened and its output is set to go to the file output. Next dup2(3,1) is called, which redirects standard output to go to the file output. Note that now when the program calls open(), file descriptor three is already opened, and thus file descriptor four is returned. Thus:


  • f: prog 3>&1 > output 2>&1

    Standard output and standard error start by going to the screen. Next, dup2(1,3) is called which opens file descriptor three and sets it up to go to the screen. Next, standard output is set to go to the file output, and then dup2(1,2) is called, which means that standard error is also going to output. As above, the open call in prog will return 4, because file descriptor 3 is opened and going to the screen.


    Grading

    Part a was worth one point. Each other part was worth two. On parts b through d getting the "A"'s in the right place was worth 0.5. If you got anything else in its right place, you got an extra 0.5. In parts e and f, you got 0.5 if you wrote a 4, and 0.5 if you got anything in its right place. In all parts, if you wrote things that didn't make sense (like multiple "CS360" lines, or "%d"), points were deducted.