15-213/18-243, Fall 2010Lab Assignment L5: ProcLabAssigned: Tuesday Oct. 12Due: Tuesday, Oct. 19, 11:59PMLast Possible Time to Turn in: Friday, Oct. 22, 11:59PMLogisticsThis is an individual project. All handins are electronic.Please contact the 15-213 staff list ([email protected]) for any questions.OverviewThis lab tests your understanding of process control and signal handling. You will be asked to use most ofthe system calls in the following list to complete this lab. Depending on your implementation, some of themmight not be used, but nonetheless, you should learn how to use all of them, as they might appear in examquestions. Examining the man pages is a good way to start.• Creating and running processes: fork, exec variants, exit• Synchronizing with children: wait, waitpid• Macros for inspecting exit status: WIFEXITED, WEXITSTATUS, WIFSTOPPED, etc• Signal handling: signal, sigaction, sigsuspend, sigprocmaskDownloading the assignmentYour lab materials are contained in a Unix tar file called proclab-handout.tar.gz, which you candownload from Autolab. After logging in to Autolab athttp://autolab.cs.cmu.edu1you can retrieve the proclab-handout.tar.gz file by selecting “Proclab - Download Lab Materials”and then hitting the “Save File” button.Start by copying proclab-handout.tar.gz to a protected directory in which you plan to do yourwork. Then run the command “tar zxvf proclab-handout.tar.gz”. This will create a directorycalled proclab-handout that contains the following files (some of them are generated by make):• puzzles.c: File in which you should write your code.• driver.a: Archive of driver object files that will be linked with puzzles.o to produce the driverbinary• Makefile: Used by make to generate the driver binary.• driver: A program you can use to evaluate your work. Type make to build this binary.• decipher: A helper program for the decipher puzzle.• README: A help file that describes the contents of the handout tarball.To compile your code and build the driver:linux> make cleanlinux> makeTo run the driver program to evaluate your work:linux> ./driverThe driver has an optional commandline argument that can run specific tests. For example, if you specifylinux> ./driver -t 03456then the driver will run tests only for puzzles 0, 3, 4, 5 and 6. Note that there must not be any spaces betweenthe numbers. If you omit the⁀-t option, then the driver will run all of the tests by default.The driver can submit an unofficial score to the class scoreboard if and only if you run all the tests. Thelatest score sent to the scoreboard will overwrite any previous ones. Submission of unofficial scores will bedone automatically by the driver and there is no option to disable this behavior.IMPORTANT: To submit your work to get an official score, please upload puzzles.c to Autolab. Asyou know, the driver program can only submit an unofficial score to the class scoreboard, and an unofficialscore will not be counted towards your grade.WARNING: Do not let the Windows WinZip program open up your .tar.gz file (many Web browsersare set to do this automatically). Instead, save the file to your AFS directory and use the Linux tar programto extract the files. In general, for this class you should NEVER use any platform other than Linux to modifyyour files, doing so can cause loss of data (and important work!).2Puzzle 0: timerCongratulations! You’re one of the promising candidates whom the 213 staff think could stop Dr. Evil fromreleasing his newly developed virus that is so potent that it could bring down the entire Internet. Before thestaff can assign you this honorable task, they have decided to give you one last challenge to help you proveyour mettle. If you can impress them, they will let you move on to your first mission.Your TaskYour job for this puzzle is to complete the function tgets in puzzles.c. The tgets function is aversion of the C fgets function that times out if the user doesn’t enter an input line quickly enough.ssize_t tgets(char*buf, int buf_size, int timeout_secs)This function reads in at most one less than bufsize characters from stdin and stores them into the bufferpointed to by buf. Reading stops after an EOF or a newline (\n). If a newline is read, it is stored intothe buffer. A NULL character (\0) is stored after the last character in the buffer. If the user fails to typean input line within timeoutsecs seconds, then tgets times out, returning to the caller with a valueof 0 to indicate that the timeout occurred. Otherwise it returns the length of the input string, excluding theterminating NULL character.To solve this puzzle you will need to modify the tgets function and write your own SIGALRM handler. Tohelp you, we have provided you with two functions in puzzles.c called mygets and set alarm handler.The mygets function behaves like fgets, but if it receives any signal while running, it will return −1 andset errno to EINTR. In this case, you must make sure that the received signal is really SIGALRM and notany other signal, because it is the SIGALRM signal that indicates that the timeout has occurred.In addition to modifying the tgets function, you will need to write a SIGALRM handler. The set alarm handlerfunction installs your SIGALRM signal handler and returns the address (a function pointer) of the previoushandler, which you can use later to restore the original handler.Notes• You should write exactly two functions: the SIGALRM handler and the tgets function.• You must use mygets to read a line from stdin.• Before tgets returns, you must restore the original alarm signal handler that you have overridden.• In Linux, slow system calls are automatically restarted after they are interrupted by signals. However,we do not want this behavior to happen for tgets, otherwise the timeout feature will not work. For-tunately, the staff has written setalarm handler such that system calls will not be automaticallyrestarted when SIGALRM is received. The details can be found in setalarm handler’s code.• You must use the SIGALRM signal to implement the timeout feature. You can use a global variableto help you determine whether the signal that woke mygets up is really SIGALRM.3• The exact specification of tgets can be found in puzzles.c. You may assume that the caller willnot pass in invalid parameters to tgets.4Puzzle 1: racerWe have been intercepting a stream of characters from two different processes that Dr. Evil has sent,
View Full Document