Notes on advanced programming in UNIX environment-is errno thread safe?

Source: Internet
Author: User

When a Unix function fails, a negative number is often returned, and the integer errno is usually set to a value containing additional information. For example, if the open function is successful, a non-negative file descriptor is returned, if an error occurs,-1 is returned. When an open error occurs, there are about 15 different errno values (file does not exist, permission issues, and so on ). Some functions do not return negative numbers, but use another convention. For example, if you return a majority of functions pointing to object pointers, a null pointer is returned when an error occurs.

The file <errno. h> defines the symbol errno and various constants that can be assigned to it. These constants start with the character E.

POSIX and Iso c define errno as such a symbol, which is extended into a modifiable left INTEGER (lvalue), which can be an integer containing an error number, or a function that returns an error number pointer. The previous definition is:

Extern int errno

However, in a multi-threaded environment, multiple threads share the process address space. Each thread has its own local errno to prevent one thread from interfering with another thread. For example, Linux supports multi-threaded access to errno, which is defined:

Extern int * _ errno_location (void );

# Define errno (* _ errno_location ())

Therefore, in Linux, errno is thread-safe.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.