[an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] Skip to the content of the web site.

Process Management

Contents Previous Topic No Next Topic

In any multi-user operating system, it is possible to run more than one process at a time. While this will not be necessary for ECE 250, it may be useful at some point in the future. Suppose you have an executable you would like to run in the background while you're doing something else. You could open up another terminal, however, this would be a waste of recourses. Instead, you can run multiple processes (or jobs) with one terminal.

Running Multiple Processes

To run a process in the background, you can append the command by with an & which will signal to the operating system that the process should be started, but that you should be able to use the terminal:

{ecelinux:1} ./a.out &
[1] 12944
{ecelinux:2} 

At this point, you can continue to execute other commands (look at the output, edit a file, etc.) The terminology is to say that a.out is being run in the background. If you leave the & out, the process will run in the foreground and you will not be able to type in a new command until the process finishes. We will see below that it is possible to switch between these states in real time.

As almost all background processes are low priority, it is generally nice to let the operating system know that:

{ecelinux:1} nice + 19 ./a.out &
[1] 12944
{ecelinux:2} 

The nice +19 says: lower the priority of this process by 19 steps. In general, this will not affect the run-time of the process, however, it will make interactive processes, such as editors, more responsive as they will have a higher relative priority.

The next step is to explain the two numbers: [1] 12944. The first is a handle which allows you to refer to the process, while the second is the Process ID (PID). When a job is finished, you will see the following output:

[1]    Done                          ./a.out

however, you can ask which processes are currently running:

{ecelinux:2} jobs
[1]  + Running                       ./a.out
{ecelinux:3}

You can end a job by killing it. You can refer to the process by the handle, preceded by a %:

{ecelinux:4} kill %1
[1]    Terminated                    ./a.out
{ecelinux:5} jobs
{ecelinux:6}

Sometimes, kill will not work. In this case, you can use kill -9, or kill with extreme prejudice. This may end not only the process, but also, for example, the terminal running the process. For example,

{ecelinux:4} kill -9 %1
[1]    Terminated                    ./a.out
{ecelinux:5}

Terminating Foreground Processes

If you are running a process which appears to have gone into an infinite loop, you can try to end the process by using Ctrl-c. If you are lucky, Ctrl-c will end the process, and you can go back to editing your mistakes.

Unfortunately, some times, Ctrl-c may not work. In this case, you can try to suspend the current foreground job by pressing Ctrl-z:

{ecelinux:5} ./a.out
^Z
Suspended
{ecelinux:6}

Next, can put the job into the background by using bg, and then kill it once you have a handle to it:

{ecelinux:6} bg
[1]    ./a.out &
{ecelinux:34} kill -9 %1
{ecelinux:35} 
[1]    Killed                        ./a.out
Contents Previous Topic No Next Topic

Copyright ©2005-2008 by Douglas Wilhelm Harder. All rights reserved.

[an error occurred while processing this directive]