operating system操作系统-ch06-process synchronization-63

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
6.3
6.1 Background
Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes
}
6.14
Swap Instruction
Definition:
void Swap (boolean *a, boolean *b) {
boolean temp = *a; *a = *b; *b = temp: }
6.15
Solution using Swap
Shared Boolean variable lock initialized to FALSE; Each process has a local Boolean variable key. Solution:
concurrently and the outcome of the execution depends on the particular order in which the access takes place.
6.7
6.2 The Critical-Section Problem
critical section, in which the process may be changing common variables, updating a table, writing a file, and so on. entry section/ exit section
register2 = count register2 = register2 - 1 count = register2
Consider this execution interleaving with “count = 5” initially:
S0: producer execute register1 = count
Atomic = non-interruptable Either test memory word and set value Or swap contents of two memory words
6.12
TestAndSet Instruction
Definition:
booleanቤተ መጻሕፍቲ ባይዱTestAndSet (boolean *target) {
3. Bounded Waiting - A bound must exist on the number of times that other processes are allowed to enter their critical sections after a process has made a request to enter its critical section and before that request is granted (limited waiting)
2. Progress - If no process is executing in its critical section and there exist some processes that wish to enter their critical section, then the selection of the processes that will enter the critical section next cannot be postponed indefinitely (Let it in if empty)
boolean rv = *target; *target = TRUE; return rv: }
6.13
Solution using TestAndSet
Shared boolean variable lock., initialized to false. Solution:
while (true) { while ( TestAndSet (&lock )) ; /* do nothing // critical section lock = FALSE; // remainder section
Assume that each process executes at a nonzero speed No assumption concerning relative speed of the N processes
6.9
6.3 Peterson’s Solution
Two processes solution Assume that the LOAD and STORE instructions are atomic; that is, cannot be interrupted. The two processes share two variables:
Chapter 6: Process Synchronization
Chapter Objectives
To introduce the critical-section problem, whose solutions can be used to ensure the consistency of shared data. To present both software and hardware solutions of the criticalsection problem. To introduce the concept of atomic transaction and describe mechanisms to ensure atomicity.
Count ?!?!
6.6
Race Condition
count++ could be implemented as
register1 = count register1 = register1 + 1 count = register1
count-- could be implemented as
6.2
Content Overview
Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic Transactions
6.10
Algorithm for Process Pi Pj
while (true) { flag[i] = TRUE; turn = j;
while (true) { flag[j] = TRUE; turn = i;
while ( flag[j] && turn == j) ; CRITICAL SECTION flag[i] = FALSE; REMAINDER SECTION }
while (true) { key = TRUE; while ( key == TRUE) Swap (&lock, &key ) ; // critical section lock = FALSE; // remainder section
Currently running code would execute without preemption Generally too inefficient on multiprocessor systems
Operating systems using this not broadly scalable Modern machines provide special atomic hardware instructions
{register1 = 5}
S1: producer execute register1 = register1 + 1 {register1 = 6}
S2: consumer execute register2 = count
{register2 = 5}
S3: consumer execute register2 = register2 - 1 {register2 = 4}
Suppose that we wanted to provide a solution to the consumerproducer problem that fills all the buffers.
We can do so by having an integer count that keeps track of the number of full buffers. Initially, count is set to 0. It is incremented by the producer after it produces a new buffer and is decremented by the consumer after it consumes a buffer.
6.5
Consumer
while (true) { while (count == 0)
; // do nothing
nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--;
/* consume the item in nextConsumed }
6.4
Producer
while (true) { /* produce an item and put in nextProduced */ while (count == BUFFER_SIZE)
; // do nothing
buffer [in] = nextProduced; in = (in + 1) % BUFFER_SIZE; count++; }
while ( flag[i] && turn == i) ; CRITICAL SECTION flag[j] = FALSE; REMAINDER SECTION }
Meet the three requirements
6.11
6.4 Synchronization Hardware
Many systems provide hardware support for critical section code Uniprocessors – could disable interrupts
S4: producer execute count = register1
{count = 6 }
S5: consumer execute count = register2
{count = 4}
race condition : several processes access and manipulate the same data
int turn; Boolean flag[2] The variable turn indicates whose turn it is to enter the critical section. The flag array is used to indicate if a process is ready to enter the critical section. flag[i] = true implies that process Pi is ready!
6.8
requirement to the solutions
1. Mutual Exclusion - If process Pi is executing in its critical section, then no other processes can be executing in their critical sections (waiting while busy)
相关文档
最新文档