2

Is there a way to create a custom signal in linux. Like lets say when "echo crazy" gets executed in a file? And whenever that signal gets executed, I would stop the process using that signal... Also I want to use the perl Enbugger with a new signal also. Is there a way to do that? I found this from http://metacpan.org/pod/Enbugger

 use Enbugger::OnError qw( USR1 USR2 INT HUP );

Where USR1 and USR2 are user defined signals.

szabgab
  • 6,202
  • 11
  • 50
  • 64
ban
  • 605
  • 2
  • 9
  • 22
  • 1
    Look here http://stackoverflow.com/questions/5741604/is-there-any-way-to-create-a-user-defined-signal-in-linux and here http://stackoverflow.com/questions/5350865/why-do-many-unix-programs-use-signals-like-usr1 – Anshul Aug 07 '13 at 15:30
  • @Anshul Hello I actually don't know what a signal might consist of, will echo crazy qualify as a signal??? – ban Aug 07 '13 at 15:35
  • No signals are just plain integers. You would need to map events to signal. You probably dont even need USR1 or USR2. If you can givev more examples on how the situation looks like i can help. (what would enerate/ where 'echo crazy' would come from and what will be monitoring it ? – Anshul Aug 07 '13 at 15:45
  • I have some test files which run in a random order. In these files is a special command. I just want to run a file everytime that command is executed. I thought if I attach a signal to the command, and then catch the signal, I might achieve somthing – ban Aug 07 '13 at 16:07
  • @Anshul and can you also guide me to some resources for mapping events to signal and then using that signal in another file somehow. I mean any resource would be good – ban Aug 07 '13 at 16:08
  • Hmm. Let me understand it more clearly. What do you have - multiple independent scripts OR multiple processes of same script running in parallel? And what should happen when special_command in say script1/process1 get's executed. Which of the 'running' process/script should be affected ? – Anshul Aug 07 '13 at 16:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35014/discussion-between-anshul-and-ban) – Anshul Aug 07 '13 at 16:13
  • [`perldoc perlipc`](http://search.cpan.org/perldoc?perlipc) has some good introductory material and sample code about signals. – mob Aug 07 '13 at 16:20

2 Answers2

7

Signal handling in Perl taps into the signal functionality of your operating system, which generally has a pre-defined and fixed set of signals. But many systems do allocate "extra" signals that don't have any particular purpose, which may be used by user processes. For example, my Red Hat system offers these signals:

$ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

You can see signals 34 through 64 are all expressed in terms of SIGRTMIN and SIGRTMAX. These are signals that a user can commandeer without fear of interfering with anything the system usually wants to do. In Perl, you would assign signal handlers to these with the mnemonics like NUM35 and send a signal with either the mnemonic (kill 'NUM37', $pid) or with just the signal number (kill 37, $pid).

mob
  • 117,087
  • 18
  • 149
  • 283
  • Can you also tell me how to interpret a command being run as a signal. I mean how to make the event of a command be seen as a signal to other files? – ban Aug 07 '13 at 16:10
  • 2
    Commands are not run as signals. Commands can send signals, and in Perl this is done with the kill builtin. Commands can receive signals, and in Perl you can manage (for most signals) what happens when you receive a signal with the %SIG variable. Signals are **NOT** sent to files. Signals are sent to processes (or to threads, which is kind of another can of worms). – mob Aug 07 '13 at 17:17
0

A signal is just like a interrupt, when it is generated by user level, a call is made to the kernel of the OS and it will action accordingly. To create a signal, here I just show you an example

#include<stdio.h>
#include<signal.h>
#include<sys/types.h>   
void sig_handler1(int num)
{
  printf("You are here becoz of signal:%d\n",num);  
  signal(SIGQUIT,SIG_DFL);
}
void sig_handler(int num)
{
   printf("\nHi! You are here becz of signal:%d\n",num);
}
int main() 
{
   signal(SIGINT,sig_handler1);
   signal(SIGQUIT,sig_handler);
   while(1)
   {
       printf("Hello\n");
       sleep(2);
   }

}

after running this code if you will press Ctrl+C then a message will show - "You are here becoz of signal:2" instead of quiting a process as we have changed a signal according to our action. As, Ctrl+C is a maskable signal.

To know more anbout signals and types of signals with examples please follow the link :

http://www.firmcodes.com/signals-in-linux/