signals/sig_sender.c

This is signals/sig_sender.c (Listing 20-6, page 412), an example from the book, The Linux Programming Interface.

The source code file is copyright 2024, Michael Kerrisk, and is licensed under the GNU General Public License, version 3.

This page shows the "distribution" or "book" version of the file (why are there two versions?), or the differences between the two versions. You can switch between the views using the tabs below.

In the listing below, the names of Linux system calls and C library functions are hyperlinked to manual pages from the Linux man-pages project, and the names of functions implemented in the book are hyperlinked to the implementations of those functions.

  Cover of The Linux Programming Interface
+/* sig_sender.c
+
+   Usage: sig_sender PID num-sigs sig [sig2]
+
+   Send signals to sig_receiver.c.
+
+   Sends 'num-sigs' signals of type 'sig' to the process with the specified PID.
+   If a fourth command-line argument is supplied, send one instance of that
+   signal, after sending the previous signals.
+*/
 #include <signal.h>
 #include "tlpi_hdr.h"
 
 int
 main(int argc, char *argv[])
 {
     int numSigs, sig, j;
     pid_t pid;
 
     if (argc < 4 || strcmp(argv[1], "--help") == 0)
         usageErr("%s pid num-sigs sig-num [sig-num-2]\n", argv[0]);
 
     pid = getLong(argv[1], 0, "PID");
     numSigs = getInt(argv[2], GN_GT_0, "num-sigs");
     sig = getInt(argv[3], 0, "sig-num");
 
     /* Send signals to receiver */
 
     printf("%s: sending signal %d to process %ld %d times\n",
             argv[0], sig, (long) pid, numSigs);
 
     for (j = 0; j < numSigs; j++)
         if (kill(pid, sig) == -1)
             errExit("kill");
 
     /* If a fourth command-line argument was specified, send that signal */
 
     if (argc > 4)
         if (kill(pid, getInt(argv[4], 0, "sig-num-2")) == -1)
             errExit("kill");
 
     printf("%s: exiting\n", argv[0]);
     exit(EXIT_SUCCESS);
 }

Note that, in most cases, the programs rendered in these web pages are not free standing: you'll typically also need a few other source files (mostly in the lib/ subdirectory) as well. Generally, it's easier to just download the entire source tarball and build the programs with make(1). By hovering your mouse over the various hyperlinked include files and function calls above, you can see which other source files this file depends on.

Valid XHTML 1.1