Friday, June 03, 2005

Examples of interrupt handling in UNIX:Trapping trap.....tap..tap..

Better learn bout trapping at first. Shell procedures may use the trap command to catch or ignore Unix operating system signals. The form of the trap command is:
trap 'command-list' signal-list
Several traps may be in effect at the same time. If multiple signals are received simultaneously, they are serviced in ascending order.wat are the various signals available in UNIX.u can get a list by using the command kill –l


Sample run from system:
$ kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS
SEGV SYS PIPE ALRM TERM USR1 USR2 CLD PWR WINCH
URG POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU
XFSZ WAITING LWP FREEZE THAW CANCEL LOST RTMIN RTMIN+1 RTMIN+2
RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX


These are the various signals. And each process is suppressed to follow this signal dada. Some examples like:

HUP-the signal to hangup(also can be numbered as 1)
We often use kill –9 .9 is the number of the signal. And u just count 9 in the upper system-run-list, u`ll get it as kill.

We better give numbered it as the number will be used frequently hereafter.
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL
5) SIGTRAP 6) SIGIOT 7) SIGEMT 8) SIGFPE
9) SIGKILL 10) SIGBUS 11) SIGSEGV 12) SIGSYS
13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGUSR1
17) SIGUSR2 18) SIGCHLD 19) SIGPWR 20) SIGWINCH
21) SIGURG 22) SIGIO 23) SIGSTOP 24) SIGTSTP
25) SIGCONT 26) SIGTTIN 27) SIGTTOU 28) SIGVTALRM
29) SIGPROF


There are some most common signals to be caught.they are as follows:
The following are the signals that are usually caught with the trap command.
0 shell exit (for any reason, including end of file EOF).
1 hangup.
2 interrupt (^C).
3 quit (^\ ; causes program to produce a core dump).
9 kill (cannot be caught or ignored).
15 terminate; default signal generated by kill.


Now take a break and be cool. It’s really interesting to make a surgery on trap. The command list is placed between single quotes, as the command line is scanned twice, once when the shell first encounters the trap command and again when it is being executed.
trap 'command-list' signal-list

The single quotes inhibit immediate command and variable substitution but are stripped off after the first scan, so that the commands are processed when the command is actually executed.
If command-list is not specified, then the action taken on receipt of any signal in the signal-list is reset to the default system action.
If command-list is an explicitly quoted null command (' ' or " "), then the signals in signal-list are ignored by the shell.
The command-list is treated like a subroutine call. The commands in the list are executed when the signal is trapped and control is then returned to the place at which it was interrupted.

Now lets a cool example of trap.Create a shell script named trap as follows:
======================vi trap =====================
trap 'echo "You woke me up!"; exit' 2
echo "Zzzzz..."
sleep 10
echo "Why didn't you wake me up?"

========================================================

The sample run:

$ sh trap
Zzzzz...
^CYou woke me up! ##CNTRL C is pressed
$
$
$ sh trap
Zzzzz...
Why didn't you wake me up? ##no interrupt or CNTRL C…after 10 secs

$

Here wat happens is that at the very first scanning the bactricks are stripped off from that trap line. And later when the line is executing if any interrupt occurs( i.e, CNTRL C),it shows its red eyes “How dare u….!!!You woke me up!”. And even if u do nothing, it`ll later blame u “Why didn't you wake me up?”..


Umm…wat a heck is it?

Type that script.Make it executable and then finally execute it.
======================== vi sorry ==========
while true;
do
trap "echo 'You hit control-C!'" INT
sleep 60
done
============================================


Sample run:

$ sh sorry
^CYou hit control-C!
^CYou hit control-C!


Hahhaha…. Got stuck na…. Sorry…Sorry…That’s why I choosed the file name as sorry…U shud use ur brain na…!!!Btw…umm..if possible use another system. Enter in the same userid/passwd and then kill it. Or if possible then close that window(if u r basically from a WINDOW).Well. Here two notable things are there in the program. Firstly, I used a double quote there and secondly, we use INT in place of any numbers. Using double quote means in the command lists the commands will be substituted in the very first run. And INT is nothing. Check that list which we have generated using kill –l. In fact if we would have used the number 2 in place of that INT, would again stuck u….Hahahha..poor fellow..:P

Hey,here is an improved version of that sorry.Taste it…Well,I wont take any responsibility this time:P.
======================= vi ExtremelySorry ===============
trap "echo 'You hit control-C!'" INT
trap "echo 'You tried to kill me!'" TERM

while true; do
sleep 60
done
==========================================================

Kkkk….Its enough.we`ll talk about more boobytraps later.Byeeeeeeee……..

Labels:

0 Comments:

Post a Comment

<< Home