Recall that every command in UNIX is a program. Now let us take a
little look under the hood. When you log in, shell is launched. The
shell accepts commands you enter at the prompt and sends them to the
kernel, or operating system, which runs the program. This can
cause output to be put to the screen, as in ls
, or happen
without comment, as in rm
.
Programs that are running in UNIX are called processes.
Every process has an owner and an integer associated with it called a
process ID (PID
). The user who spawns a process will
generally be its owner. You are the owner of all processes you spawn.
Many, such as ls
, last such a short time you never notice
them beyond the output they produce; they terminate in a fraction of a
second after you enter them. When you log into your host, you actually
are launching a program; this is your shell. When the shell terminates,
your terminal session will be gone. At the command line, enter
ps
and you will see something like this.
unix> ps PID TTY 10355 pts/1 10356 pts/1 unix> TIME CMD 00:00:00 bash 00:00:00 ps
The ps
command shows all processes currently running
spawned by your shell. On this machine, the shell’s (bash) process ID is
10355. By entering ps aux
at the command line, you can see
all processes running on your UNIX server, along with their process IDs
and an abundance of other information. Try this at several different
times. If you are using a server, you will see processes spawned by
other users. You will also see other processes being run by the system
to support your machine’s operation.
An example of a program that does not finish its work immediately is the program bc. We show a sample bc session here; this application is a simple arbitrary-precision calculator.
unix> bc bc 1.06.94 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type ‘warranty’. 3+4 7 4*5 20 2^20 1048576 2^100 1267650600228229401496703205376 quit
When you type bc
at the command prompt, the shell runs
the bc program. This program continues to run until you stop it by
typing quit. To see bc
’s process ID, start bc and then type
Control-Z to put it to sleep. This interrupts the bc
process, puts it in the background, and returns you to your shell. Then
enter ps
at the command prompt to see the process ID for
your bc session.
unix> bc bc 1.06.94 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type ‘warranty’. [1]+ unix> ps PID 14110 14253 14254 unix> Stopped TTY pts/4 pts/4 pts/4 bc TIME 00:00:00 00:00:00 00:00:00 CMD bash bc ps
Try typing exit
to log out; you will see something like
this.
unix> exit exit There are stopped jobs. unix>
Now type jobs
at the command prompt. You will see
this.
unix> jobs [1]+ Stopped unix> bc
You can end the job bc
labeled [1]
by doing
the following
unix> kill %1 unix> jobs [1]+ Terminated unix> jobs unix> bc
If several jobs are stopped, each will be listed with a number. You
can end any you wish to by entering a kill
command for each
job. When you type jobs
at the command line the first time,
it will tell you what jobs it has suspended. After that, you will see a
(possibly empty, like here) list of jobs still in the background. Do not
dismiss a shell with running jobs; end them to preserve system
resources.
You can bring your stopped job into the foreground by entering
fg
at the command prompt.
Programming Exercises
Start up a session of bc and put it into the background using control-Z. Do this for several sessions. Type in some calculations into some of the sessions and see if they reappear when you bring the bc session containing that calculation into the foreground.
The bc calculator has variables which allow you to store numbers under a name. These play the role of the symbols described in Chapter 0, but they are limited to storing numbers. Here we show some variables being created and some expressions being evaluated.
unix> bc bc 1.06.94 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type ‘warranty’. cow = 5 pig = 2 horse = 7 horse + cow 12 horse/pig 3 pig/horse 0 cow^horse 78125
Replicate this session. Put it into the background and bring it into the foreground. Were your variables saved? Notice that this calculator does integer arithmetic. The = sign you see is actually assignment, and not test of equality.
- Look at one of the algorithms for converting a binary number into
a decimal number. Can you step through the
process using
bc
and make it work?