Orion Lawlor's Short UNIX Examples

These short examples illustrate various simple features of UNIX. Because these are low-level UNIX hacks, they tend not to build on, e.g., Windows.
asctime.c Shows how to use the <time.h> routines localtime and asctime.
asctime_windows.c Shows how to use the <time.h> routines localtime and asctime.
dlopen_gcc.c Shows how to write some code at runtime, compile the code into a shared library, link in the new shared library, and use its new functions. This makes C++ feel more like an interpreted language. And it's fast! It only takes 70ms to compile, link, and run!
dlopen_libc.c Shows how to use dlopen to examine a shared libraries' symbols and functions. In this case, we look up and call the math library's "cos" routine. (Just like Linux' man dlopen).
dlopen_self.c Shows how to use dlsym to examine the running executable's properties. This lets you, e.g., look up one of your own global variables by its string *name*, which is pretty amazing. Note that this only works if the executable is compiled with "-fpic" (so some_symbol shows up in the run) as well as linked with "-ldl" (at least under Linux).
fork.c Shows how to use fork() in a UNIX program.
fork_exec.c Shows how to fork()/exec() a child process, in this case simply a shell. fork/exec is more complicated, but much more flexible and secure than using "system()" to run a process.
large_file_stdio.c Tests if fopen/fwrite/fseek can build a large (>2gb) file. This seems to work properly under 32-bit Linux; but breaks in fseek (EOVERFLOW) on 32-bit Solaris. The fix is to use the fseeko/ftello interfaces (see large_file_stdio_o.c)
large_file_stdio_o.c Tests if fopen/fwrite/*fseeko* can build a large (>2gb) file. This seems to work properly under 32-bit Linux; the "fseeko" makes it work on 32-bit Solaris and other machines.
large_file_unix.c Tests if fopen/fwrite/fseek can build a large (>2gb) file. This seems to work properly under 32-bit Linux and Solaris. Large files seem to be broken if: - You leave off the _FILE_OFFSET_BITS define below AND you are on a 32-bit machine. - Your filesystem doesn't support it (e.g., NFS v2)
longjmp.c Shows how to use setjmp/longjmp() for non-local control.
memory_map.c Walk through the system's RAM, printing out a memory map of mmap-able and unmappable regions.
mmap.c Shows how to use the basic "mmap" syscall to gain access to some private memory. The same technique can be used to map a file into memory. WARNING: This uses MAP_ANONYMOUS, which works in Linux and OS X, but not on some other weirder UNIXes. mmap doesn't exist on Windows. On some machines, you have to open("/dev/zero") to map in some zeros.
nan_signal.c Shows how to enable "signaling NaN" (Not-a-Number), which actually crash as soon as an absurd result is formed. Normally, the floating point hardware silently inserts ordinary "quiet NaNs" into your calculations when you perform an absurd operation. WARNING: There doesn't seem to be any portable way to do this before C99-- for now, this is glibc specific, and only in glibc 2.2 or greater.
profil.c Shows how to use the "profil" syscall.
pthread.c Shows how to create and join pthreads.
readdir.c Shows how to use the POSIX opendir/readdir routines to examine the list of files in a directory.
setitimer.c Shows how to use setitimer to get periodic interrupts.
setitimer_pc.c Shows how to use setitimer to get periodic interrupts, and how to extract the program counter from a signal handler.
setitimer_pthreads.c Shows how to use setitimer to get periodic interrupts for pthreads.
setuid.c Shows how to write a set-uid executable. This program, to do its thing, should be "chmod +s".
signal.c Shows how to install your own signal handler to handle, and hopefully gracefully recover from, crashes.
signal_info.c Shows how to use POSIX siginfo_t to find the faulting address from a signal handler.
signal_info_mprotect.c Shows how to use a signal handler with mprotect to set and reset memory protection. This is useful for weird memory tricks like software distributed shared memory.
socketpair.c Send and receive data over a UNIX socket.
stdout_pipe.c Connect our own stdout to a pipe, for a weird way to read back our own printf's. Note that because ordinary pipes block when they reach 4K of unconsumed input, this program deadlocks if you try to do too much output. To fix this, use a network socket (for a bigger buffer), fork off a reader, or redirect stdout to a file.
Created by Orion Sky Lawlor, olawlor@acm.org, 2008-04-21
Placed in the public domain (download)
Back to
My Home Page