13

Is there any way to create a user defined signals in Linux? My signal (signal number) should not match any of the existing signal numbers.

In other words, i want to create my own unique signal, which will be registered and caught by my handler.

Is it possible? If yes, how?

Thanks in advance.

RajSanpui
  • 11,556
  • 32
  • 79
  • 146

5 Answers5

16

SIGRTMIN through SIGRTMAX (these are not normally constants but macros which expand to functions evaluated at runtime) are available for whatever use you like, as are SIGUSR1 and SIGUSR2. The former have additional realtime queueing support; the latter don't. Simply choose one to use.

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
8

You can compile your own kernel with special signals :)

Florian
  • 3,145
  • 1
  • 27
  • 38
5

You can't add or register your own SIGWHATEVER.

See sigset_t, it is fixed size. See valid_signal() beartraps.

adobriyan
  • 2,594
  • 16
  • 8
  • 1
    Where do u get this `valid_signal( )`? I could not find any man page for that. – RajSanpui Apr 21 '11 at 09:39
  • 1
    @kingsmasher1: `valid_signal()` is a function in the Linux kernel that returns true if the signal is < the number of signals. It's used by things like the `kill()` syscall to stop you from doing things like defining your own signals. – JeremyP Apr 21 '11 at 10:18
4

There are the USR1 and USR2 signals designed for user defined purposes.

Didier Trosset
  • 36,376
  • 13
  • 83
  • 122
2

You can use USR1 and USR2 for this kind of thing.

If that's not enough, you can emulate signal like behaviour by having your application listen on a socket which external apps can send messages to.

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • But in that case, we can't register it with a signal handler. Can we? Because i want that when this signal to be used by a timer, and this signal no should be unique, not to clash with any existing signal. So the socket method is not feasible. – RajSanpui Apr 21 '11 at 09:03
  • 1
    If its completely inside your program, you needn't use a signal. You can implement a simple messaging system and use that. Signals are best used for async communication between processes. – Noufal Ibrahim Apr 21 '11 at 09:20
  • Noufal, that can't be used by a POSIX timer, timer_create. – RajSanpui Apr 21 '11 at 09:36
  • I know. You'll have to implement something yourself. – Noufal Ibrahim Apr 21 '11 at 12:11