| UNIX Tutorial | 13. Controlling processes |
|
Commands covered in this section: jobs, bg, fg, kill, nice, date, sleep Up to this point in the tutorial, you've been running just one process at a time. However, since UNIX is a multi-tasking operating system, you are not limited to a single process -- you can run an arbitrary number of processes simultaneously (within the constraints imposed by the resources available on the system). So far, in your interactions with the shell you've been launching a process and then waiting for it to complete and return control back to the shell (at which point you see the shell prompt again). This is called running a process in the foreground. It is also possible to run a process in the background. In this case, the program is launched and control is returned the the shell immediately, allowing you to run other commands. In the meantime, the initial process continues to run in the background. To place a process in the background when you launch it, add the ampersand character ('&') to the end of the command line (.e.g., "myprog &"). The ability to launch programs and move them between foreground and background execution is often called job control. Exercise 13.1 We will use the date and sleep commands to create process that runs long enough for us to see that the process continues to run in the background. The date command simply prints the current date and time, while sleep just waits for specified amount of time and does nothing productive (yes, it's a very lazy program!). We are going to run date twice, with a sleep in the middle. We'll use parentheses to run the three programs as a group, but we still need to separate each command so the shell recognizes them as distinct entities (this is done with the semicolon). The output will be redirected to a file.What if you've launched a process in the foreground and you want to move it to the background (perhaps because it's taking longer to execute than you thought and you have other work to do while the program runs). Since the process is running in the foreground, you can't give any other commands to the shell. Instead, you'll need some other way of communicating with the process. This is accomplished via a signal. Signals provide a means of sending an out of band message (that is, via some means other than the standard input) to a process. Many different types of signals are supported in UNIX. One instructs the process to clean up nicely and terminate, another instructs it to re-read its configuration files, etc. In order to place a foreground application in the background, you must first send it the suspend signal. This is accomplished by pressing CTRL-Z (hold the Control key and Z key down simultaneously). Exercise 13.2 Run the same command used in the previous exercise, but omit the ampersand at the end:Let's suppose that, despite your best efforts, you've written a program that has run amok and is causing problems on the system (perhaps it has an infinite loop and is chewing up all the CPU cycles, or it's producing 1000 times more output than you intended and it's filling up a disk). How would you stop it? Well, if it's running in the foreground, you could send it the terminate signal. This is done by pressing CTRL-C. If the program is running in the background, you can send it the terminate signal using the kill command. Exercise 13.3 Run the sleep command in the foreground. Wait a few seconds and press CTRL-C. This will terminate the sleep command and return control to the shell.Note that the job id only exists in the context of the currently running shell. Once a program has been placed in the background, it is not necessary to remain logged in. The process will continue to run even after you log out. In fact, it is common practice to do this for long running jobs. However, once you've logged out, you can no longer refer to the process by its job id. You can, on the other hand, still access it using its process id, since this remains in effect during the entire time the process is running. To find the process id for a given process, we can use our old friends ps and grep. Exercise 13.4
Run sleep in the background again. Then
use ps and grep to find
its process id and use kill to terminate
it.
One final note about processes: since UNIX is a multi-user
system, your actions can have an impact on the other users
of the system. Like other multi-tasking systems, UNIX uses
a scheduler to divide up time on the CPU and allocate
the slices of time to the various processes running on the
system.
In general, a higher priority is given to interactive processes (such as the login shell you are using now). This makes the system much more pleasant to use for humans, since we tend not to like waiting around for machines to do work for us. Therefore, you want to be careful when running batch processes (for example, a sort of a huge text database) to avoid using up all of the CPU resources and making interactive use of the system frustrating for other users. The nice program is perfect for this application. You can use it to lower the scheduling priority of your batch process. Generally, this will not increase the run time of your process significantly, but it will often help to reduce its effect on interactive processes a great deal. Exercise 13.5 Run sleep in the background again, this time using nice to lower the scheduling priority. Note that non-administrative users can assign nice values between 0 (no effect) and 19 (greatest effect). The higher the nice value, the lower the scheduling priority (in other words, the nicer the program becomes!).It's a good idea to use top to verify the nice level of your process and see what effect it's having on the system. You can also get a sense of this by just looking at how quickly the shell responds to interactive commands after you launch it. In the examples using sleep given here, the CPU usage will be trivial. However, you can apply the concepts you've learned here to any situation in which you need to run a batch process that is likely to use a significant amount of system resources.% nice +19 sleep 600 & |
|
|
< previous | table of contents | |
|