Kill
[ # vi /user/include/bits/signum.h]
Terminate a process
Kill [-s signal 1 -p ] [-a] [--] p id…….
Kill – l [signal]
The command kill sends the specified signal to the specified process or process group. If no signal is specified the TERM signal is sent . TERM signal will kill process, when don’t catch signal . for other process , it may be necessary to use KILL (9) signal. Since this signal can not be caught.
Most modern shells have a buittin kill function , with a usage rather similar to that of the command described here. The ‘- a’ and ‘- p’ options , and the possibility to specify p ids by command name is local extension.
# | define | SIGUP | 1 | / * Hang ap (POSIx) */ |
# | define | SIGINT | 2 | / * Interrupt (ANSI) */ |
# | define | SIGQUIT | 3 | / * Quit (POSIx) */ (ctrl + 1) |
# | define | SIGILL | 4 | / * Illegal instruction(ANSI) */ |
# | define | SIGTRAP | 5 | / * TRACE trap (POSIx) */ |
# | define | SIGABRT | 6 | / * Abort(ANSI) */ |
# | define | SIGIDT | 6 | / * IDT trap(4.2 BSD) */ |
# | define | SIGBUS | 7 | / * Bus Error (4.2 BSD) */ |
# | define | SIGFPE | 8 | /*Floating – point Exception (ANSI) * / |
# | define | SIGXILL | 9 | /* Kill , un blockable (POSI x) * / |
# | define | SIGUSR 1 | 10 | /* user= defined signal 1(posi X) |
# | Define | SIGSEGV | 11 | /* segmentation violation(ANSI)*/ |
# | Define | SIGUSR 2 | 12 | /* user – defined signal 2(posi X) |
# | define | SIGPIPE | 13 | /* Broken pipe(posi X)*/ |
# | Define | SIGALRM | 14 | /* Alarm clock*/ |
# | Define | SIGTERM | 15 | /* Termination (ANSI)*/ |
# | Define | SIG8 Tk FLT | 16 | /* Stack fault*/ |
# | Define | SIG CLD | SIGCLD | /*same as SIGHLD (system V)*/ |
# | Define | SIGCHLD | 17 | /*Child status has changed(POSI x)*/ |
# | Define | SIGCONT | 18 | /*continue (POSIX )*/ |
# | Define | SIGSTOP(ctrl+z) | 19 | /*stop, un blockable(POSI X)*/ |
# | Define | SIG T STP | 20 | /*Key board stop(POSIX)*/ |
# | Define | SIG TTIN | 21 | /* Background read from tty (POSI X)*/ |
# | Define | SIGTTOU | 22 | /*Background write to tty*/ |
# | Define | SIGURG | 23 | /*Urgent condition on socket(4.2 BSD)*/ |
# | define | SIGX CPU | 24 | /*CPU limit exceeded(4.2 BSD)*/ |
# | define | SIGX FSZ | 25 | /* file size limit exceeded (4.2 BSD)*/ |
# | define | SIGX VT ALRM | 26 | /*Virtual alarm clock(4.2 BSD)*/ |
# | define | SIG PROF | 27 | /*profiling alarm clock (4.2 BSD)* |
# | define | SIG WINCH | 28 | /* Windows size change (4.3 BSD, SUN)*/ |
# | define | SIGPOLL | 81G10 | /*pollable event occurred system*/ |
# | define | SIGX IO | 29 | /* I/O now possible (4.2 BSD)*/ |
# | define | SIG PER | 30 | /* power failure restart(system v)*/ |
# | define | SIG SYS | 31 | /* bad system call*/ |
# | define | -NSIG | 65 | /* biggest signal number + 1(including real time signal)*/ |
# | define | SIGRT MIN | ||
# | define | SIGRT MAX |
# define SIGRTMIN (-libc – current – sigrmin ())
# define SIGRTMAX(-libc – current – sigrmax())
/* these are hinders limits of the kernel . these values should not be used directly at user level */
# define - - SIGRTMIN 32
# define - - SIGRTMAX (- NSIG -1 )
Implementation of kill command
Terminal - 1
# include <stdio.h>
Main ()
{
While (1)
Print f (“%d \n”, get p id ());
}
Terminal - 2
Vi imp – kill .c
# include <stdio.h>
Main ( int argc, char ** arg V)
{
Int p id , sig num;
P id = a to I (arg V [1]);
Sig num = a to I (arg V [2]);
If ((Kill (p id, signum)) < 0)
Print p error (“Kill”);
Else
Print f (“Signal % d is sent to % d by % d \n”, sig num , p id, get p id ());
}
Output :
In terminal -1 run the program . it executes infinitely. EX:- 27247 27247
In terminal -2 cc - 0 V kill imp _kill .c
./vkill 27247 19 this interrupt number can be any of valid.
Output signal 19 is sent to 27247 by 27297 interrupts 27297 is process id of present process that is in terminal 2
Raise
-send a signal to the current process
#include <sign at. h>
Int raise (int sig );
-the raise () function sends a signal to the current process . it is equivalent to ,
Kill (get p id (), sig);
-Return value : ‘0’ on success , non – zero for failure
Alarm (2)
-set an alarm clock for delivery of a signal
# include <unistd . h >
Unsigned int alarm (unsigned int seconds );
Alarm () – arranges for a SIG ALARM signal to be delivered to the process in seconds second’s.
If seconds is zero , no new alarm () is scheduled.
In any event any previously set alarm () is cancelled.
Returns
Returns number of seconds remaining until any previously scheduled alarm was due to delivered , or zero if the was no previously scheduled alarm.
Sleep () may be implemented using SIGALRM; mixing calls to alarm () and sleep () is a bad idea.