LINUX TIPS AND TRICKS --- October 06, 2000

Published by ITworld.com, the IT problem-solving network
http://www.itworld.com/newsletters

*********************************************************************

Killing a Process
by Danny Kalev

This week I will discuss two techniques for killing a process: killing a 
process from itself and killing it from another process.


Killing Oneself

A process can kill itself by calling either exit() or _exit(), both are 
declared in "stdlib.h" as follows:

    void exit (int exit_code);
    void _exit (int exit_code);

exit() is a standard C function whereas _exit() is a system call. 
_exit() terminates the process immediately, without performing any 
cleanup operations such as closing open files, flushing streams, etc.... 
exit(), on the other hand, terminates the process gracefully, allowing 
it to perform cleanup operations before exiting. In both cases, the 
value exit_code is stored as the exit code of process, where 0 indicates 
successful termination and any other value indicates an error. Using the 
atexit() function, you can register functions called automatically 
before the process terminates:

    int atexit(void (*pf)(void));

atexit() takes a pointer to a function that takes no arguments and 
doesn't return a value. When exit() is called, the functions registered 
via atexit() are invoked in the opposite order from which they were 
registered. Remember:  if _exit() is called, functions registered via 
atexit() are not invoked.


Killing Others

You can also kill a process by using the kill() function. kill() has the 
following prototype:

    int kill(pid_t pid, int sig);

kill() sends the signal sig to the process with the specified pid. The 
signal SIGTERM kills a process gracefully, allowing it to perform 
necessary cleanup operations before exiting. Note that a process may 
ignore the request and continue running. To kill a process 
unconditionally, use the SIGKILL signal instead. 

Under Linux, the pid value is divided into the following four 
categories:

    1. pid  0  -- The signal is sent to a process with the specified 
                   pid.

    2. pid -1 -- Pid indicates a process group. The signal is sent  
                     to all the processes in the specified group. For   
                     example: kill(-6413, SIGKILL); immediately         
                     terminates all the processes in process group      
                     6413.

    3. pid = 0 -- The signal is sent to all the processes in the        
                  process group of the current process. 

    4. pid = -1 -- Sent to all the processes in the system except 
                   init(). This is used for shutting the system down.

kill() can only kill processes that share the same user ID as the 
killing process. Only a process with user the ID 0 can kill any process 
on the system.


Resources

Using the m4 Macro Processor 
Say goodbye to repetition with this powerful utility
http://www.itworld.com/jlw/lintps_nl/lw-2000-04/lw-04-m4.html

Sending signals 
There's more than one way to kill a process. Which should you use?
http://www.itworld.com/jsw/lintps_nl/swol-04-1998/swol-04-unix101.html

Errno Libretto 
The system call return value is supplemented by the error number, or 
errno value.
http://www.itworld.com/jsw/lintps_nl/swol-10-1995/swol-10-sysadmin.html

************************************************************************ 

About the author
----------------
Danny Kalev is a system analyst and software engineer with more
than 10 years of experience, specializing in C++ and
object-oriented analysis and design on various platforms including
VMS, DOS, Windows, Unix, and Linux. His technical interests involve
code optimization, networking, and distributed computing. He is
also a member of the ANSI C++ standardization committee and the
author of ANSI/ISO C++ Professional Programmer's Handbook (Que,
1999). Contact him at linuxnl@excite.com.

*********************************************************************

CONTACTS

* For editorial comments, write Andrew Santosusso, Associate Editor, 
Newsletters at: andrew_santosusso@itworld.com
* For advertising information, write Dan Chupka, Account Executive at:
dan_chupka@itworld.com
* For recruitment advertising information, write Jamie Swartz, Eastern
Regional Sales Manager at: jamie_swartz@itworld.com or Paul Duthie,
Western Regional Sales Manager at: paul_duthie@itworld.com
* For all other inquiries, write Jodie Naze, Product Manager,
Newsletters at: jodie_naze@itworld.com

*********************************************************************

Copyright 2000 ITworld.com, Inc., All Rights Reserved. 

http://www.itworld.com
