Concurrency Mutual Exclusion and Synchronization Chapter 5 1
- Slides: 65
Concurrency: Mutual Exclusion and Synchronization Chapter 5 1
Concurrency (并发) Concurrency encompasses a host of design issues, including: Communication among processes Sharing of and competing for resources Synchronization of the activities of multiple processes Allocation of processor time to processes 2
Concurrency 3
Difficulties of Concurrency Sharing of global resources Operating system managing the allocation of resources optimally Difficult to locate programming errors The relative speed of execution of processes is unpredictable. 4
A Simple Example void echo() { chin = getchar(); chout = chin; putchar(chout); } 5
A Simple Example Process P 1 Process P 2 发生中断. . chin = getchar(); . chout = chin; . putchar(chout); chout = chin; putchar(chout); 6
Solution to the problem Control access to the shared resource. We impose the discipline that only one process at a time may access to the shared resource. 7
Operating System Concerns Keep track of various processes Allocate and deallocate resources Processor time Memory Files I/O devices Protect data and resources Output of process must be independent of the speed of execution of other concurrent processes 8
Process Interaction Processes unaware of each other Processes indirectly aware of each other Process directly aware of each other 9
10
Competition Among Processes for Resources Mutual Exclusion(互斥) Critical sections(临界区) Only one program at a time is allowed in its critical section Example only one process at a time is allowed to send command to the printer Deadlock(死锁) Starvation(饿死) 11
Const int n=/*进程的数目*/ Void p(int i) { while(true) { entercritical(Ra); //进入临界区 /*critical section*/ exitcritical(Ra); //退出临界区 /*remainder*/ } } Void main() { parbegin(p(1), p(2), …p(n)); } 12
Cooperation among process by sharing Mutual Exclusion Deadlock Starvation Data coherence(数据一致性) 13
Requirements for Mutual Exclusion Only one process at a time is allowed in the critical section for a resource A process that halts in its noncritical section must do so without interfering with other processes No deadlock or starvation 14
Requirements for Mutual Exclusion A process must not be delayed access to a critical section when there is no other process using it No assumptions are made about relative process speeds or number of processes A process remains inside its critical section for a finite time only 15
Three approaches Software approaches: the processes would be responsible with mutual exclusion. Hardware approaches: use specialpurpose machine instructions to realize mutual exclusion. Provide some level of support within the operating system. semaphore, monitor, message passing 16
Mutual Exclusion: Software Support Dekker’s Algorithm First Attempt int turn=0; Process 0 Process 1. . . . While(turn!=0) While(turn!=1) {do nothing} {do nothing} /*critical section*/ /*critical section*/ turn=1; turn=0; 实现方法:若turn值等于该进程的进程号,则该进 程可以进入临界区执行。 17
Mutual Exclusion: Software Support Second Attempt: each process should have its own key to the critical section. enum boolean{false=0; true=1}; boolean flag[2]={false, false} Process 0 Process 1. . . . While(flag[1]) While(flag[0]) {do nothing} {do nothing} flag[0]=true; flag[1]=true; /*critical section*/ /*critical section*/ flag[0]=false; flag[1]=false; 18
Mutual Exclusion: Software Support Third Attempt: cause deadlock enum boolean{false=0; true=1}; boolean flag[2]={false, false} Process 0 Process 1. . . . flag[0]=true; flag[1]=true; While(flag[1]) While(flag[0]) {do nothing} {do nothing} /*critical section*/ /*critical section*/ flag[0]=false; flag[1]=false; 19
Mutual Exclusion: Software Support Fourth Attempt: cause livelock enum boolean{false=0; true=1}; boolean flag[2]={false, false} Process 0 Process 1. . . . flag[0]=true; flag[1]=true; While(flag[1]) While(flag[0]) {flag[0]=false; {flag[1]=false; /*延迟一段时间*/ flag[0]=true; } flag[1]=true; } /*critical section*/ /*critical section*/ flag[0]=false; flag[1]=flase; 20
Mutual Exclusion: Software Support Peterson’s Algorithm boolean flag[2]; int turn; Void P 0() Void P 1() { while(true) { while(true) { flag[0]=true; { flag[1]=true; turn=1; turn=0; While(flag[1] &&turn=1) While(flag[0] &&turn=0) /* do nothing*/ /*critical section*/ /*critical section*/ flag[0]=false; flag[1]=false; }} }} 21
Mutual Exclusion: Hardware Support Interrupt Disabling(中断禁用) A process runs until it invokes an operating system service or until it is interrupted Disabling interrupts guarantees mutual exclusion Drawback: Processor is limited in its ability to interleave programs Multiprocessing: not guarantee mutual exclusion 22
Mutual Exclusion: Hardware Support Special Machine Instructions Performed in a single instruction cycle Access to the memory location is blocked for any other instructions 23
Mutual Exclusion: Hardware Support Test and Set Instruction boolean testset (int i) { if (i == 0) i相当于锁值,i=0表示 当前该临界区未被访 { i = 1; 问 return true; } else { return false; } } 24
Mutual Exclusion: Hardware Support Exchange Instruction void exchange(int register, int memory) { int temp; 交换存储器单元和寄 存器单元的内容 temp = memory; memory = register; register = temp; } 25
Mutual Exclusion Machine Instructions Advantages Applicable to any number of processes on either a single processor or multiple processors sharing main memory It is simple and therefore easy to verify It can be used to support multiple critical sections 27
Mutual Exclusion Machine Instructions Disadvantages Busy-waiting consumes processor time Starvation is possible when a process leaves a critical section and more than one process is waiting. Deadlock 28
Semaphores(信号量) Special variable called a semaphore is used for signaling If a process is waiting for a signal, it is suspended until that signal is sent 29
Semaphores Semaphore is a variable that has an integer value May be initialized to a nonnegative number(非负数). Wait/P operation decrements(减 1) the semaphore value. If the value becomes negative, then the process is blocked. Signal/V operation increments(增 1) semaphore value. If the value is less than or equal to zero, then a blocked process is unblocked. 30
Binary Semaphore Primitives 32
Mutual Exclusion Using Semaphores 第一个执行sem. Wait 操作的进程将进入临 界区 33
Semaphores More than one process be allowed in its critical section at a time. S. count≥ 0: s. count is the number of critical resource. S. count ≤ 0: the number of processes suspended in s. queue. 35
Producer/Consumer Problem One or more producers are generating data and placing these in a buffer A single consumer is taking items out of the buffer one at time Only one producer or consumer may access the buffer at any one time 36
Producer producer: while (true) { /* produce item v */ b[in] = v; in++; } 37
Consumer consumer: while (true) { while (in <= out) /*do nothing */; w = b[out]; out++; /* consume item w */ } 保证不会从空的缓 冲区读数据 38
Producer/Consumer Problem 39
41
Producer consumer wait. B(s) n++ if(n==1)(signal. B(delay)) signal. B(s) wait. B(delay) wait. B(s) n-signal. B(s) s 0 0 0 1 1 0 0 1 n delay 0 0 1 1 1 0 1 0 0 0 42
Producer consumer wait. B(s) n++ if(n==1)(signal. B(delay)) signal. B(s) if(n==0)(wait. B(delay)) wait. B(s) n-signal. B(s) s 0 0 0 1 1 0 0 1 n delay 0 0 1 1 1 1 1 0 1 43
Producer consumer if(n==0)(wait. B(delay)) wait. B(s) n-signal. B(s) s n delay 1 0 0 0 -1 0 1 -1 0 44
45
Producer with Circular Buffer producer: while (true) { /* produce item while ((in + 1) /* do nothing b[in] = v; in = (in + 1) % } v */ % n == out) */; n 47
Consumer with Circular Buffer consumer: while (true) { while (in == out) /* do nothing */; w = b[out]; out = (out + 1) % n; /* consume item w */ } 48
49
Readers/Writers Problem Any number of readers may simultaneously read the file Only one writer at a time may write to the file If a writer is writing to the file, no reader may read it 51
Reader has priority In this case, writers can be starved as long as one reader is present wsem is used to enforce mutual exclusion in the reader function Only one writer is allowed to have access, during which no reading and writing by others When there is no reader, the first one wants to read has to wait on wsem, the subsequent readers don’t have to wait. readcount is used to keep track of number of readers. semaphore x is used to assure that readcount is updated properly. 53
54
Writer has priority The modified version, which guarantee that no reader can access the data once a writer has shown the desire to access, obviously, few more semaphores and variables are required. semaphore rsem is used to inhibit readers once a writer wishes to access. writecount is used to control the setting of rsem. semaphore y is used to assure that writecount is updated properly. Only one reader is allowed to queue on rsem all other will queue on z, because writer will not be able to jump all of them. 55
解: semaphore SR, SM 1, SM 2, SP; Buffer B 1, B 2; SR=1; SM 1=0; SM 2=0; SP=1 void read() { Buffer X; while(true) { 接收来自�入��上一个�� ; X=接收的一个�� ; SR、SP分别用于对 wait(SR); B 1、B 2的互斥访问; B 1=X; signal(SR); SM 1、SM 2用于三 个进程之间的同步 signal(SM 1); } } 61
void move() { Buffer Y; while(true) { wait(SM 1); wait(SR); Y=B 1; signal(SR) 加 Y wait(SP); B 2=Y; signal(SP); signal(SM 2); } } 62
void print() { Buffer Z; while(true) { wait(SM 2); wait(SP) Z=B 2; signal(SP) 打印Z; } } Void main() { parbegin(read, move, print); } 63
- Singhal's heuristic algorithm for mutual exclusion
- Design fifo and non fifo execution
- Mutual exclusion.
- Singhal's heuristic algorithm
- Properti-properti dari algoritma dekker adalah
- Mutual exclusion principle
- Dijkstra mutual exclusion algorithm
- Mutual exclusion in distributed systems
- Indirect and mutual holdings chapter 9
- Indirect and mutual holdings chapter 9
- Synchronization algorithms and concurrent programming
- Chapter 13 investing in mutual funds
- Concurrency control and recovery in database systems
- Pengertian concurrency
- Ccs milner
- Safety and liveness in concurrency
- Transactions and concurrency control in distributed systems
- What is concurrency control and recovery management.
- Locks in dbms
- Special segments in triangles
- Transaction management and concurrency control
- Literature review problem statement example
- Systematic review definition
- What is the inclusion and exclusion criteria in research
- Exclusion crisis in absalom and achitophel
- State pauli's exclusion principle
- Fast clock to slow clock synchronization
- One fundamental high level synchronization construct is
- Process synchronization in os
- Data synchronization in tally
- Multiprocessor synchronization
- Lean synchronization
- Soft-reconfiguration inbound
- Lock free synchronization
- Classical problems of synchronisation
- Synchronization tools in operating system
- Process synchronization in os
- Basic synchronization principles
- Windchill logout
- Syncthreads
- Process synchronization definition
- Time frequency domain
- Pthread synchronization
- Posix shared memory synchronization
- Synchronization primitives c#
- Process synchronization definition
- Linux kernel synchronization
- Linux synchronization primitives
- Physical clock synchronization in distributed system
- Wait free synchronization
- Synchronization tools in operating system
- Supply chain synchronization
- Synchronization in computer architecture
- Synchronization matrix
- Dining philosophers problem using monitors java
- Synchronization 作業系統
- Chia number of threads
- External clock synchronization
- C++ synchronization primitives
- Multiprocessor synchronization
- Classical problems of synchronization in os
- Points of concurrency in triangles
- Name it
- Concurrency in web applications
- Concurrency
- Ue4 mutex