How to make errno multithreaded/process secure

Source: Internet
Author: User

In Linux or UNIX environments, errno is a very important part. When there is a problem with the called function, we can determine the cause of the error by errno value, which involves a problem, that is, how to ensure that the errno in multithreading or process security. We want each thread or process to have its own independent and unique errno in multiple threads or processes, so that there is no competitive condition to emerge. In general, the compiler will automatically guarantee the security of errno, but in order to properly period, we want to write makefile when the _libc_reentrant macro definition, such as we found in the inspection <bits/errno.h> file the following definition:
# 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/*!__assembler__ *
#endif/* _errno_h * *
That is, errno is multithreaded/process-safe when there is no definition of __libc or definition of _libc_reentrant.
Generally speaking, __assembler__, _LIBC, and _libc_reentrant are not defined by the compiler, but if we define _libc_reentrant once it doesn't hurt that. <!--[if!vml]--><!--[endif]-->
To check whether your compiler defines the above variables, you might want to use one of the following simple programs.
#include <stdio.h>
#include <errno.h>

int main (void)
{
#ifndef __assembler__
printf ("Undefine __assembler__/n");
#else
printf ("define __assembler__/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;
}
Hopefully, when the reader is porting, read the relevant UNIX version of <bits/errno.h> files to determine what macros should be defined. There may be some small differences in the different versions of UNIX.
<!--[if!supportlinebreaknewline]-->
<!--[endif]-->



I want to add that, to be thread-safe, we should define _reentrant macros. Please refer to features.h file

in Windows, we need to define the _MT oncoming error with multi-threaded security!

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.