pgsjc/job_mon.c

This is pgsjc/job_mon.c (Listing 34-5, page 719), an example from the book, The Linux Programming Interface.

The source code file is copyright 2022, 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
+/* job_mon.c
+
+   This program is useful for:
+
+      - demonstrating the order in which the shell creates the processes in a
+        pipeline and how it assigns a process group to these processes, and
+
+      - monitoring some of the job control signals sent to a process (group).
+
+   The program displays its PID, parent PID and process group.
+
+   Try running a pipeline consisting of a series of these commands:
+
+        job_mon | job_mon | job_mon
+
+   This will demonstrate the assignment of process groups and PIDs to
+   each process in the pipeline.
+
+   Try running this pipeline under different shells and also in the background
+   (pipeline &) to see the effect this has.
+
+   You can also try typing control-C (^C) to demonstrate that this is
+   interpreted by the terminal driver as meaning "send a signal to all the jobs
+   in the foreground process group".
+*/
 #define _GNU_SOURCE     /* Get declaration of strsignal() from <string.h> */
 #include <string.h>
 #include <signal.h>
 #include <fcntl.h>
 #include "tlpi_hdr.h"
 
 static int cmdNum;      /* Our position in pipeline */
 
 static void             /* Handler for various signals */
 handler(int sig)
 {
     /* UNSAFE: This handler uses non-async-signal-safe functions
        (fprintf(), strsignal(); see Section 21.1.2) */
 
     if (getpid() == getpgrp())          /* If process group leader */
         fprintf(stderr, "Terminal FG process group: %ld\n",
                 (long) tcgetpgrp(STDERR_FILENO));
     fprintf(stderr, "Process %ld (%d) received signal %d (%s)\n",
                 (long) getpid(), cmdNum, sig, strsignal(sig));
 
     /* If we catch SIGTSTP, it won't actually stop us. Therefore we
        raise SIGSTOP so we actually get stopped. */
 
     if (sig == SIGTSTP)
         raise(SIGSTOP);
 }
 
 int
 main(int argc, char *argv[])
 {
     struct sigaction sa;
 
     sigemptyset(&sa.sa_mask);
     sa.sa_flags = SA_RESTART;
     sa.sa_handler = handler;
     if (sigaction(SIGINT, &sa, NULL) == -1)
         errExit("sigaction");
     if (sigaction(SIGTSTP, &sa, NULL) == -1)
         errExit("sigaction");
     if (sigaction(SIGCONT, &sa, NULL) == -1)
         errExit("sigaction");
 
     /* If stdin is a terminal, this is the first process in pipeline:
        print a heading and initialize message to be sent down pipe */
 
     if (isatty(STDIN_FILENO)) {
         fprintf(stderr, "Terminal FG process group: %ld\n",
                 (long) tcgetpgrp(STDIN_FILENO));
         fprintf(stderr, "Command   PID  PPID  PGRP   SID\n");
         cmdNum = 0;
 
     } else {            /* Not first in pipeline, so read message from pipe */
         if (read(STDIN_FILENO, &cmdNum, sizeof(cmdNum)) <= 0)
             fatal("read got EOF or error");
     }
 
     cmdNum++;
     fprintf(stderr, "%4d    %5ld %5ld %5ld %5ld\n", cmdNum,
             (long) getpid(), (long) getppid(),
             (long) getpgrp(), (long) getsid(0));
 
     /* If not the last process, pass a message to the next process */
 
     if (!isatty(STDOUT_FILENO))   /* If not tty, then should be pipe */
         if (write(STDOUT_FILENO, &cmdNum, sizeof(cmdNum)) == -1)
             errMsg("write");
 
     for (;;)            /* Wait for signals */
         pause();
 }

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