Last updated: February 1, 2021
Carleton University School of Computer Science
COMP 3000 (WINTER 2021) OPERATING SYSTEMS TUTORIAL 2
Tasks/Questions
1. Compileandrun3000shell.c.
2. ComparedtocsimpleshellfromTutorial1,whatfunctionalityimprovementshas3000shell introduced? List at least two improvements (functional differences). You can mention whether you found them by reading the source code or trying both shells.
By using both shells, you can see 3000shell shows the current user in the prompt whereas csimpleshell doesn¡¯t (with just a ¡°$¡±). Also, if you look at the code, you¡¯ll see 3000shell supports additional commands like ¡°plist¡±. Moreover, you may also notice richer functionalities of 3000shell, such as signal handling, support for stdout redirection, execution in the background, etc.
3. Payattentiontolines229-235.Howcanyourunaprograminthebackground?
Note: ¡°in the background¡± means the shell starting a program without waiting it to finish. So, the shell returns immediately to be ready to accept the next command.
As in bash, just use the ¡°&¡± operator at the end.
4. Observethebehaviorwhenrunningdifferentprogramsinthebackground.Whathappenstothe input and output of the program? Try this for different types of programs, such as ls and bc, or nano and top, or others of your choice. Write down your observations.
For non-interactive programs (those taking only inputs from the standard input, i.e., the command line arguments), no difference would be noticed. For interactive ones (e.g., nano and top), they may mess up the shared standard input and standard output.
5. Youmayhavetroubleinteractingwiththeshellafterrunningprogramsinthebackground.Howcan you recover from such a situation?
Note: you can do it in a harsh way now. By the next tutorial, we will see a smarter way.
This was used in one question of Assignment 1 so we will discuss the solution after the due date.
You can hit Ctrl-C to terminate both processes, if the interactive program also handles Ctrl-C. Otherwise, if the terminal is not frozen (but just losing key presses) you can try the
interactive program¡¯s own way repetitively (e.g., Ctrl-X for nano). As the last resort, you can kill them from another terminal using the kill command.
6. 3000shellimplementsasimpleformofoutputredirection(i.e.,thestandardoutputmentioned earlier). What syntax should you use to redirect standard output to a file?
Just >filename. Note space after the operator cannot be handled by 3000shell.
7. Make showeveryattempttofindabinary.
Use at each invocation, so you¡¯ll see attempted pathnames.
find_binary
printf()
stat()
8. Make the shell output “Ouch!” when you send it a
Register for in with , add an
signal.
the same way as other signals. Then, in .
SIGUSR1
SIGUSR1
main()
sigaction()
signal_handler()
fprintf() if (the_signal == SIGUSR1)
9. Delete line 324 (SA_RESTART). How does the behavior of 3000shell change?
As mentioned in the lecture, SA_RESTART is to restart/resume the system call interrupted by a signal. In the case of 3000shell, if line 324 was removed, after you send SIGUSR1, the command prompt will no longer work because the system call read() from the library call fgets() cannot be restarted, so it will wait infinitely.
10. Replace the use of find_env() with getenv(). How do their interfaces differ?
getenv() is neater and takes only one argument the environment variable name. It searches in the actual environment variables instead of our maintained *envp[].
11. Make plist output the parent process id for each process, e.g., “5123 ls (5122)”. Pay attention to the stat and files in the per-process directories in /proc.
is human-readable and /proc/
status
/proc/
2