共计 6050 个字符,预计需要花费 16 分钟才能阅读完成。
CS3214 Fall 2023 Exercise 1
Due: See website for due date.What to submit: A tar file that should contain the file answers.txt, which must be anUTF-8 encoded text file with answers for questions 1 and 2. For question 3, which asksfor code, include a file named dpipe.c in your tar file.Use the submit.py script in ~cs3214/bin/submit.py to submit your tar file from the
command line, or use the submit website. Use the target identifier‘ex1.’The assignment will be graded on the rlogin cluster, so your answers must match thisenvironment. In addition, every student will use a variant of dpipe that is unique to them;please visit cs3214/fall2023/exercises/exercise1 to find the number of your variant.
Understanding Processes and Pipes
In this exercise we consider a program‘dpipe,’which, in connection with a‘netcat’utilityprogram, can be used to turn any ordinary program into a network server. A‘netcat’utility program is a program that extends the standard Unix‘cat‘utility over a network.The learning goals of this exercise are:
• Understand how to write C code that makes system calls.
• Understand how to start and wait for processes in Unix with the fork() and waitpid()system calls.
• Understand how to create pipes as an example of an IPC(interprocess communication) facility.
• Understand how to manipulate a process’s standard input and standard output filestreams using standard Unix system call facilities (e.g. dup2()) that affect low-levelfile descriptors. A very similar file descriptor manipulation is performed by a shellwhen it sets up I/O redirection or pipes.
• Understand how to use the strace facility to trace a process’s system calls.
• Understand how to use the /proc file system to obtain information about processesin a system.
• Gain a deeper understanding of process states using a practical example applicationthat combines process management, IPC, and network I/O.In this exercise, you are asked to observe, reverse-engineer, and then reimplement thedpipe program. The dpipe binary is provided in ~cs3214/bin/dpipe.variants/
dpipe-ABC on our machines, where ABC is the unique id created for you. For netcat, usethe ~cs3214/bin/gnetcat6 binary.To allow you to invoke those commands directly, make sure that ~cs3214/bin and(optionally) ~cs3214/bin/dpipe.variants are included in your PATH environmentCS3214 Fall 2023 Exercise 1variable.1 You should already have your PATH set up from exercise 0 to include the firstdirectory. If you haven’t, do this now.Here’s an example session of how to use it. Say you’re logged in to the machine‘locust’and run the command dpipe-ABC wc gnetcat6 -l 159992. This will produce output as follows:
[cs3214@locust sys1]$ ~cs3214/bin/dpipe.variants/dpipe-ABC wc gnetcat6 -l 15999Starting: wcStarting: gnetcat6
Running in server mode
Note that the shell is still waiting for dpipe to complete at this point. The questions listedin part 1 must be answered at this point, after you have started dpipe, but before the nextstep. You may wish to continue reading and then return to this point, after openingasecond terminal on the same machine.3The next command can be run on any rlogin machine:gnetcat6locustrlogin.cs.vt.edu 15999 /etc/passwdRunning in client modeThe gnetcat6 instance started here connected to the gnetcat6 instance running on locust4and sent its input there (in this case, the content of file /etc/passwd). Gnetcat (runningon locust) outputs this data to its standard output stream, which is connected via a pipeto the standard input stream of the‘wc’program. The standard output stream of‘wc’is connected to thestandard input stream of locust’s gnetcat6 instance. Anything output by‘wc’will then be forwarded tochinkapin’s gnetcat6 instance, which outputs it toits standardoutput, thus appearing on the user’s terminal on chinkapin. You havejustturned the ordinary‘wc’program into a network service! The entire scenario is shown inFigure 1.Finally, dpipe exits when both of the processes it started haveterminated:
- Using /proc The /proc file system, originally introduced as part of the Solaris OS,is a mechanism by which the Linux kernel provides information about processesto users. This information is organized in directories and files that can be accessed1The latter is, however, not necessary – you can also replace dpipe-ABC with its full path~cs3214/bin/dpipe.variants/dpipe-ABC whenever you invoke it.
- When you try this out, please replace 15999 with a number that with high likelihood is unique so youdo not conflict with other students using this machine, let’s agree to using 10 000 + last 4 digits of your VTPID here.
- Hint: ssh to portal.cs.vt.edu, then choose the machine on which your other shell session runs. If you’reusing vscode you already have the ability to open a second terminal on the same machine. Or you can usessh’s jump option by doing ssh -J you@rlogin.cs.vt.edu you@locust to hop onto locust.
- You must specify the hostname as given, in the form locust-rlogin.cs.vt.edu. Please don’tblindly copy these instructions, replace it with the hostname of the machine your gnetcat6 is running on. Itshould be shown in your prompt; if not, run the commandhostname to see it.CS3214 Fall 2023 Exercise 1dpipe forks,waits for forks,waits forgnetcatl 15999 wcgnetcat locust15999 <pipestdoutTCP/IPstdin–l 15999 /etc/passwdchinkapin locuststdinFigure 1: Using dpipe to cross-link two programs’standard input and output streams.using ordinary tools such as ls or cat. However, they do not represent actualdirectories or files – rather, their content is created dynamically when a user accessesthem, based on the current state of the system and its processes. The kernel createsa directory for each process, which is named for its pid. Within the directory, thereare additional files and directories that describe the current state of the process.After you have started dpipe with‘wc’and‘gnetcat -l …’, use the ps(1) commandto find the process ids of these three processes, then use ps or /proc to find theparent process id, the process group id, and the current status of these processes.
(a) Provide the following information in table form:Process PID Parent PID Process Group ID Statedpipe-ABCwcgnetcatYou may find the proc(5) man page useful. 5Use the single letter Linux shorthand for the process state. (Omit additional identifiers such as s or + youmay see.)
(b) You should find that all three processes are in the same state. To which of the3 states of the simplified process state diagram discussed in lecture does this statecorrespond?
Important: even though the relative order of some of the system calls is not important for the correct functioning of dpipe,yourassignedvariant of dpipe will have aspecific ordering your submission must reproduce to obtain full credit.Make sure that your dpipe-ABC does not return to the prompt prematurely, or fails
to return to the prompt after its children have exited.Important: We will use the compiler switches -O -Wall -Werror when compiling your program. If you compile without these switches, and any warnings occurin your code, you run the risk of failing our autograding scripts.