Processes
Operating Systems Lab Class 2
This lab is about processes. We will use the Linux operating system, which is a unix-style operating system. We can trace its lineage1 back to Multics via Minix2 (which was written by A. Tanenbaum, the author of our textbook). There are plenty of variants of Linux, which all use the Linux kernel3 but are accompanied by different collections of software. You may have heard of popular distributions such as Ubuntu, Debian, Fedora and RedHat. We are going to use TinyCore Linux4, a lightweight distribution that comes with minimal software, but still has a graphical user interface thanks to an efficient window manager. This means that it will run easily in our virtual machine, QEmu.
Feel free to work on this lab either in pairs or on your own.
1 Getting started
1. Downloadtheos-lab2.zipfilefromCanvasandunzipthecontentintoyourN:drive.There should be a directory os-lab2 after unzipping. In the command prompt change the direc-tory into that folder:
N:
cd os-lab2
2. Start TinyCore in the QEmu virtual machine by running:
“C:\Program Files\QEmu\qemu-system-i386.exe” -hda tinycore.img -m 512
This command loads the Intel 32-bit version of QEmu, using the .img file as the hard drive, and with 512mb main memory. You should see a text mode screen appear that reports various activities and eventually turns into a graphical desktop after about a minute.
3. In order to use your keyboard and mouse inside the virtual machine, press Ctrl+Alt+g. This tells QEmu to grab your input devices. In order to release them, press Ctrl+Alt+g again.
4. Familiarise yourself with the TinyCore desktop. There is not much software installed, but you can get a feel for the window system.
2 Terminal
5. Open a terminal using the icon at the bottom of the screen. The terminal runs a shell program (bash in our case), which is the Linux equivalent of the Command Prompt in Windows.
You can control the terminal using the following keys:
1 http://www.computerhope.com/history/unix.htm 2 http://www.minix3.org
3 http://www.kernel.org/linux.html
4 http://distro.ibiblio.org/tinycorelinux
1
• Ctrl+c • Ctrl+l • ↑, ↓
• Ctrl+r • →|
kill the current process clear the screen
look through your command history search through your command history
auto-complete command jump to beginning of line jump to end of line
• Ctrl+a
• Ctrl+e
• Ctrl+←, Ctrl+→ jump back/forward one word
The most important commands are the following. Note that directories are delimited by / (as opposed to Windows, which uses \).
• ls list contents of current directory
• ls -la get a detailed listing
• cat some existing file print the contents of some existing file in the terminal
• less some existing file displays the contents of some existing file; you can scroll with the cursor keys; quit by pressing q.
• cp some existing file some new file copies the file some existing file to the file some new file
• mv some existing file some new file moves/renames the file some existing file to the file some new file
• mkdir some new directory creates some new directory
• cd some directory changes the current directory to some directory
• cd ..
• cd ∼
• pwd
• whoami
• nano some file opens some file in the text editor nano
• grep some text some file outputs the lines containing some text in some file
• rm some file removes some file
• top displays a live view of the processes that are running; quit by pressing q
• man some command displays the documention of some command; quit by pressing q There are many more useful commands (which actually form a programming language to write
so-called shell scripts). Have a look at http://ss64.com/bash.
6. Try out the above commands to familiarise yourself with the Linux terminal.
changes to the parent directory (denoted by ..) changes to your home directory (denoted by ∼)
tells you the current directory tells you your user name
3 Processes in Linux
7. You can view processes in Linux by running the command top. TinyCore has an improved version of this command called htop
8. Open a second terminal window and put it side-by-side with the other one. Run man htop. This will display the documentation of the htop command. Read through the documentation to understand the columns of the table that is displayed by htop, and use it as a reference to perform the following tasks (see Appendix 1 for more details).
9. Answer the following questions: Which process is taking most CPU right now? Which one is taking most memory?
10. Press H and K to look at user and kernel threads. Which threads are running right now? What does this tell us about how Linux handles threads?
11. All Linux processes are children of the init process. Where do you see this information in htop’s output? What is the process ID of init?
Press F5 to look at the process tree. You can resize the terminal window to make this more easily visible.
12. Close the man pages in your second terminal, and use it to run a CPU-bound process while watching it in htop. For example try
cat /dev/urandom > /dev/null
This command requests random numbers from a random number generator5 and discards the data by writing it to the null device6 (the > symbol writes the output of the preceding command into the following file).
You can kill the process by pressing Ctrl+c in the terminal window where you started it.
How much CPU does it take? Let it run for about a minute and observe the effect on the statistics in htop. What is changing?
13. Another way to look at a snapshot of the currently running processes is using the ps command. Run
ps ax
This list might be long, so you might want to use less to make it scrollable (the | symbol forwards
the output of one command to the input of another):
ps ax | less
14. You can use the grep command for searching through ps’s output. For example ps ax | grep “ps ax”
shows the line for the ps process itself. Try this command for other processes.
15. You can kill a process with the command
kill process id
where process id is the process’ ID as displayed in ps’ output.
To try run a program from the start menu, for example an editor, and kill it from your terminal. 16. You can also kill a process from top or htop by pressing k and entering the process ID.
5 http://linux.die.net/man/4/urandom 6 http://linux.die.net/man/4/null
Try this again by running a program from the start menu and kill it from your terminal using htop.
17. Kill some more processes that run on your system and see what effect it has on your machine. Can you kill the terminal? The windowing system? The whole machine?
If you are refused to kill a process prefix your command with the sudo command:
sudo kill process id
This runs the command as “superuser” giving you full permissions over the system. You might cause damage to the virtual machine, but don’t worry you can just download it again and restart QEmu.
18. Start 10 CPU-bound processes, for example by running the following command 10 times:
cat /dev/urandom > /dev/null &
Adding & at the end of a command runs the process in the background. The process IDs are printed in the terminal. You can also find the process IDs using ps or htop.
Observe your processes in htop.
19. In Linux, each process has a priority. You can change the priority using the renice command7. Put a terminal window side-by-side with htop if you have not done yet. Then try changing the priority of your processes using renice and observe the effects in htop.
Kill the processes. You can also kill a group of processes using the killall command.
4 Forking Processes in Linux
System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose of fork() is to create a new process, which becomes the child process of the caller. After a new child process is created, both processes will execute the next instruction following the fork() system call. Therefore, we have to distinguish the parent from the child. This can be done by testing the returned value of fork():
• If fork() returns a negative value, the creation of a child process was unsuccessful.
• fork() returns a zero to the newly created child process.
• fork() returns a positive value, the process ID of the child process, to the parent. 7 http://linux.about.com/library/cmd/blcmd18_renice.htm
System call fork() creates two identical copies of address spaces, one for the parent and the other for the child. Both processes will start their execution at the next statement following the fork() call. In this case, both processes will start their execution at the assignment statement as shown below:
20. Open the file fork1.c in the editor. This program is written in the C language, which is commonly used for most operating system implementations (e.g. the Linux kernel is written in C). The syntax of Java resembles the one of C, so it should not be too difficult to read the code and understand what it is doing.
#include
int main(int argc , char∗∗ argv) {
int x=100;
int return value = 0; printf(”Hello \n\n”);
return value = fork ();
printf (”bye\n” );
printf(”x=%d\n”, x);
printf(”return value =%d\n”,return value); printf(”process id= %d\n\n\n”,getpid());
return 0 ; }
21. We will now compile the code and run it on the Linux server at Sussex.8 Select PuTTY from the Program Files menu. The PuTTY Windows utility will allow you to remotely log on a machine using the secure shell SSH protocol 8. PuTTY will provide you with a terminal where you can compile the source code on the remote machine. To do so, type the hostname unix.sussex.ac.uk in the Host Name text box and click Open. On the shell, type the directory where you have fork1.c.
22. Compile the code
gcc fork1.c -o fork
The program gcc is a compiler for C.
23. Run the generated executable
8 http://www.sussex.ac.uk/its/services/networkandstorage/serverhosting/linuxserver
./fork
You should see output that resembles the following:
Hello
bye
x=100
return_value = 21667
process id = 21666
bye
return_value = 0
process id = 21667
Which lines are printed by which the parent process? Which by the child process? Fill in Table 1 based on the output of running ’fork1.c’.
Table 1: The output of running fork1.c
24. fork() creates a new process, so you now have two identical processes. To distinguish between them, the return value of fork() differs. In the original process, you get the PID of the child process. In the child process, you get 0.
Open the file fork2.c in the editor and try to understand what it is doing. Then compile it and run it. You should see output that resembles the following:
Parent: Hello, I am the Parent process
Parent: Here’s my PID: 1073
Parent: The PID returned by fork() is: 1076
Child: Hello, I am the Child process
Child: Here’s my PID: 1076
Child: The PID returned by fork() is: 0
25. The wait() system call is used by a parent process to wait for the status of the child to change. A status change can occur for a number of reasons, the program stopped or continued, but we will only concern ourselves with the most common status change: the program terminated or exited. Parent waits until children terminate.
Open the file fork3.c in the editor and try to understand what it is doing. Then compile it and run it. You should see output that resembles the following:
PID
return value
Parent process
……….
……….
Child process
……….
……….
fork is starting
This is the parent
This is the child
This is the child
This is the parent
This is the child
This is the child
This is the child
Interpret the order of the messages printed in the output. What is happening?
26. Now uncomment lines 7 (int status;) and 24( wait(&status); )to use the wait() system call. Compile and run again. Explain why the output is now different.
5 Learning more about Linux
If you have some time left and you want to learn more about Linux (which is very useful for your student and professional life) then try more of the commands from http://ss64.com/bash.
Appendix
Htop columns description
Htop is a process viewer for Linux. Below is the columns description of htop:
• PID: A process’s process ID number.
• USER: The process’s owner.
• PR: The process’s priority. The lower the number, the higher the priority.
• NI: The nice value of the process, which affects its priority.
• VIRT: How much virtual memory the process is using.
• SHR: How much physical RAM the process is using, measured in kilobytes.
• S: The current status of the process (zombied, sleeping, running, uninterruptedly sleeping, or traced).
• %CPU: The percentage of the processor time used by the process.
• %MEM: The percentage of physical RAM used by the process.
• TIME+ : How much processor time the process has used.
• COMMAND: The name of the command that started the process.