Personal tools
You are here: Home Docs Help! Reference Manuals Introduction to Linux Processes Processes inside out Life and death of a process

Life and death of a process

Note: Return to reference manual view.

A Hands on Guide, 1.27 Edition, Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008 Machtelt Garrels

1. Process creation

-

A new process is created because an existing process makes an exact copy of itself. This child process has the same environment as its parent, only the process ID number is different. This procedure is called forking.

After the forking process, the address space of the child process is overwritten with the new process data. This is done through an exec call to the system.

The fork-and-exec mechanism thus switches an old command with a new, while the environment in which the new program is executed remains the same, including configuration of input and output devices, environment variables and priority. This mechanism is used to create all UNIX processes, so it also applies to the Linux operating system. Even the first process, init, with process ID 1, is forked during the boot procedure in the so-called bootstrapping procedure.

This scheme illustrates the fork-and-exec mechanism. The process ID changes after the fork procedure:

Figure 4-1. Fork-and-exec mechanism

fork-and-exec.png

There are a couple of cases in which init becomes the parent of a process, while the process was not started by init, as we already saw in the pstree example. Many programs, for instance, daemonize their child processes, so they can keep on running when the parent stops or is being stopped. A window manager is a typical example; it starts an xterm process that generates a shell that accepts commands. The window manager then denies any further responsibility and passes the child process to init. Using this mechanism, it is possible to change window managers without interrupting running applications.

Every now and then things go wrong, even in good families. In an exceptional case, a process might finish while the parent does not wait for the completion of this process. Such an unburied process is called a zombie process.

2. Ending processes

-

When a process ends normally (it is not killed or otherwise unexpectedly interrupted), the program returns its exit status to the parent. This exit status is a number returned by the program providing the results of the program's execution. The system of returning information upon executing a job has its origin in the C programming language in which UNIX has been written.

The return codes can then be interpreted by the parent, or in scripts. The values of the return codes are program-specific. This information can usually be found in the man pages of the specified program, for example the grep command returns -1 if no matches are found, upon which a message on the lines of "No files found" can be printed. Another example is the Bash builtin command true, which does nothing except return an exit status of 0, meaning success.

3. Signals

-

Processes end because they receive a signal. There are multiple signals that you can send to a process. Use the kill command to send a signal to a process. The command kill -l shows a list of signals. Most signals are for internal use by the system, or for programmers when they write code. As a user, you will need the following signals:

Table 4-2. Common signals

Signal name Signal number Meaning
SIGTERM 15 Terminate the process in an orderly way.
SIGINT 2 Interrupt the process. A process can ignore this signal.
SIGKILL 9 Interrupt the process. A process can not ignore this signal.
SIGHUP 1 For daemons: reread the configuration file.

You can read more about default actions that are taken when sending a signal to a process in man 7 signal.

Linux Rackmount Server
For high quality performance Multi-core RAID servers at unbeatable prices with ongoing maintenance and support.
IT Support Bristol
IT Services and IT Support provider based in Bristol. Computer maintenance with server support as standard.
Mobile broadband
Compare mobile broadband deals and offers at the UK's best mobile broadband comparison site
Best Mobile Broadband Deals
Compare wireless mobile internet deals on Mobile Broadband Genie, the independent UK price comparison website.
IT Support
Award winning IT Support from help4IT, providing all the IT services of a dedicated IT department to businesses in London and the UK.
Call 0800 043 4448.
Introduction to Linux
Comprehensive 5 day introductory course in London, 8-12 March/10-14 May. Covers installation, command-line, Gnome, power tools, Shell scripting, Office Applications and more.
Broadband Packages
Compare broadband packages in your area with Broadband Choices, the UK's leading Ofcom-accredited broadband comparison calculator
IT Support Bristol
Netzen provide business IT Support for London, Bristol & Bath, Supporting Microsoft, Cisco, Linux & Mac
IT Support
For The Best IT Support In London Make Our IT Department Your IT Department. Microsoft Gold Certified Partner.
IT Support London
IT Services and IT Support London and Microsoft Small Business Specialist.
IT Support London
Efficent IT Support & Hosted Services for SME businesses in and around greater London.
IT SUPPORT
Award winning IT Services from London provider Wavex offering support, advice, and training
IT Outsourcing
A full range of services, from hosting, data recovery and day-to-day support through to Board-level IT advice through our virtual IT Director offering.
Data Recovery
Professional data recovery services