procpri/demo_sched_fifo.c

This is procpri/demo_sched_fifo.c, an example to accompany the book, The Linux Programming Interface.

This file is not printed in the book; it is the solution to Exercise 35-3 (page 752).

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

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.

 

Download procpri/demo_sched_fifo.c

  Cover of The Linux Programming Interface

Function list (Bold in this list means a function is not static)

/* demo_sched_fifo.c

   This program demonstrates the use of realtime scheduling policies. It creates
   two processes, each running under the SCHED_FIFO scheduling policy. Each
   process executes a function that prints a message every quarter of a second
   of CPU time. After each second of consumed CPU time, the function and calls
   sched_yield() to yield the CPU to the other process. Once a process has
   consumed 3 seconds of CPU time, the function terminates.

   This program must be run as superuser, or (on Linux 2.6.12 and later)
   with a suitable RLIMIT_RTPRIO resource limit.
*/
#define _GNU_SOURCE
#include <sched.h>
#include <sys/resource.h>
#include <sys/times.h>
#include "tlpi_hdr.h"

#define CSEC_STEP 25            /* CPU centiseconds between messages */
static void
useCPU(char *msg)
{
    int prevStep = 0;
    int prevSec = 0;
    for (;;) {
        struct tms tms;
        if (times(&tms) == -1)
            errExit("times");
        int cpuCentisecs = (tms.tms_utime + tms.tms_stime) * 100 /
                           sysconf(_SC_CLK_TCK);

        if (cpuCentisecs >= prevStep + CSEC_STEP) {
            prevStep += CSEC_STEP;
            printf("%s (PID %ld) cpu=%0.2f\n", msg, (long) getpid(),
                    cpuCentisecs / 100.0);
        }

        if (cpuCentisecs > 300)         /* Terminate after 3 seconds */
            break;

        if (cpuCentisecs >= prevSec + 100) {    /* Yield once/second */
            prevSec = cpuCentisecs;
            sched_yield();
        }
    }
}
int
main(int argc, char *argv[])
{
    setbuf(stdout, NULL);               /* Disable buffering of stdout */

    /* Confine all processes to a single CPU, so that the processes
       won't run in parallel on multi-CPU systems. */

    cpu_set_t set;
    CPU_ZERO(&set);
    CPU_SET(1, &set);

    if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
        errExit("sched_setaffinity");

    /* Establish a CPU time limit. This demonstrates how we can
       ensure that a runaway realtime process is terminated if we
       make a programming error. The resource limit is inherited
       by the child created using fork().

       An alternative technique would be to make an alarm() call in each
       process (since interval timers are not inherited across fork()). */

    struct rlimit rlim;
    rlim.rlim_cur = rlim.rlim_max = 50;
    if (setrlimit(RLIMIT_CPU, &rlim) == -1)
        errExit("setrlimit");

    /* Run the two processes in the lowest SCHED_FIFO priority */

    struct sched_param sp;
    sp.sched_priority = sched_get_priority_min(SCHED_FIFO);
    if (sp.sched_priority == -1)
        errExit("sched_get_priority_min");

    if (sched_setscheduler(0, SCHED_FIFO, &sp) == -1)
        errExit("sched_setscheduler");

    switch (fork()) {
    case -1:
        errExit("fork");

    case 0:
        useCPU("child ");
        exit(EXIT_SUCCESS);

    default:
        useCPU("parent");
        exit(EXIT_SUCCESS);
    }
}

 

Download procpri/demo_sched_fifo.c

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