In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >
Share
Shulou(Shulou.com)06/01 Report--
Define
(1)。 Thread-safe functions: generally speaking, a function is called thread-safe, and when it is called repeatedly by multiple concurrent threads, it always produces correct results.
(2)。 Reentrant: when the program executes to a function foo (), it receives a signal, then pauses the currently executing function and goes to the signal processing function, and during the execution of this signal processing function, it just enters the function foo () that has just been executed, so the so-called reentry occurs. At this point, if foo () runs correctly, and if the previously paused foo () runs correctly after the processing is complete, it is reentrant.
(3)。 Expand:
1)。 If a global or static variable is used in a function, it is neither thread safe nor reentrant
2)。 If we improve it and lock it by using mutexes or semaphores when accessing global or static variables, we can make it thread-safe, but it is still non-reentrant at this time, because locking is usually for different thread access, and there may be problems for the same thread.
3)。 If the global or static variables in the function are removed and changed to other forms such as function parameters, it is possible to make the function both thread-safe and reentrant.
contact
A reentrant function is a true subset of thread-safe functions. The reentrant function is a thread-safe function, but in turn, a thread-safe function is not necessarily a reentrant function.
Difference
(1) to solve the problem:
a. The problem with reentrant functions is that static or global data is not used inside the function, static or global data is not returned, and non-reentrant functions are not called.
b. The problem with thread-safe functions is that access to resources conflicts when multiple threads call the function.
(2) ensure measures
a. The measure to ensure thread safety is that thread-safe functions do not use shared data (global, static, or heap) or implement synchronization mechanisms to protect shared data.
b. Measures to ensure reentrant: do not share data and do not call non-reentrant functions.
(1) do not use static variables and global variables, insist on using only local variables
(2) if you must access global variables, use mutex semaphores to protect global variables.
(3) get to know which system calls are reentrant, and use secure system calls in multitasking programs.
(4) do not call any other non-reentrant functions
(5) use stack malloc/new carefully.
(3) Transformation
If a function uses static variables, it can be converted to a thread-safe function after locking, but it may not be reentrant, such as strtok. Strtok is neither reentrant nor thread-safe. Locked strtok is not reentrant, but thread-safe. Strtok_r is both reentrant and thread-safe.
(4) when the signal processing function is called
a. The difference between reentrant and thread safety is reflected in whether it can be called in the signal processing function. The reentrant function can be safely called in the signal processing function, so it is also an asynchronous signal safety function. The thread safety function does not guarantee that it can be safely called in the signal processing function. If a non-reentrant function is guaranteed not to be interrupted by the signal by setting the signal blocking set, then it is also an asynchronous signal safety function.
It is worth mentioning that the Syste m Interface of POSIX 1003.1 is thread-safe by default, but not asynchronous signal-safe. The need for asynchronous signal security is clearly stated, such as fork () and signal ().
b. A non-reentrant function is usually (though not in all cases) judged by its external interface and usage. For example: strtok () is non-reentrant because it internally stores the string split by the tag; the ctime () function is also non-reentrant and returns a pointer to static data that is overwritten and rewritten in each call.
c. Thread safety is only related to the internal implementation of the function, not the external interface of the function. In C, local variables are assigned on the stack. Therefore, any function that does not use static data or other shared resources is thread-safe. A thread-safe function implements multi-thread secure access to shared data by locking.
4. The reasons for thread unsafe and non-reentrant of function
(1) the root cause of any thread unsafety problem is "shared data". Therefore, functions that do not use any shared data (that is, reentrant functions) must be thread-safe.
(2) the reason for the non-reentrant function is:
a. They are known to use static data structures
b. They call malloc and free.
Because malloc usually maintains a linked table for the allocated storage, the process may be modifying the linked table when the insert executes the signal processing function.
c. They are standard IO functions.
Because many implementations of standard IO libraries use global data structures
(3) Thread safety functions are not necessarily reentrant functions, because even if a thread has shared data, the result can be made correct when the thread is called concurrently-correctness is guaranteed by synchronous operation.
Shared data can be:
Function to put the returned result in a common location
Pointer or reference variables shared between threads passed in by the caller
Shared static variables that are already used inside the function
4. Supplement
(1) the common non-reentrant functions are:
Printf-references the global variable stdout
Malloc-Global memory allocation table
Free-Global memory allocation table
(2) examples of non-reentrant solvers (printf)
For example, the program is calling printf output, but when calling printf, a signal appears, and the corresponding signal processing function also has a printf statement, which will cause the output of the two printf to be mixed together.
If the printf is locked, the same situation will lead to a deadlock. In this case, the method is generally used to block a certain signal in a specific area.
The method of blocking the signal:
1 > signal (SIGPIPE, SIG_IGN); / / ignore some signals
2 > sigprocmask ()
Sigprocmask is only defined for a single thread
3 > pthread_sigmask ()
Pthread_sigmasks can be used in multithreading
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.