Process Management Operating Systems Spring 2005
Total Page:16
File Type:pdf, Size:1020Kb
Process Management Operating Systems Spring 2005 Lab Assistant Magnus Johansson [email protected] room: 1442 postbox: 54 (4th floor, building 1) phone: 018 - 471 6225 Course hompage http://www.it.uu.se/edu/course/homepage/os/distvt05 ² ¯ • The lab package can be downloaded from the course homepage. You unpack the package in an appropriate directory by tar zxf ProcessManagement.SunOS.tar.gz Begin with this. All files necessary for this lab is located in the OSLab/process mgmt/ directory. • Questions are marked Qx and programs Px, x ∈ IN. Answers must be well justified, answers of the type “yes”, “3”, or “no” will be rejected. • How to hand in: Send an email to [email protected] with answers to the questions and the source code to the programs. • There is a FAQ (frequently asked question) at the end of this assignment (se section 4), have a look at it before you start. ± This document was last updated January 26, 2005 1 Getting Started 1.1 ps(1) In this part of the assignment you will learn how to use the ps command to see what processes are running. Start by reading the manual page for ps(1), (do: man -s 1 ps in your shell)1. Use the ps command to see what processes are running on your machine. To use the ps command just type ps in a shell. You should not type ps(1). The (1) is just away of telling in what section of the manual pages the ps command belongs. Try using different options to see what they do. In particular, run ps with: • no options • -l • -e • -u ² ¯ Q1 What does ps do with no option and with these options? Q2 Explain what happens when you use -el? ± Try the top(1) command and get familiar with the pstree2 script. ² ¯ Q3 Explain what the -s flag, that you gave to man command above, actually does. Hint: use man man to read the manual page for the man command. ± The manual pages are divided into different sections. An introduction to each section can be found by man -sn intro, where n is the section number. ² ¯ Q4 Which section of the manual holds documentation for the UNIX system calls? Q5 And shortly, what is the difference between wait(1) and wait(2)? ± A useful command when looking for manual pages is apropos. It looks at the indexes of the manual pages and sorts out the occurences of what you are looking for. Try apropos wait. There is also a graphical tool for displaying manual pages called xman. Write xman & in your shell and familiarise yourself with it. It may not work depending on your system setup, in this case check your PATH environment variable that should include /usr/sup/X11/bin. In the worst case use /usr/sup/X11/bin/xman with full path. If you use the Konqueror web brower you can also type man:wait in the location field to get a good looking man page. 1.2 fork(2) and wait(2) In this part you will learn how processes are created, and how to avoid defunct (zombie) processes. Begin with reading the manual pages for fork(2) and wait(2). This is the standard way to use fork(2) to create a single child process: 1man 1 ps on Linux 2If pstree is not available on your platform, a script is provided in the lab package, run it with ./pstree 2 /* parent creates a process */ pid=fork(); /* the return value is a process ID, and * has the following meaning (always test these): */ switch (pid) { case -1: /* error: fork was unsuccessful */ case 0: /* this is the child process */ /* no process ID */ /* ... do something ... */ default: /* this is the parent process */ /* pid=process ID of the child */ /* ... */ } /* both processes continue here */ Note: both processes will run the code that follows the switch statement. Normally this is unde- sirable, and by forcing the child to terminate after it has done its “useful work”, we can prevent it. Pay attention to the switch command, you may want to use exit or break. The code shown above can be modified as follows: . case 0: /* this is the child process */ /* ... */ exit(0); . ² ¯ P1 Write a simple program that uses the fork(2) system call. Start with simplefork.c and complete it. Do not forget to wait for the spawned child: use wait(2), in order to avoid defunct processes. ± Note: “to use fork(2)...” or “ use wait(2)...” means that the system call fork or wait shall be used, you are not supposed to use fork or wait with 2 as an argument. A good way to find out what arguments a system call needs is to look at the man pages. Compile: type make and all your programs will be compiled. You will get an error message No rule to make target ‘server1.c’, needed by ‘server1’. That’s normal, you will create these files, it is the next step, do not worry about that, but care about errors from the compiler! To answer the following questions, you may experiment with the simple fork.c program. ² ¯ Q6 Processes marked <defunct> are known as zombies. When/why do they appear? When do they disappear? (Additional sleep(3C) statements may help you follow the behaviour more closely). Q7 What happens when you try to use kill or kill -9 to remove a zombie? What will happen if they are left behind after your program exits? ± 3 If you want to see a defunct process, put in a sleep(20) before the wait to delay the parent for 20 seconds. Start another xterm and use the ps -u [user name] command repeatedly. After 20 second, the defunct process should disapear from the listing. ² ¯ Q8 You see some printouts in the program (simplefork.c), what do pid,ppid and pgrp stand for? You may look at the manual pages of getpid(2),getppid(2) and getpgrp(2). The function sleep(3C) is used to delay the processes. ± 2 Process Management This assignment will demonstrate to you a more advanced use of fork(2) and wait(2) through a client-server program. The files you have to look at are server.c and netstuff.h. When you start the server on a machine it needs a “port” to listen to. It takes by default 5000. If another server is already running (if you are sitting on hamberg this is probably the case), it cannot take it, and you may have an error. It is possible to bypass this by giving another port. Of course, if you do this, the client has to know it. The client may take as an argument, the name of a machine (useful if you run the server on a different machine) or a port number. The client may take both argument in the order server name port number. • Begin with compiling the server (normally already done, with make that you used for the previous exercise). Try xterm -e ./server & and xterm -e ./client &3 to get a feeling of what the programs do. • Start several clients and observe the problem: the server can handle only one client at a time. The other ones have to wait. The client is used as follows: it waits for a string (you enter one at the prompt), sends the string to the server which does some useful job, in this case it returns the reversed string4 . The client has an auto mode, to activate it, enter auto at the prompt. To stop the client press Ctrl-D or Ctrl-C. Now you have a feeling of your task : make the server handle several clients at the same time. To achieve this, you will have to use fork(2). You will proceed in two steps : 1. add a fork() 2. handle the generated zombies 2.1 Accepting Several Clients The server works as follows : 1. initServer() which initializes network stuff, you are not supposed to look at this, and you do not have the sources either. 2. for(;;) infinite loop where the server (a) listens to the network and accept a new connection with acceptConnection() which returns 0 in case of success. (b) handle the connection with connectionJob(pid) You see in the code closeAccept(pid) which is to close the accepted connection, got from acceptConnection(). The problem is : when the server is in connectionJob(pid) it does not listen to the network. The function connectionJob returns only when the connection is closed. And that’s the reason why clients have to wait. 3the & is used to start the programs in the background. 4try to enter “dromedarenalpotoplanerademord” 4 client requests client requests listens listens acceptConnection acceptConnection handle the connection fork handle the connection connectionJob fork connectionJob close the connection close the connection terminate closeAccept closeAccept exit server.c server1.c Figure 1: Structure of server.c (given) and server1.c (your task) ² ¯ P2 Copy server.c to server1.c and modify server1.c so that it can accept several connections at the same time. The main process will do acceptConnection, fork, closeAccept while the spawned children will do connectionJob, exit. Do not care (yet) to the function childHandler. Figure 1 illustrates the programs. ± Note: you must get rid of error messages if you get any during execution. ² ¯ Q9 How are multiple connections handled now? ± Open several clients and make sure that they all get responses from the server. Close one con- nection(client)5 and use ps(1) with the right options to visualize the zombies, they are marked <defunct>. ² ¯ Q10 Why isn’t it a good idea to use wait in this program to get rid of the zombie servers.