Marwan Marwan - 2 months ago 5
C Question

What is the difference between the various unistd.h under /usr/include in Linux?

Under the /usr/include directory in Linux i entered the command:
find -type f -name unistd.h which gave the following output:

./unistd.h
./linux/unistd.h
./asm-generic/unistd.h
./bits/unistd.h
./asm/unistd.h
./sys/unistd.h

my question is, what is the purpose of each unistd.h, since there is only one definiton of that file in the single unix specification ?

Thanks in advance.

Answer

linux/unistd.h actually points to asm/unistd.h, which in turn points to either asm/unistd_32.h or asm/unistd_64.h, which is where system call numbers are defined and presented to user space depending on the system's architecture. These come from the kernel.

bits/unistd.h is a collection of macros that augment unistd.h (mostly stuff to help prevent buffer overflows), which is conditionally included via:

/* Define some macros helping to catch buffer overflows.  */
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
# include <bits/unistd.h>
#endif

In essence, the only POSIX required header is in fact, just unistd.h, the rest are either extensions, or definitions from the kernel. So, just including unistd.h is all you have to worry about doing, everything you need will be pulled in depending on your architecture and whatever build options you've selected.

Comments