In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article is about the sample analysis of semaphores in Linux. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The concept of semaphore is put forward in Linux system. This is a relatively eclectic approach, which can not only ensure the synchronization between threads, but also improve the concurrency of threads. Note that the semaphores mentioned here have nothing to do with the signals we have learned, just as Java has nothing to do with JavaScript.
Main application functions:
Sem_init function
Sem_destroy function
Sem_wait function
Sem_trywait function
Sem_timedwait function
Sem_post function
The return values of the above six functions are: 0 for success,-1 for failure, and errno is set.
Careful readers may notice that they do not have a pthread prefix, indicating that semaphores can be used not only between threads but also between processes.
The sem_t data type is still a structure in nature. But similar to the file descriptor, we can simply treat it as an integer during application and ignore the implementation details.
Usage: sem_t sem; we agreed that the semaphore sem should not be less than 0. When using, be careful to include header files.
Similar to mutexes, semaphores also have operations similar to locking and unlocking. Locking uses the sem_wait function, and unlocking uses the sem_post function. These two functions have the following characteristics:
When calling sem_post, if the semaphore is greater than 0, the semaphore is reduced by one
When the semaphore is equal to 0, calling sem_post will cause thread blocking.
When sem_post is called, the semaphore is incremented by one and the thread blocked on the semaphore is awakened.
Because the implementation of sem_t is hidden from the user, the two operations mentioned above can only be implemented through functions, not directly using the + +,-- symbols.
# # sem_init function
Function prototype: int sem_init (sem_t * sem, int pshared, unsigned int value)
Function: initialize a semaphore
Parameter description: sem: semaphore; pshared: 0, semaphore is used for inter-thread synchronization; non-0 (usually 1) is used for inter-process synchronization; value: specify the initial value of semaphore, and the initial value of semaphore determines the number of threads allowed to occupy semaphores at the same time.
# # sem_destroy function
Function prototype: int sem_destroy (sem_t * sem)
Function: destroy a semaphore
# # sem_wait function
Function prototype: int sem_wait (sem_t * sem)
Function function: add one to the semaphore value
# # sem_post function
Function prototype: int sem_post (sem_t * sem)
Function function: subtract one from the semaphore value
# # sem_trywait function
Function prototype: int sem_trywait (sem_t * sem)
Function: try to lock the semaphore, similar to pthread_mutex_trylock
# # sem_timedwait function
Function prototype: int sem_timedwait (sem_t sem, const struct timespec abs_timeout)
Function: try to lock the semaphore for a limited time
Parameter description: sem: semaphore; abs_timeout: like pthread_cond_timedwait, absolute time is used.
The usage is as follows (for example, the timeout is set to 1 second):
Time_t cur = time (NULL)
Gets the current time. Struct timespec t
Define the timespec structure volume variable t t.tv_sec = cur+1
Timing 1 second t.tv_nsec = t.tv_sec + 100
Sem_timedwait (& sem, & t)
Pass parameters
Producer-consumer semaphore model:
/ * Semaphore implements producer-consumer problem * / # include # define NUM 5 int queue [NUM]; / / Global array implements ring queue sem_t blank_number, product_number / / space subsemaphore, product semaphore void * producer (void * arg) {int I = 0; while (1) {sem_wait (& blank_number); / / the producer sets the number of spaces [I] = rand ()% 1000 + 1 / / produce a product printf ("- Produce---%d\ n", queue [I]); sem_post (& product_number); / / make the number of products + + I = (item1)% NUM; / / realize circular sleep (rand ()% 3) with the help of subscript }} void * consumer (void * arg) {int I = 0; while (1) {sem_wait (& product_number); / / consumers set the number of products--, if 0, block and wait for printf ("- Consume---%d\ n", queue [I]); queue [I] = 0 / / consume a product sem_post (& blank_number); / / after consumption, change the number of spaces + + I = (item1)% NUM; sleep (rand ()% 3);}} int main (int argc, char * argv []) {pthread_t pid, cid Sem_init (& blank_number, 0, NUM); / / initialize the space subsemaphore of 5 sem_init (& product_number, 0,0); / / the number of products is 0 pthread_create (& pid, NULL, producer, NULL); pthread_create (& cid, NULL, consumer, NULL); pthread_join (pid, NULL); pthread_join (cid, NULL) Sem_destroy (& blank_number); sem_destroy (& product_number); return 0;}
Running result:
Thank you for reading! This is the end of this article on "sample analysis of semaphores in Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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
View service name delete service open cmd input command sc delete TlntSvr
© 2024 shulou.com SLNews company. All rights reserved.