Wednesday, January 15, 2014

Advance : Signals

Hello... Friends, Today i am going to discus  about the signals in python programming what are the signals ? how we can use them in python etc.
Signals are an operating system feature that provide a means of notifying your program of an event , and having it handled asynchronously. they can be generated by the system itself, or send from one process to another. signals intrupt the regular flow of your program,it is possible to that some operations (I/O) may produce error if a signal is received in the middle.

Signals are identified by integers and are defined in the operating system C headers . python exposes the signals appropriate for the platform as  symbols in the signal module.

to get the list of signals in your system (OS) run the command 

# man 7 signal 

here you can get a list of all signals in Linux OS 

in the image above we can see a list of signals along with there values & comments so now we want how to use these signals in the python.

here i'll tell you how we can receive signal using python, sending signal, alarm signal, ignoring signals.
so start with receive signal :



so here you can see i have a little code snippet to generate the signals in my os, here signals are received by establishing a callback function,signal handler, the arguments to your signal handler are the signal number and the stak fram from the point in your program that was inerrupted by the signal. Let's me  run this script
  

in the program execution we can see when a signal comes in, the sleep call is interrupted and the signal handler which is receving_signal() prints the signal number and when the signal handler returns, the loop continues...
in second terminal you can see i am sending signal to kill the program & our python program terminated.

sending signal :
here you can see i am sending a alarm signal to tell that yes i receive your signal by using alarm method or alarm signal in the script so let's run this script & see what happened with this script :')


as you can see i am calling signal.SIGALRM signal to notify us that what is going on in the script
here i am printing the time before & after the alarm setup here we can see i set the time to sleep 3 sec to give us a alart


well our signals are received properly

ignoring signals:

now last part on signal let's makes some fun in it what if we can't able to kill the process the process is running on & we are set the signal that you are unable to kill the process


here in this program i am just calling SIGINT signal as it is defined in the signal manual we can see that this function perform.
Let's try to run this script & see what happened with this program .... here i am making this script a little funny that i am sending virus in your system & after that it done & when user try to stop this script using ctrl+c [ ^C ]  you can see what happened with the user.


when user try to stop it using ctrl+c then he is unable to stop the processing & give a funny message as you wish :')


Exercise :
1. import the os module in sending script & try to kill the signal with the help of python script in the same program as sending signal (Hint : use os.kill() method in this script.
2. try to use other signals from the signal manual page & make fun with signals.