操作系统七版课件
Chapter 6: Process Synchronization Background The Critical-Section Problem Peterson’s Solution Synchronization Hardware Semaphores Classic Problems of Synchronization Monitors Synchronization Examples Atomic Transactions Background Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. Shared-memory solution to bounded-butter problem (Chapter 4) allows at most n – 1 items in buffer at the same time. A solution, where all N buffers are used is not simple. Suppose that we modify the producer-consumer code by adding a variable counter, initialized to 0 and incremented each time a new item is added to the buffer Bounded-Buffer Shared data#define BUFFER_SIZE 10 typedef struct { . . . } item; item buffer[BUFFER_SIZE]; int in = 0;int out = 0;int counter = 0; Bounded-Buffer Producer process item nextProduced; while (1) { while (counter == BUFFER_SIZE) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BUFFER_SIZE; counter++; } Bounded-Buffer Consumer process item nextConsumed; while (1) { while (counter == 0) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BUFFER_SIZE; counter--; } Bounded Buffer The statementscounter++;counter--;must be performed atomically. Atomic operation means an operation that completes in its entirety without interruption. Bounded Buffer The statement “count++” may be implemented in machine language as:register1 = counter register1 = register1 + 1counter = register1 Bounded Buffer If both the producer and consumer attempt to update the buffer concurrently, the assembly language statements may get interleaved. Interleaving depends upon how the producer and consumer processes are scheduled. Bounded Buffer Assume counter is initially 5. One interleaving of statements is:producer: register1 = counter (register1 = 5)producer: register1 = register1 + 1 (re
您可能关注的文档
最近下载
- 2024—2025学年度安徽省合肥市第六中学高一下学期期末考试历史试题(含答案).docx VIP
- 4.国内高空观测数据BUFR编码格式(V1.0).doc VIP
- 兽药企业安全生产培训PPT.ppt VIP
- 输变电工程建设标准强制性条文10248-2016_部分2.pdf VIP
- 2025年高考(北京卷)物理真题及答案.doc VIP
- 2026人教版五年级上册语文期末考试3套试卷(打印版含答案解析).pdf
- 承插型盘扣式钢管模板支架施工验收记录表.doc VIP
- 长郡初三期末物理试卷及答案.doc VIP
- 湖北省武汉市江汉区2024-2025学年八年级上学期1月期末道德与法治试卷.docx VIP
- 黄冈达标卷数学2年级下(单元测+期中+期末).docx VIP
原创力文档

文档评论(0)