当前位置:网站首页>12. Process synchronization and semaphore
12. Process synchronization and semaphore
2022-07-02 10:40:00 【PacosonSWJTU】
【README】
1. The content of this paper is summarized from B standing 《 operating system - Li Zhijun, teacher of Harbin Institute of technology 》, The content is great , Wall crack recommendation ;
2. Process synchronization : Let the cooperation between processes become reasonable and orderly ;
3. adopt Semaphore To achieve process synchronization ;
4. The operating system realizes process cooperation with semaphores , The process goes on and on ;( When does the process stop , Where to stop is particularly important )
【1】 Process cooperation : Multiple processes work together to accomplish a task
【 example 1】 Example of driver and conductor
The driver | conductor |
While(true) { Start the vehicle ;// Wait for the signal 1 The normal operation ; Stop at the station ;// Sending signal 2 } | While(true) { close ;// Sending signal 1 Sell ticket ; Open door ; // Wait for the signal 2 } |
【 example 2】 Producer consumer
producer | consumer |
Block until counter It's not equal to BUFFER_SIZE; After production data ,counter Add 1, Similar to sending signals to consumers ; | Block until counter It's not equal to 0; After consumption data ,counter reduce 1, Similar to sending signals to producers ; |
【2】 Process synchronization
Process synchronization definitions : We need to let the process go and stop , Ensure the reasonable and orderly multi process cooperation ;
【3】 Semaphore semantics
1) Signaling alone cannot solve all the problems ;
- Signals can only indicate Yes perhaps No, ; Introduce semaphores to express richer information ;
2) Problem description :
- When counter be equal to BUFFER_SIZE when , producer 1 sleep ;
- When counter be equal to BUFFER_SIZE when , producer 2 sleep ;
- next , Consumers consume a piece of data ,counter reduce 1;
- here counter reduce 1 After equal to BUFFER_SIZE reduce 1, So consumers will call wakeup Wake up producers 1;
- next , Consumer recycling consumption in addition 1 Data ,counter reduce 1;
What happened :
- here counter reduce 1 After equal to BUFFER_SIZE reduce 2 ( Because this is the second 2 Secondary consumption ), Because it 's not enough counter==BUFFER_SIZE-1 Conditions , So consumers will not wake up producers 2;
- obviously ,counter semantics Not enough to wake up all producers , So the semaphore is introduced ;
- (counter The number of free buffers is recorded , The number of sleep producers cannot be recorded , So according to counter Semantics cannot wake up all producers of sleep )
3) Semaphore
Semaphores not only need to record sleep and wakeup , You also need to record the number of currently blocked producers and other information ;
4) The semaphore starts to work
step | Producer and consumer execution details | Semaphore sem value |
1 | Buffer full , producer P1 perform , P1 sleep , The semaphore is reduced 1; Then the semaphore is -1;( Semaphore -1 Express 1 Producers sleep ) | -1 |
2 | producer P2 perform , P2 sleep , The semaphore is reduced 1; Then the semaphore is -2( Express 2 Producers sleep ) | -2 |
3 | Consumer execution 1 Secondary cycle , wakeup Wake up the P1 after , Semaphore plus 1 obtain -1;( Express 1 Producers sleep ) | -1 |
4 | Consumers will execute 1 Secondary cycle , wakeup Wake up the P2, Semaphore plus 1 obtain 0;( No producer sleeps ) | 0 |
5 | The consumer is performing a cycle ; Semaphore plus 1;( Express 1 Buffer space available ) | 1 |
6 | producer P3 perform , The semaphore is reduced 1; | 0 |
Semaphore sem Value means :
- -2: Yes 2 Producers are blocking , Or owe the producer queue 2 Unit buffer space ;
- -1: Yes 1 Producers are blocking , Or owe the producer queue 1 Unit buffer space ;
- 0: No producer blocking , The normal operation ;
- 1: It means that there is still 1 Units of available buffer space ;
- 2: It means that there is still 2 Units of available buffer space ;
【 Summary 】
- Next , Producers and consumers It can be based on Semaphore sem To determine process synchronization , Or determine the reasonable and orderly implementation of multiple process cooperation ;
5) Semaphore based process cooperation
Multiple processes work together to accomplish one thing , Multiple processes are executing , The order of implementation should be reasonable and orderly ;
In particular , After a certain degree of implementation , The process judges whether to stop and wait according to the semaphore ;
- 5.1) producer : If the semaphore is equal to 0 Or negative value , Then the producer process waits , And the semaphore decreases 1;
- 5.2) consumer : If semaphore equals negative value , Then the consumer wakes up a producer process , And the semaphore adds 1;
- If the semaphore is equal to 0, Then the consumer performs normally , And the semaphore adds 1;
【4】 Semaphore implementation
1) Semaphore definition : A special integer variable , Quantity is used to record , Signals are used to determine whether you sleep sleep And wake up wakeup;
2) Semaphore code
// Semaphore code
struct semaphore()
{
// Record the number of resources
int value ;
// Process blocking queue ( Record the process waiting on this semaphore )
PCB *queue;
}
// producer : Consumption resources ( Here, the consumption of resources refers to the consumption of a free buffer by producers , Or an array item )
P (semaphore s)
{
s.value--; // Consumption resources
if (s.value < 0) {
sleep(s.queue); // Current producer process sleep
}
}
// consumer : Generate resources ( Generating resources here refers to the consumer releasing a free buffer , Or an array item )
V (semaphore s)
{
s.value++; // Release resources
if (s.value <=0 ) {
wakeup(s.queue); // Consumers wake up the producer process of sleep
}
}
【 Add 】 In Dutch
- P Express proberen, namely test Test means , The producers ;
- V Express verhogen, namely increment, Increasing resources means , The consumer ;
【5】 Use semaphores to solve the problems of producers and consumers
1) Semaphore solves the source code of producer consumer problems
- The following source code has 3 A semaphore
- full: The number of data or contents in the cache , Or the number of buffers occupied ;
- empty: Number of free buffers ;
- mutex: Mutex semaphore , At the same time, only 1 Processes enter code ;
// 1 Define shared buffer with file
int fd = open("buffer.txt");
write(fd, 0, sizeof(in)); // Write data to in Location
read(fd, 0, sizeof(out)); // from out Location read data
// 2 Definition and initialization of semaphores
semaphore full = 0; // Represents the data produced in the buffer ( Content ) Number , Or the number of buffers used ;
semaphore empty = BUFFER_SIZE; // Indicates the number of free buffers
semaphore mutex = 1; // Mutex semaphore , At the same time, there can only be 1 A process goes in ;
// 3 producer
Producer(item) {
P(empty); // The producer tests first empty Whether the semaphore is 0( by 0 Indicates that there is no free buffer )
P(mutex);
// Read in, hold item Write to in Location ( Production operation )
V(mutex);
V(full); // Add data ( Content ) Number
}
// 4 consumer
Consumer() {
P(full); // Consumers first test whether there is content in the buffer , If not, it will block
P(mutex); // Determine whether the file can be accessed ,mutex It's a mutex semaphore , At the same time, only 1 Processes can access files (mutex reduce 1, obtain mutex Semaphore or lock )
// Read out, From file out The position is read to item, Print item ( Consumer operations )
V(mutex); // mutex Add 1, Release mutex Semaphore
V(empty);// After consumption, consumers , Increase the number of free buffers
}
边栏推荐
猜你喜欢
pytest--之测试报告allure配置
Feature (5): how to organize information
Determine whether there are duplicate elements in the array
互联网快讯:腾讯会议应用市场正式上线;Soul赴港递交上市申请书
Blender摄像机环绕运动、动画渲染、视频合成
Solutions to a series of problems in sqoop job creation
Operator-1初识Operator
Sum the two numbers to find the target value
VLAN experiment
13. Semaphore critical zone protection
随机推荐
【Visual Studio】每次打开一个Unity3D的脚本,都会自动重新打开一个新的VS2017
stm32和電機開發(上比特系統)
测试--面试题总结
What is the significance of the college entrance examination
Message mechanism -- getting to know messages and message queues for the first time
[IDL] Research
Aiphacode is not a substitute for programmers, but a tool for developers
【Unity3D】嵌套使用Layout Group制作拥有动态子物体高度的Scroll View
互联网快讯:腾讯会议应用市场正式上线;Soul赴港递交上市申请书
2021-10-04
Mongodb quickly get started with some simple operations of mongodb command line
Blender体积雾
Solution of mysql8 forgetting password file in Windows Environment
What are the popular frameworks for swoole in 2022?
Retrofit's callback hell is really vulnerable in kotlin synergy mode!
How to get the password of cpolar?
MySQL -- time zone / connector / driver type
Sum the two numbers to find the target value
Pytest-- test report allure configuration
webUI自动化学习