Learn about your system configuration using getconf

I recently learnt about a simple program named getconf that can be used to query and view values of various system configuration variables. It is a POSIX program that ships with the GNU C Library (glibc) and so should be available on any Linux system. It can be used to query system variables and path variables from the shell.

  • To view all the variables and values that this program handles:
$ getconf -a
  • To query a specific variable:
$ getconf INT_MAX
  • Some of the getconf variables that I find interesting:
  • In case you are curious, getconf just calls the sysconf function provided by the GNU C Library. This can be seen in the posix/getconf.c source file in glibc.

  • To get the cache details of an Intel 64-bit x86 CPU, sysconf uses the cpuid assembly instruction. This can be seen in the sysdeps/x86_64/cacheinfo.c source file in glibc.

Tried with: GLibC 2.19 and Ubuntu 14.04

The strange case of the random number generator that was not random


I was using the simple rand API provided by C for generating random numbers. I needed to generate m sequences of n numbers each. To my surprise, I found that two of these sequences were exactly the same!


I was using srand to seed the generation of each sequence. In the release version of my code, I was seeding it using time, which is a common practice:

srand((unsigned int) time(nullptr));

When I needed to debug the code, I needed the sequences to be reproducible, so I seeded them as:

// m sequences of n random numbers
for (int i = 0; i < m; ++i)
    for (int j = 0; j < n; ++j)
        // Use rand() here

On closer observation, the sequences produced by srand(0) and srand(1) were exactly same!

This was surprising behavior. The srand man page had no information about this. It only warned that if no seed was provided, 1 would be assumed as seed:

If no seed value is provided, the rand() function is automatically seeded with a value of 1.

The problem was found by looking in the source code of GNU Libc. In the implementation of srand in the file stdlib/random_r.c lies this gem:

/* We must make sure the seed is not 0.  Take arbitrarily 1 in this case.  */
if (seed == 0)
  seed = 1;

You have got to love open source! So, seed of 0 is internally reset to 1 in the glibc implementation of srand!

So, beware of the seed you pass to srand. It might be wise to make sure that neither 0 or 1 is used as seed on Linux.

Tried with: GLibc 2.19.1 and Ubuntu 14.04