How to secure errno multithreading/process

Source: Internet
Author: User

In Linux or UNIX environments, errno is a very important part. When the called function encounters a problem, we can use the errno value to determine the cause of the Error. This will involve a problem, that is, how to ensure the security of errno in multithreading or processes? We hope that in a multi-thread or process, each thread or process has its own independent and unique errno, so as to ensure that there will be no competition conditions. Generally, the compiler automatically guarantees the security of errno. However, we want to define the _ libc_reentrant Macro when writing makefile to ensure the security of errno. For example, we are checking <bits/errno. h> The following definitions are found in the file:
# Ifndef _ assembler __
/* Function to get address of global 'errno' variable .*/
Extern int * _ errno_location (void) _ Throw _ attribute _ (_ const __));

# If! Defined _ libc | defined _ libc_reentrant
/* When using threads, errno is a per-thread value .*/
# Define errno (* _ errno_location ())
# Endif
# Endif /*! _ Cycler __*/
# Endif/* _ errno_h */
That is to say, when _ libc or _ libc_reentrant is not defined, errno is multithread/process secure.
Generally, _ javaser __, _ libc and _ libc_reentrant are not defined by the compiler. But what if we define _ libc_reentrant once?
To check whether your compiler defines the preceding variables, use the following simple program.
# Include <stdio. h>
# Include <errno. h>

Int main (void)
{
# Ifndef _ assembler __
Printf ("undefine _ er Er _/N ");
# Else
Printf ("DEFINE _ explorer _/N ");
# Endif

# Ifndef _ libc
Printf ("undefine _ libc/N ");
# Else
Printf ("DEFINE _ libc/N ");
# Endif

# Ifndef _ libc_reentrant
Printf ("undefine _ libc_reentrant/N ");
# Else
Printf ("DEFINE _ libc_reentrant/N ");
# Endif

Return 0;
}
We hope that you can read the <bits/errno. h> file of the relevant unix version to determine what macros should be defined. There may be some minor differences between different UNIX versions!

 

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.