Killing a runaway process

When the XL C/C++ application program you are developing and testing runs unchecked, the processes assigned to it can be out of the normal control of the application user. These processes need to be ended to free address spaces and associated storage. You may need to kill an application program's active processes to do this.

Before you can kill an application program's processes, you must know the process IDs to send a kill signal to. See Obtaining the status of z/OS UNIX application program processes for information on how to do this.

It is possible to add signal-handling code to your XL C/C++ application program, enabling it to monitor the status of its processes. In the event that the application program detects and intercepts a program signal indicating that one of these processes is not behaving as anticipated, it can then take appropriate action based on the specific signal received. The application program can then kill the flagged process and take the appropriate processing action. You need to use the XL C/C++ signal functions to do this. See z/OS XL C/C++ Runtime Library Reference, for the descriptions of the signal-handling functions. For information on XL C/C++ considerations for signal delivery, see z/OS XL C/C++ Programming Guide. For more information on POSIX signal handling, see The POSIX.1 Standard: A Programmer's Guide, by Fred Zlotnick (Redwood City, CA: The Benjamin/Cummings Publishing Company, Inc., 1991). This book contains a thorough discussion of POSIX.1-defined signal handling.

Follow these procedures to kill a process:

  • Interactively, you can do one of four things to end a runaway process:
    • Enter the break keystroke sequence (<Ctrl-C>) from the terminal.
    • Issue the kill command specifying the kill signal and the process IDs to end the processes and free memory. The following example shows two ways to kill the same z/OS® shell process:
      • kill -s KILL 2819 15163
      • kill -9 2819 15163
    • If the kill command with the kill signal does not end the process, specify the command again using the -K option, which will send a superkill signal. For example:
      • kill -K 2819 15163
    • If the shell prompt is not available, invoke the shell again. Then enter the kill command for the correct process ID from a user ID that has the appropriate privilege to kill the process.
  • From within an application program, you can do the following:
    1. Intercept a signal indicating that something has gone wrong with a process.
    2. Call the z/OS UNIX callable services getpid() or getppid() to get the process ID in question.
    3. Call the kill(pid,sig) function to pass the signal on to the process identified. The process responds to the signal according to how the process has been coded to handle signals.
    4. Call the sigaction() function if the action indicated by the kill command is to be changed.
    Note: There is no special method for killing a process under the TSO/E environment if the z/OS UNIX application program is started from that environment.

After you kill a process, control is returned to the parent process or the application continues on with conditional processing.