Semaphore mechanism solves process synchronization / mutex issues

Semaphore mechanism solves process synchronization / mutex issues
//我知道CSDN有许多非中文母语的人阅读,为解决此问题,给出英文版本,并在图片下方非代码部分给出中文的英文注释。
//I know CSDN is read by many non-native Chinese speakers. In order to solve this problem, an English version is given, and Chinese English comments are given below the picture.
In the book “Computer Operating System”, the role of the two primitives P and V can not be underestimated. The following is a new and helpful book for beginners, and I would like to invite you to come forward with valuable suggestions. Some basic knowledge is at the end of the article, if some friends are unfamiliar, you can refer to it.
// References (one of Mengxin’s favorite reference books) “Computer Operating System” Zhao Yi, Zhang Huimin, Xu Yuebin, Sun Yinghua (Qingdao University), Beijing University of Posts and Telecommunications Press
Complex mutex problem
The Reader-Writers problem is a concurrent programming problem. Data in a computer system is often shared by multiple processes, but some processes may only require reading data; others may require writing data. In terms of shared data, Reader and Writer are two sets of concurrent processes sharing a set of data areas. The following are the requirements:
(1) Multiple readers can perform read operations simultaneously;
(2) Cannot be operated by readers and writers at the same time;
(3) Cannot be operated by multiple writers at the same time.
In this I在这里插入图片描述在这里插入图片描述
Multiple producers and multiple consumers
在这里插入图片描述
The producer-consumer problem is a very classic multi-threaded concurrent collaboration problem. Understanding the producer-consumer problem can deepen our understanding of concurrent programming. The so-called producer-consumer problem actually consists of two types of threads, one is the producer thread used to produce data, and the other is the consumer thread used to consume data. After the producer produces data, it is placed directly in the share In the data area, there is no need to care about the behavior of consumers; consumers only need to obtain data from the shared data area, and they no longer need to care about the behavior of producers. However, this shared data area should have such a function of concurrent collaboration between threads:
If the shared data area is full, block producers from continuing to place production data into it;
If the shared data area is empty, block consumers from continuing to consume data;
// The above description of the producer and consumer issues has been slightly changed to facilitate beginners to learn. For the original text, please refer to the link below
// Original link: https://www.jianshu.com/p/e29632593057

Smoker problem
在这里插入图片描述
Suppose there are three smoker processes in a system, and each smoker keeps rolling and smoking. Smokers need three materials to roll up and smoke a cigarette: tobacco, paper, and glue. One smoker has tobacco, one has paper, and the other has glue. There is also a supplier process that supplies all three materials indefinitely, but only two of the three materials are offered in turn at a time. The smoker who got the missing two materials would signal the supplier after rolling up and smoking a cigarette and let it continue to provide the other two materials.

Of course, what I am talking about here is the allocation in turn. If you encounter random allocation, please use the random () function to write the code.

Dining Philosophers
Five philosophers whose lifestyle is to alternate thinking and eating. They shared a round table and sat on five chairs. There are five bowls and five chopsticks on the round table. Usually a philosopher thinks. When he is hungry, he uses the chopsticks that are closest to him on the left and right. Only when he gets two chopsticks can he eat. After eating, put down the chopsticks and continue thinking. The philosopher dining problem can be regarded as a classic problem of dealing with shared resources when concurrent processes execute concurrently.
The following code is only a solution to avoid the deadlock phenomenon. Of course, there are many other ways. We can refer to one of the three methods given in the textbook.
在这里插入图片描述
For the above major issues, we need to understand the basic PV mechanism, then the first thing to do is to be familiar with the relationship between signal () and wait (), how to wake up how to apply for resources, when is the blocking state and so on.
For the convenience of readers, I will give some basic knowledge below for your reference.

What is PV operation? Is to eat ice cream in the summer.

As for the semaphore, it can be considered as a refrigerator, and then we should understand the two things of capacity and the current number of ice cream bars. The P operation is equivalent to getting tired from the refrigerator just after playing the ball. If you do n’t wait forever, then until the V operation, your dad bought the ice cream and put it in the refrigerator. In order to avoid the P operation, you have to wait for a while. After you use it, you will also be stuck in a blocked waiting state, because you ca n’t always eat it. You also have other friends and brothers and sisters, which are equivalent to other processes. Your dad ’s V operation is to send ice cream to the refrigerator. If the refrigerator is full, wait until there is a P operation. Remove the ice cream from the refrigerator. P operation: subtract 1 from the ice cream. , Indicating that ice cream has been obtained, the thread continues. Otherwise, the thread is blocked. Operation V: add 1 to the number of ice cream bars. After the addition, the number of ice cream bars is less than or equal to the capacity, indicating that your dad has successfully purchased ice cream bars and the thread continues. Otherwise, the thread is blocked.

发布了13 篇原创文章 · 获赞 2 · 访问量 1053

猜你喜欢

转载自blog.csdn.net/weixin_44033021/article/details/103944100