This assignment is Due on: Day: Friday, May 22, 1998 Time: Before 10:00 PM WARNINGS: TURN IN THIS ASSIGNMENT ELECTRONICALLY USING "turnin". LATE ASSIGNMENTS WILL NOT BE ACCEPTED. PLEASE NOTE ONE-TIME CHANGE OF DUE TIME. ---------------------------------------------------------------------- The purpose of this homework is to reinforce explanation of the following programming concepts: Use of pipe(2) Use of file routines read(2), write(2), close(2) Use of dup(2) You should have gotten copies of the files "mypipe.c" and "input.in" with your getjob distribution. The next few questions will require you to make several modifications to the source file(s). Please read the following steps through carefully to make sure you have a complete understanding of everything that needs to be done, BEFORE you begin. You may want to copy your original file(s) to a different directory so that you will ve a backup set in the event that something doesn't go exactly right. 0. Modify the mypipe.c file so that the child process executes a call to the xorencode prior to writing the Line buffer to stdout. The xorencode function works similarly to the read(2) and write(2) functions in that it takes an integer argument indicating the number of characters to process. Use the static variable secretkey as the 'key' parameter. Now compile and test the program. You should see some strange looking output. 1. Now modify the parent code to read in a single line from standard input and pass it through the pipe. Compile again and verify that everything is working properly. Now is a good time to ensure that the encoding works by running output through the code again (note -- xor encoding will reproduce the input when the output is processed). 2. Now modify both the parent and the child to run in a loop, with the parent reading in from stdin and writing to the pipe, the child reading from the pipe and xor encoding it, and printing the output to stdout. Stop reading input when you encounter the end-of-input character (Ctrl-D). Compile and test your program some more. 3. Add a signal handler for SIGPIPE in the parent process. 4. Finally, run your program and re-direct the input from the file input.in. -----------------------Start Answer Here----------------------- [p0] 15.0 points Include the source code for your main() function, showing all modifications that were made in completing Steps 0 through 2 above. ------------------------End Answer Here------------------------ -----------------------Start Answer Here----------------------- [p1] 5.0 points Include the source code for your SIGPIPE signal handler from Step 3 above. ------------------------End Answer Here------------------------ The next few questions will require you to make several modifications to the source file(s). Please read the following steps through carefully to make sure you have a complete understanding of everything that needs to be done, BEFORE you begin. You may want to copy your original file(s) to a different directory so that you will have a backup set in the event that something doesn't go exactly right. 0. Copy your file mypipe.c to origpipe.c and compile origpipe. You will need this later in the assignment. 1. Look over the material on dup(2) and dup2(3) on pages 376-378 of the Weiss book. Using those programming examples as a guide, modify your version of mypipe.c so that the parent process duplicates the writing end of the internal pipe (in the pfd[] array) onto its standard output (note - you MUST use dup(2) for this, and NOT dup2(3) ). 1. Now comment out the reading and writing loops in the parent and child processes and have both parent and child exec(2) origpipe in such a manner that the new version of mypipe will encode and then immediately decode your input. You will need to use dup(2) to map stdin and stdout appropriately. 2. Finally, compile and test this version of mypipe. -----------------------Start Answer Here----------------------- [p2] 15.0 points Include the source code for your main() function, showing all modifications that were made in completing Steps 0 through 5 above. ------------------------End Answer Here------------------------ Consider the following prototype and description for dup2(3c): int dup2( int fd1, int fd2 ); dup2(3c) causes the file descriptor fd2 to refer to the same file as fd1. Fd1 is a file descriptor referring to an open file, and fd2 is a non-negative integer that is less than the current value for the maximum number of open file descriptors allowed for the calling process. You may assume all file descriptors that are less than fd2 are open and currently in use. If fd2 already referred to an open file, not fd1, it is closed first. If fd2 refers to fd1, or if fd1 is not a valid open file descriptor, fd2 will not be closed first. Upon successful completion a non-negative integer, namely, the file descriptor, is returned. Otherwise, a value of -1 is returned. -----------------------Start Answer Here----------------------- [p3] 10.0 points For this problem, write the code for dup2(3c), using dup(2) and close(2) and include your function here. ------------------------End Answer Here------------------------ -----------------------Start Answer Here----------------------- [p4] 5.0 points All the examples and discussion so far of pipes under UNIX have assumed that the pipe is used to connect a parent process with its child. Would it be possible to create a pipe that would connect any arbitrary process with any other arbitrary process? Explain your answer. ------------------------End Answer Here------------------------