CSC128: Introduction to UNIX

Process Management

UNIX Process Structure

UNIX is a multitasking system, which means that it can execute more than one program at a time.  The execution of a program is called a process  The kernel keeps track of system resources and hands them out in a controlled fashion to all the programs that are running.  To do this, it creates a process structure.

The kernel makes the first process, the init process.  The init process's Process ID (
pid) is 1.  All other processes are children of this process.  To create another process, the initiating process is forked.  

When a process is forked, a complete copy of the process is made in system memory, and then the new process takes over in the memory space of the copy.  Every process has a pointer to the parent that initiated it.  By mapping the parent/child relationships between processes, a tree-shape similar to the file structure emerges, with the init process at the root.

This command lists the processes currently running, each with its pid. (See p. 826-9 in the textbook for more details on the ps command.)

Foreground and Background
Processes can be in one of many states.  The most typical states are foreground , background, and stopped.

Foreground processes execute while the shell waits for them to terminate.  When the program finishes, the shell gives another prompt.

Background processes execute the same as foreground processes, but the shell returns immediately with a prompt, an doesn't wait for the process to finish.

Stopped processes don't execute; they remain in memory, suspended and ready to execute if ever placed in the foreground or background.  Note that many programs don't take to being placed in the background, and stop themselves instead--
vi is an example.

This command will display a listing of the shell's current jobs.

fg %
fg %n
 (job number n)
This command will place the specified job (or the last job if none is specified) in the foreground.

bg %
bg %n  
(job number n)
This command will place the specified job (or the last job if none is specified) in the background. Or will start it in the background, leaving it there.

By appending an ampersand to a command, the shell will automatically start its process in the background, and return immediately with a prompt.

This command stops most processes.  The stopped process can be started again with
fg or bg.

This command cancels most processes.  The process is killed, gone forever.

Killing Processes
Sometimes, processes can't be easily ended in a normal way.

kill PID
kill -9 PID
This first of these commands will ask a process to terminate.  The second will force a process to terminate, whether it wants to or not.

Priority of Execution
The kernel gives different processes different priorities, so that important processes can get quicker access to the hardware, and unimportant porcesses don't get in the way.

This command displays the current processes in a continually-updating display, sorted by resource usage.

nice command
nice -n niceness command (10 is default niceness; 19 is usually max)
This command executes with a lower priority, so that it doesn't get in the way of other, more important, processes.  The superuser (root) can also use this command to give processes higher priority, by passing a negative value.

Hanging Up on a Process
Usually, when you log out of a shell, it hangs up on all remaining processes.  Some shells will warn you when this happens.  To keep a process from being killed when you log out, run it with the nohup (no hang- up) command.

nohup command

The process will run until it terminates normally or is killed.  
stdout will be redirected to nohup.out for the duration of the process's execution. 

Directory Stack Manipulation

Before the advent of putty and X-windows where we can have multiple windows open with cut-and-paste capability in them it was very common to use directory stack manipulation to navigate the directory structure. This can be a useful method of navigation, although is is somewhat dated. See page 313-314 for a treatment of dirs, pushd, popd

Here are some of the commands and resulting output in the exact order that I executed them:
[smauney@shaula:~/public_html/csc128]$ pwd
(pwd shows my current directory as ~/public_html/csc128

[smauney@shaula:~/public_html/csc128]$ dirs
(note that only the current directory is on the stack)

[smauney@shaula:~/public_html/csc128]$ pushd ../csc130
~/public_html/csc130 ~/public_html/csc128
(note that now I have pushed the directory ../csc130 onto the stack,
and at the same time (see below) I have been taken to the new directory)

[smauney@shaula:~/public_html/csc130]$ pushd ../../public
~/public ~/public_html/csc130 ~/public_html/csc128
(now I have pushed another directory  onto the stack)

[smauney@shaula:~/public]$ pwd
(note that when pushiing a directory onto the stack, I also cd to there)

[smauney@shaula:~/public]$ pushd
~/public_html/csc130 ~/public ~/public_html/csc128

[smauney@shaula:~/public_html/csc130]$ pwd
(note that if I just type pushd, I toggle back and forth between
the first 2 directories on the stack as reported by the pushd command)

[smauney@shaula:~/public_html/csc130]$ pushd +2
~/public_html/csc128 ~/public_html/csc130 ~/public
(now if I pushd +2, that tells the shell to move over 2 ADDITIONAL
spaces and go to that directory the directory that was the 3rd over
is now placed on the top of the stack)

[smauney@shaula:~/public_html/csc128]$ dirs
~/public_html/csc128 ~/public_html/csc130 ~/public
(look at the stack, now in the next command I am going
to pop ~/public_html/csc130 off of the stack)

[smauney@shaula:~/public_html/csc128]$ popd +1
~/public_html/csc128 ~/public
(now it is gone and I am back to toggling between 2 every time I
type pushd, until I add another to the stack)