signals/nonatomic_uint64.cThis is signals/nonatomic_uint64.c, an example to accompany the book, The Linux Programming Interface. This file is not printed in the book; it is a supplementary file for Chapter 21. The source code file is copyright 2024, 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.
|
/* nonatomic_uint64.c On x86-32, stores and fetches of a 64-bit integer are not atomic. This can result in "memory tearing" on fetches and stores, where the memory access requires two bus transactions. Consequently, a fetch of an integer that is being modified on another CPU might receive half of the old value and half of the new value. Thus, for example, a 64-bit integer can not be safely used as the flag variable set by a signal handler and checked by the main program. This program demonstrates the behavior. The signal handler inverts a 64-bit integer whose initial value is zero, so that the value constantly switches between all bits zero and all bits one. The main program repeatedly fetches a copy of that integer and prints out a message whenever the fetched value is not all zeros or all ones. Compile this program with -m32, and run it: $ cc -o nonatomic_uint64 -m32 nonatomic_uint64.c $ ./nonatomic_uint64 Unexpected: ffffffff00000000 (loop 48533) Unexpected: ffffffff00000000 (loop 171488) Unexpected: 00000000ffffffff (loop 272321) Unexpected: 00000000ffffffff (loop 297708) On Ubuntu and Debian, you may need to install the package 'gcc-multilib' in order to build this program as described above. */ #include <inttypes.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <signal.h> #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0) #define SIG SIGUSR1 static volatile uint64_t ip = 0; /* 'volatile' included here just to show that it really doesn't help. */
/* Signal handler inverts bits in '*ip'; this may not be atomic. */ static void handler(int sig) { ip = ~ip; }
int main(int argc, char *argv[]) { /* Set up signal handler. */ struct sigaction sa; sa.sa_handler = handler; sa.sa_flags = 0; sigemptyset(&sa.sa_mask); if (sigaction(SIG, &sa, NULL) == -1) errExit("sigaction"); /* Create child that will blast signals at parent. */ pid_t childPid = fork(); if (childPid == -1) errExit("fork"); if (childPid == 0) { while (1) { if (kill(getppid(), SIG) == -1) errExit("kill() failed in child"); } } /* Parent falls through */ /* Loop, fetching the value of '*ip' (the fetch may not be atomic) and checking whether the result is all bits zero or all bits one. If it is not, print the fetched value and the loop counter. */ for (int long long j = 0; ; j++) { uint64_t loc = ip; if (loc != 0 && loc != ~0) printf("Unexpected: %016" PRIx64 " (loop %lld)\n", loc, j); } 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.