Coding: Final Patch
After submitting your preliminary patch, you'll work on your final patch, where you will address any changes you've made to your design document. In this part, you'll implement your PID system, and use it to support the following system calls:
getpid,waitpid,fork, and_exit
You'll also need to implement exec.
Contingency Plans
If you can't get argument copying to work correctly for execv, you can at least allow the execution of programs ignoring arguments. This is not ideal, but it's better than nothing.
Testing
Testing getpid, fork, waitpid, and _exit
The final part of hw4test should now look like this:
**********
* testing getpid
* getpid() returned 1
**********
* testing fork (parent waits)
* Forked, in parent
* Forked, in child
* Child (pid 2) exited with status 42 (claimed as pid=2)
**********
* testing fork (child finishes first)
* Forked, in child
* Forked, in parent
* Child (pid 3) exited with status 54 (claimed as pid=3)
Program exited with status 0.
Operation took 2.366932066 seconds
Also, you can further test fork and waitpid with the /testbin/forktest program. The output should look like this:
OS/161 kernel [? for menu]: p /testbin/forktest
/testbin/forktest: Starting. Expect this many:
|----------------------------|
AABBBBCCCCCCCCDDDDDDDDDDDDDDDD
/testbin/forktest: Complete.
Program exited with status 0.
Operation took 0.947268061 seconds
Note that this program uses quite a lot of memory, since it runs a total of 32 processes. Currently, our VM system (DUMBVM) runs out of memory after running a few processes, so you may want to run this test on a freshly booted kernel and quit the kernel after running the test. If you try to run it twice, it will likely error out due to running out of memory (but shouldn't crash the kernel!).
Testing execv
Once you have execv written (or even partially written without any argument copying), can run the OS/161 shell from there, have it execute other programs, and then return to the shell. The provided shell is a lot like vbsh from Homework 2. The program is located at /testbin/sh and can be run with p /bin/sh. For convenience, you can also run it with the s command.
Here's a sample session using the /testbin/argtest and /testbin/add programs. Note that we don't have to write the /testbin/ part because the shell uses execvp from userland/lib/libc/unix/execvp.c which tries execv on each path in the PATH environment variable until it succeeds:
OS/161 kernel [? for menu]: s
/bin/sh: Timing enabled.
OS/161$ argtest testing more-testing three four five
argc: 6
argv[0]: argtest
argv[1]: testing
argv[2]: more-testing
argv[3]: three
argv[4]: four
argv[5]: five
argv[6]: [NULL]
/bin/sh: subprocess time: 0.353069691 seconds
OS/161$ add 37 5
Answer: 42
/bin/sh: subprocess time: 0.115529809 seconds
OS/161$ add 37 5 >add.out
/bin/sh: subprocess time: 0.157702277 seconds
OS/161$ cat add.out
Answer: 42
/bin/sh: subprocess time: 0.131020660 seconds
OS/161$ exit
Program exited with status 0.
Operation took 40.535884325 seconds
OS/161 kernel [? for menu]:
The shell supports background processes, so you can run something like this:
OS/161 kernel [? for menu]: s
/bin/sh: Timing enabled.
OS/161$ add 29 13 >add.out &
[2] add ... &
OS/161$ wait
pid 2: Exit 0
OS/161$ hash add.out >hash.out &
[3] hash ... &
OS/161$ add 5 2
Answer: 7
/bin/sh: subprocess time: 0.113413616 seconds
pid 3: Exit 0
OS/161$ cat hash.out
Hash : 69060
/bin/sh: subprocess time: 0.135048262 seconds
OS/161$ exit
Program exited with status 0.
Operation took 79.729596769 seconds
OS/161 kernel [? for menu]:
The shell builtin command wait waits for all background processes to finish. Also, before printint the prompt, the shell checks its children to see if any have finished, and prints a message if they have. If you haven't implemented WNOHANG in waitpid, you can still start background processes, but the shell will wait for them to finish before printing the prompt, which somewhat defeats the purpose of running them in the background.
You could also try this on a freshly booted kernel:
OS/161 kernel [? for menu]: s
/bin/sh: Timing enabled.
OS/161$ hw4test >hw4test.out &
[2] hw4test ... &
OS/161$ **********
* write() works for stderr
add 4 38
Answer: 42
/bin/sh: subprocess time: 0.115502896 seconds
pid 2: Exit 0
OS/161$ hash hw4test.out
Hash : 24682
/bin/sh: subprocess time: 5.149773867 seconds
OS/161$ exit
Program exited with status 0.
Operation took 83.216084594 seconds
Submission
Use the submit-this program as usual to submit your final patch.
(When logged in, completion status appears here.)