Sending and Receiving Signals

A Unix signal is a tiny message sent to a process. It can’t say much; it’s like a car horn honking: does that honk you hear mean “look out—the bridge collapsed,” “the light has changed—get going,” “stop driving—you’ve got a baby on the roof,” or “hello, world”? Fortunately, Unix signals are easier to interpret than that because there’s a different one for each of these situations.[330] Different signals are identified by a name (such as SIGINT, meaning “interrupt signal”) and a corresponding small integer (in the range from 1 to 16, 1 to 32, or 1 to 63, depending on your Unix flavor). Signals are typically sent when a significant event happens, such as pressing the interrupt character (typically Ctrl-C) on the terminal, which sends a SIGINT to all the processes attached to that terminal.[331] Some signals are sent automatically by the system, but they can come from another process.

You can send signals from your Perl process to another process, but you have to know the target’s process ID number. How to figure that out is a bit complicated,[332] but say you know that you want to send a SIGINT to process 4201. That’s easy enough:

    kill 2, 4201 or die "Cannot signal 4201 with SIGINT: $!";

It’s named “kill” because one of the primary purposes of signals is to stop a process that’s gone on long enough. You can use the string 'INT' in place of the 2 there because signal number 2 is SIGINT. If the process no longer exists,[333] you’ll get a false return value, ...

Get Learning Perl, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.