The New errno Value

If you've been a veteran of UNIX C/C++ code for some time, then you've probably noticed some changes in the declaration of the variable errno over the years. Modern UNIX platforms have undergone some changes in order to support threads.

While threads are a welcome addition to the UNIX platform, they have required a number of internal changes to the underlying C libraries and the way in which the errno variable is defined. A thread is a separate flow of instructions within one memory environment (all threads share one memory address space). Consequently, the traditional single global integer value of errno no longer suffices, since function calls in one thread would alter the errno values being referenced in another thread.

In order to support threads without requiring all existing software to be redesigned, a new declaration has been crafted for the errno value (usually a C macro). This new definition defines a separate copy of errno for each thread. Rather than have the programmer declare this variable, it is now done by the provided include file <errno.h> instead. This change in definition should be transparent to most UNIX source code. Note that there were older releases of the GNU libraries under Linux, where the extern int errno declaration was in conflict and required removal to compile successfully. The modern GNU libraries no longer suffer from this problem.

Declaring the New errno Variable

The new errno value is now defined in a platform-dependent manner. This means that you should let the system define it for you by including the file <errno.h>. You should no longer declare it as an external integer variable.

The <errno.h> include file will define errno in a manner that is appropriate for your specific UNIX platform. This also defines the errno macro constants for the error codes.

Using the New errno Variable

Once variable errno is appropriately declared for your platform, you can still use it as you did before. For example

int saved_errno;

saved_errno = errno;            /* Saving errno */
printf("errno = %d
",errno);   /* Inspecting errno */
errno = ENOENT;                 /* Changing errno */
errno = 0;                      /* Clearing errno to zero */

You can obtain value of errno and change its value, just as before its definition changed. The change in the way errno is defined is meant to be transparent to you.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.144.228