Be sure you have read chapter 1 and chapter 2 up through section 2.4.
Do the following problems. You may write out your answers by hand or using a word processor or other program, but please submit hard copy, either in class or in my mailbox in the department office.
strace -o OUT ls -l
Your mission for this problem is to run strace for a command of your choice, capture the output, and then describe what some of it means. Specifically, I want you to pick at least four lines of the output using different system calls and briefly explain each of these lines, describing in general terms what the system call is supposed to do and what the parameters and return value mean. (So, you will turn in a printout of the output of strace with your homework. You can mark it up with numbers and then refer to these numbers in your explanation.)
The man page for strace explains the general format of the output. To find out what the individual system calls do, you will need to read their man pages. Some of these are easy to find -- e.g., the first call is usually to execve, and man execve will tell you about it. Some are a little harder to track down -- e.g., man open produces information about an open command rather than a system call. man -k open produces a list of all man pages whose one-line descriptions include ``open'', and from this list one can perhaps guess that to look at the desired man page you need the command man 2 open. If the system call reported by strace ends in 64 (e.g., fstat64), the right man page can be found by removing ``64'' from the name (e.g., man fstat).
Do the following programming problems. You will end up with at least one code file per problem. Turn in your code by sending mail to cs4320@cs.trinity.edu, with each of your code files as an attachment. If there's any question of which file(s) correspond to which problems, explain in the body of the mail message. Please use a subject line such as ``homework 2'' or ``hw2''. You can develop your programs on any system that provides the needed functionality, but I will test them on one of the department's RedHat 9 Linux machines, so you should probably make sure they work in that environment before turning them in.
[bmassing@Athena]$ ./simple-shell ? ls Unable to find command ? /bin/ls Makefile another simple-shell simple-shell.cpp somefile ? /bin/ls -l total 28 -rw------- 1 bmassing bmassing 119 Oct 3 08:02 Makefile -rw------- 1 bmassing bmassing 5 Oct 3 08:00 another -rwx------ 1 bmassing bmassing 22035 Oct 3 08:15 simple-shell -rw------- 1 bmassing bmassing 1407 Oct 3 08:15 simple-shell.cpp -rw------- 1 bmassing bmassing 5 Oct 3 08:00 somefile ? /bin/ls junk /bin/ls: junk: No such file or directory ? [bmassing@Athena]$
You can add more functionality (searching a path for the command, doing more sophisticated parsing of inputs, exiting when the user types ``exit'', etc.). If you do, describe the added functionality in comments at the top of your code. I will give up to 5 extra points for significant extra features.
Turning the pseudocode into code mostly involves defining appropriate data structures for the variables in the pseudocode and replacing the type_prompt and read_command functions with appropriate real code. Your first step should probably be to read the man page for execve to see what arguments it expects, and then figure out what you need to do to turn what the user types in into suitable input to execve.
You will probably find that most of the code you write for this problem will be code to parse the input (accept a line of text and break it into a command and arguments). You can do this using C functions such as scanf, with the C++ string class, or whatever you prefer. If you use the C functions and fixed-size character arrays, try to make the program fail gracefully if the user supplies more input than your code has room to accept.
Start by compiling the program and observing its behavior with different numbers of threads. To compile with g++, you will need the extra flag -pthread, e.g.
g++ -o threads-cr -pthread threads-cr.cpp(On the RedHat 9 machines, this command produces a lengthy warning about one of the #included files. This message appears to be harmless. An updated version of the program that doesn't produce the message can be found in new-threads-cr.cpp. It produces other warnings on older systems.) Interestingly enough, the exact behavior of this program seems to depend both on the number of processors and on the release of the operating systems -- try it on one of the lab machines and then on one of the Dwarf machines to see what I mean. You may need to recompile when switching to a machine running a different release of the operating system.
Then make your changes and confirm that the program now behaves as expected, i.e., when one thread starts its critical region no other thread can start its critical region until the first one finishes.