Processes and signals
From Linux 101, The beginner's guide to all things Linux.
Contents |
[edit] Processes
Any running program on your computer is refered to as a process. The system also assigns it a process ID, which is a number from about 10 to 32,000. (The first 10 usually go to the kernel)
[edit] Signals
Signals are how programs communicate to one another. They are also the software equivalent of hardware interrupts. Your programing is doing some task, but it receives a signal, and jumps to some different code.
There are a good many signals, but only the commonly used ones are listed below:
| Signal Name | Number | Purpose |
| SIGHUP ("hang up") | 1 | |
| SIGTERM | 15 | Tell the program to close gracefully. ("Save & exit") Careful: nano for example just quits and nothing gets saved! It is best to try using this on your programs before just assuming it shall save. |
| SIGKILL | 9 | Tell the program to quit immediately. ("No save, just exit") |
| SIGUSR1 | User defined #1. Depending on the program, sending this signal may cause some reaction. | |
| SIGUSR2 | User defined #2. Depending on the program, sending this signal may cause some reaction. |
There are also signals for problems, such as segmentation faults and floating point exceptions. You most likely will only see these ones if you are developing.
[edit] Monitoring processes
A great utility is ps to report process status.
-
ps ashow your processes that are currently running at a shell -
ps xshow your processes that do not have a controlling shell (ie, running in the behind the scenes) -
ps axushow all processes and group by user
|
Note: the ps command does not require its arguments to be preceeded by the dash. |
Another great utility is top. It does continuous monitoring on your system until you tell it to quit with the 'q' key on your keyboard.
Both of these utilities have a lot of options and features. Reading their man pages would be beneficial.
[edit] Sending signals
There are two useful programs when it comes to sending programs signals.
-
kill -<signal|signalnumber> <pid> -
killall -<signal|signalnumber> <process program name>
For example, if you had a run-away program called program1 (pid 5000) that you wanted to end, you could do:
-
kill -9 5000 -
killall -SIGKILL program1
[edit] Background programs
This section is the third article on bash.
If you were at a bash shell but running a program in the foreground, such as a text editor and you wanted to go back to a shell to run another command, it is possible without terminating the program.
If you hit CTRL-z it will suspend the program. Suspending means it will drop it to the background. The bg command lists all of your programs that you have backgrounded. If you want to bring one of them to the foreground, the fg command can do so.
$ top after running it, hit CTRL-z and you are back to the shell... $ nano -w file & $ bg [1]+ top & [2]+ nano -w file & $ fg 2 and you see the nano screen. exit it $ fg and you are running top again
To see currently running jobs, simply type jobs.
These tools can be fairly handy, especially if you want to save system resources. The one thing to note, is that fg and bg are both shell built-in commands. This means if you have 2 open sessions of bash running and you background something in one, you cannot foreground it in the other. If you need something like that, you should look into screen.
[edit] External Links
Introduction To Unix Signals Programming
A short intro to Signals programming
A short overview between BSD and POSIX signal programming
A list of UNIX signals
|
The Bash Articles Series |
|---|
| The shells and specifically bash | The bash environment | Processes and signals | Simple shell scripts | Shell scripting |

