15 213 18 243 Fall 2010 Lab Assignment L5 ProcLab Assigned Tuesday Oct 12 Due Tuesday Oct 19 11 59PM Last Possible Time to Turn in Friday Oct 22 11 59PM Logistics This is an individual project All handins are electronic Please contact the 15 213 staff list 15 213 staff cs cmu edu for any questions Overview This lab tests your understanding of process control and signal handling You will be asked to use most of the system calls in the following list to complete this lab Depending on your implementation some of them might not be used but nonetheless you should learn how to use all of them as they might appear in exam questions 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 sigprocmask Downloading the assignment Your lab materials are contained in a Unix tar file called proclab handout tar gz which you can download from Autolab After logging in to Autolab at http autolab cs cmu edu 1 you 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 your work Then run the command tar zxvf proclab handout tar gz This will create a directory called 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 driver binary 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 clean linux make To run the driver program to evaluate your work linux driver The driver has an optional commandline argument that can run specific tests For example if you specify linux driver t 03456 then the driver will run tests only for puzzles 0 3 4 5 and 6 Note that there must not be any spaces between the 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 The latest score sent to the scoreboard will overwrite any previous ones Submission of unofficial scores will be done 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 As you know the driver program can only submit an unofficial score to the class scoreboard and an unofficial score will not be counted towards your grade WARNING Do not let the Windows WinZip program open up your tar gz file many Web browsers are set to do this automatically Instead save the file to your AFS directory and use the Linux tar program to extract the files In general for this class you should NEVER use any platform other than Linux to modify your files doing so can cause loss of data and important work 2 Puzzle 0 timer Congratulations You re one of the promising candidates whom the 213 staff think could stop Dr Evil from releasing his newly developed virus that is so potent that it could bring down the entire Internet Before the staff can assign you this honorable task they have decided to give you one last challenge to help you prove your mettle If you can impress them they will let you move on to your first mission Your Task Your job for this puzzle is to complete the function tgets in puzzles c The tgets function is a version 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 buf size characters from stdin and stores them into the buffer pointed to by buf Reading stops after an EOF or a newline n If a newline is read it is stored into the buffer A NULL character 0 is stored after the last character in the buffer If the user fails to type an input line within timeout secs seconds then tgets times out returning to the caller with a value of 0 to indicate that the timeout occurred Otherwise it returns the length of the input string excluding the terminating NULL character To solve this puzzle you will need to modify the tgets function and write your own SIGALRM handler To help 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 and set errno to EINTR In this case you must make sure that the received signal is really SIGALRM and not any 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 handler function installs your SIGALRM signal handler and returns the address a function pointer of the previous handler 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 Fortunately the staff has written set alarm handler such that system calls will not be automatically restarted when SIGALRM is received The details can be found in set alarm handler s code You must use the SIGALRM signal to implement the timeout feature You can use a global variable to 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 will not pass in invalid parameters to tgets 4 Puzzle 1 racer We have been intercepting a stream of characters from two different processes that Dr Evil has sent but we aren t sure what they mean Your job is to synchronize the processes such that both strings can be extracted We know that one string is printed one character at a
View Full Document