【RTS笔记3】Shared Resources
Monitors
A monitor is an abstract data type that includes
- a shared resource, and
- procedures for accessing the resource.
The critical data can only be accessed through procedures contained in the monitor.
The critical data is hidden from the rest of the program.
→ Tasks must communicate with the monitor, rather than directly with the
resource itself.
Only one task at a time may actively execute a monitor procedure
- Other calls have to queue, therefore mutual exclusion is guaranteed
Advantages over Semaphores
A monitor automatically enforces mutual exclusion.
I With semaphores the responsibility for correctly accessing critical resources is
placed with tasks that use them.
A monitor can be developed and tested in isolation.
I With semaphores all the tasks that use the same shared resource need to be
written and tested together.
Deadlock 死锁
System Liveness
Reason of multi-threaded processes stuck:
-
Deadlock死锁: all affected tasks are suspencded indefinitely.
两个以上的线程互相争夺资源导致互相等待,无外力作用无法推进。 -
Livelock活锁: all affected tasks are indefinitely executing busy-wait loops. I One thread acts in response to another, which in turn influences the first.
线程互相谦让导致都无法使用资源。线程T1和T2都想用资源R,但他们都在等待对方先使用。双方可能会遇到永远等待的情况。 -
Starvation饥饿: A task is never able to gain access to a shared resource because other tasks continually gain access before it.
饥饿:是指如果线程T1占用了资源R,线程T2又请求封锁R,于是T2等待。T3也请求资源R,当T1释放了R上的封锁后,系统首先批准了T3的请求,T2仍然等待。然后T4又请求封锁R,当T3释放了R上的封锁之后,系统又批准了T4的请求…,T2可能永远等待。
Dealing with Deadlock 处理死锁
prevention 预防
programs to eliminate the circular waits:
- placing an ordering on the resources
- only allowing tasks to request resources in that order.
【假设给资源R1R2R3排序,持有R2资源的task只能申请R3资源,不能反过来申请R1资源】
detection侦测
【循环检测死锁,检测到后要么杀进程要么拿走资源】
Detection typically involves periodically searching for circular task dependencies. Once a deadlock is detected, recovery involves either
- terminating one or more processes,
- removing one or more resources from a task.
avoidance 避免
Various algorithms are used, the most famous being the banker’s algorithm. This requires some prior information about how resources are to be requested.
Inter-task Communication 任务间通讯
Concurrent Task Communication
最简单办法:shared memory locations共享内存地址
这办法需要用到一些mechanisms,来保证consistency。例如 pools 和
buffers.
POOL
A data pool consists of a set of memory locations common to multiple tasks (e.g., shared variables). In data pools, writing to the pool is destructive while reading from the pool is non-destructive. Access to the pool needs to be carried out under mutual exclusion.
Buffer (or channel):
A buffer (or channel) is shared memory into which data is placed by producer tasks, and from which data is extracted by consumer tasks. Data is consumed in the same order it was produced. In buffers, writing to the pool is non-destructive (data is appended to the buffer) while reading from the pool is destructive (data is removed from the buffer). Buffers may be bounded or unbounded.
In an unbounded buffer:
无界Buffer:增加内存,减缓系统速度
the memory allocated to the buffer grows dynamically.
This could fill up the memory, and slow the system down.
In a bounded buffer:
有界buffer:暂停生成
the memory allocated to the buffer is fixed.
If the buffer becomes full the producer(s) need to be suspended.
If the buffer becomes empty the consumer(s) need to be suspended.
→ Require synchronisation (producer-consumer problem).
Message-passing 消息传递
A more sophisticated way to achive both synchronisation and communication is to set up a full message-passing system between tasks.
For this, four issues need to be dealt with:
- Message structure and format. 【结构格式】
- Method of synchronisation between communicating tasks. 【同步】
- Method of task naming/addressing.【命名寻址】
- Method of message queuing.【queue队列】
Message Format:
一些例子:
- 消息是几bit的(比如32或64)
- 如何编码 how the information is encoded
- 长度是否固定fixed- or variable-length,
- 是否包含headers/footers or error-checking information.
Method of Synchronisation:
- Asynchronous同步 (or no-wait) sender proceeds immediately after sending the message, regardless of whether the message is received or not. 【发送者一发信息就行动】
- Synchronous异步 (or rendezvous) sender proceeds only when the message has been received.【发送者发消息,接收者收到就行动】
- Remote invocation远程调用 (or extended rendezvous) sender proceeds only when a reply has been returned from the receiver.【发送者发消息,发送者收到接收者的回执后行动】
Asynchronous message passing
同步消息传递涉及到等待服务器响应消息的客户端。消息可以双向地向两个方向流动。本质上,这意味着同步消息传递是双向通信。即发送方向接收方发送消息,接收方接收此消息并回复发送方。发送者在收到接收者的回复之前不会发送另一条消息。
同步执行的特征为:在两个通信应用系统之间必须要进行同步, 两个系统必须都在正常运行, 并且会中断客户端的执行流, 转而执行调用。发送程序和接收程序都必须一直做好相互通信的准备。发送程序首先向接收程序发起一个请求(发送消息)。发送程序紧接着就会堵塞它自身的进程, 直到收到接收程序的响应。发送程序在收到响应后会继续向下进行处理。
Synchronous message passing
异步消息传递涉及不等待来自服务器的消息的客户端。事件用于从服务器触发消息。因此,即使客户机被关闭,消息传递也将成功完成。异步消息传递意味着,它是单向通信的一种方式,而交流的流程是单向的。
当使用异步消息传送时, 调用者在发送消息以后可以不用等待响应, 可以接着处理其他任务。对于异步通信, 一个应用程序(请求者或发送者)将请求发送给另一个应用程序, 然后可以继续向下执行它自身的其他任务。发送程序无须等待接收程序的执行和返回结果, 而是可以继续处理其他请求。与同步方式不同, 异步方式中两个应用系统(发送程序和接收程序)无须同时都在运行, 也无须同时都在处理通信任务。
Task Naming/Addressing:
Effective communication also requires a system to determine a naming/addressing system (i.e., where messages are sent).【找到消息在哪里】
There are two issues to consider for this: directness and symmetry.
directness:
symmetric:
Method of message queuing
There are two main approaches:
- First-In-First-Out (FIFO) 【先进先出】: Messages are read in the order they are sent.
- Priority-Based【优先度排列】: The sender specifies the priority of the message - more urgent
messages are read first by the receiver.
Q&A
The Sleeping-Barber Problem 睡眠理发师
• A barbershop consists of a waiting room with n chairs and barber room containing the barber chair.
• If these are no customers to be served ,the barber goes to sleep.
• If a customer enters the barbershop and all chairs are occupied, then the customer leaves the shop.
• If the barber is busy but chairs are available, then customer sits in one of the free chairs.
• If the barber is asleep,the customer wakes up the barber.
• 一个理发店由一个含有n把椅子等候房间和一个只有一把理发椅子的剪发房间组成。如果没有顾客的时候,发型师就去睡觉了。如果一位顾客到来时,理发店的所有椅子都没有空闲,那么这位顾客会离开。如果理发师正在给顾客剪头发,理发店还有空闲的椅子,那么在此时到达的顾客会坐下等待。
The Cigarette-Smokers Problem 吸烟者问题
• Suppose a cigarette requires three ingredients, tobacco, paper and match. There are three chain smokers. Each of them has only one ingredient with infinite supply. There is an agent who has infinite supply of all three ingredients. To make a cigarette, the smoker has tobacco (respectively, paper and match) must have the other two ingredients paper and match (respectively, tobacco and match, and tobacco and paper). The agent and smokers share a table. The agent randomly generates two ingredients and notifies the smoker who needs these two ingredients. Once the ingredients are taken from the table, the agent supplies another two. On the other hand, each smoker waits for the agent’s notification. Once it is notified, the smoker picks up the ingredients, makes a cigarette, smokes for a while, and goes back to the table waiting for his next ingredients.