Next: Implementation Details Up: No Title Previous: Objective

# Problem Statement

This project consists of several small parts, to help you understand the concepts better.

1. (5 points) Implement a C or C++ program that takes two command line arguments, double values A and B, computes and outputs
1. How many iterations of A = sin(A + sin(B)) are required before the value of A converges to within 10-15.
2. How many iterations of A = cos(A + cos(B)) are required before the value of A converges to within 10-15.
3. a line stating which of the two equations took fewer iterations to converge.

Do this as a normal, regular, single-process, single-threaded program.

2. (20 points) Write a program than does the same as part 1, except that you are required to invoke two child processes to solve this problem:
• one child to calculate (but not print) the number of iterations that the sin formula takes. And then sends that result to the other child process,
• the other child to calculate and print the number of iterations that the cos formula takes, and receive the result from the first child process, print it, and compare the two values.
(So the parent process ends up doing very little, other than settings things up, spawning the child processes, and waiting for them to die.)

Use fork to spawn the child processes. Use a pipe as the means of communication to pass that value from the first child to the second child.

Hint: This requires two calls to fork, and one call to pipe.

3. (20 points) Write a program that uses UNIX fork system call to create two processes. The first process will be an execed instantiation of the ``ls -R'' command and it should pass its output to standard output. The second process will be an execed instantiation of the ``ls -lR'' command, and it should pass its output to standard output too.

Write and submit two versions of your program:

1. Concurrent Mode. The parent process will fork two child process almost concurrently (one immediately after another). On large input sets (that is, large directory trees for ls), you should notice that the output of the two child processes is interleaved. The output from ``ls -R'' will be just filenames, and the output from ``ls -lR'' will be standard ``ls -l'' formatted output with permissions, dates, etc.
2. Sequential Mode. The parent process will force a sequential execution of the two child processes, to prevent interleaving of outputs. In other words the output should be the concatenation from the two child processes.

Hint: The difference between the programs for two versions should be very small.

4. (55 points) Write a shell-like program that illustrates how UNIX spawns processes. This program will provide its own prompt to the user, read the command from keyboard input, execute the command, and then loop back to get another command.

Note: You may not use the system or popen library calls. Rather, you must use system calls such as fork, dup2 and the exec-family.

1. For the first part, it is sufficient to only handle ``argument-less'' command invocations, such as ``ls'' and ``date''.

2. Make the mini-shell (from the previous part) more powerful by allowing (an arbitrary number, zero or more) arguments to the commands. For example, it should be able to execute commands such as ``date'', ``more filename'', ``ls -l /tmp'', ``gcc -s -O -g -o testing foo.c'' etc.

3. Extend the mini-shell to do simple input file redirection. The shell should scan the input line for a less-than-sign (<) and if one exists, treat the single word after it as a filename. Any input that the command (the part before the <) reads from standard input, must be read from the file, rather than from the keyboard. If the file does not exist, an error must be generated, and the command not executed. For example: ``wc < datafile''

(Note: a string after the filename is illegal, and the shell should detect that, and print an error message instead of executing anything. For example: ``ls < datafile foo'')

4. Extend the mini-shell to do simple output file redirection. The shell should scan the input line for a greater-than-sign (>) and if one exists, treat the single word after it as a filename. Any output that the command (the part before the >) generates to standard output, must be written to the file, rather than displayed on the screen. If the file already exists, it should be truncated and overwritten. If the file cannot be created, an error must be generated, and the command not executed. For example: ``ls > outputfile'', ``ps -augx > file2'', ``date''.

(Note: any string after the filename is illegal, and the shell should detect that, and print an error message instead of executing anything. For example: ``ls > outputfile foo'')

5. Extend your mini-shell to allow it to view and modify environment variables. Your shell must understand the following commands: set, print. The format for using them is:
• setenv var=value (No spaces on either side of the =)
• printenv var
See the man pages for getenv(3c) and putenv(3c). Also environ(5) for some examples of commonly used environment variables. Hint: read the putenv(3c) man page carefully, including the NOTES section at the end.

6. Extend your mini-shell to allow it to provide some common builtin commands:
1. cd path : Change directory to specified path. Implement using chdir(2).
2. pwd : Print the current directory. Implement using getcwd(3C).
3. echo rest of line : Display the rest of the line.
4. exit : Exit the mini-shell.

7. Extend your mini-shell so that it has all the features of all of parts (b), (c), (d), (e) and (f). (If you already wrote it by adding each additional feature to the previous version, then you have nothing more to do for this part. But if you did them separately, then you need to create a combined version now.)

So, this version needs to accept the examples given in all the above parts, as well as things such as: ``ls -l /tmp > filefile'', ``grep Hello < infile > outfile'', ``grep Hello > outfile < infile''.

Note, however, that you do not have to be able to mix file redirection with the environment or builtin commands. So being able to do the following is not required: ``printenv PATH > filename'', ``pwd > file''

Next: Implementation Details Up: No Title Previous: Objective

Davin Milun
Sun Sep 6 19:41:44 EDT 1998