操作系统——Process Synchronization 进程同步

目录​​​​​​​

1. Background 背景

1.1 What is Process Synchronization? 什么是进程同步

1.2 Race condition 竞争条件

1.3 Syncronization 同步

2. The Critical-Section Problem 临界区问题

3. Solutions for critical-section problem 临界区问题解决办法

3.1 Software Solutions

3.1.1 Peterson's Solution

3.2 Hardware Solutions

3.3 Mutex locks / Mutual exclusion 互斥锁

3.4 Semaphore 信号量

4. Classical Problems of Synchronization 同步的经典问题

4.1 The Bounded-Buffer / Producer-Consumer Problem 有限缓冲问题

4.2 The Readers–Writers Problem  读者-写者问题

4.3 The Dining-Philosophers Problem 哲学家进餐问题


1. Background 背景

1.1 What is Process Synchronization? 什么是进程同步

PS is the task of coordinating the execution of processes in a way that no two processes can have access to the same shared data and resources. 两个进程不能同时访问共享资源。

1.2 Race condition 竞争条件

Concurrent access to shared data may result in data inconsistency. Maintaining data consistency requires mechanisms to ensure the orderly execution of cooperating processes. 对共享数据的并发访问可能导致数据的不一致。

Race condition: The situation where several processes access and manipulate shared data concurrently. The final value of the shared data depends upon which process finishes last. 指的是几个进程并发访问和操作共享数据。共享数据的最终值取决于哪个进程最后完成。

To prevent race conditions, concurrent processes must be synchronized. 为了防止竞争,必须同步并发的进程

1.3 Syncronization 同步

同步发生在协作进程之间,基础思想是需要一个任何形式的锁。


2. The Critical-Section Problem 临界区问题

Each (concurrent) process has a code segment, called Critical Section (CS), in which the shared data is accessed. When using critical sections, the code can be broken down into the following sections:

The critical-section problem is to design a protocol that the processes can use to cooperate. Each process must request permission to enter its critical section.

  1. 启用这个请求的代码段叫做entry section
  2. entry section 后面会有exit section
  3. entry和exit中间的进程代码段叫做critical section
  4. 剩下的代码叫做remainder section

Race condition updating a variable

Critical section to prevent a race condition 

 Multiprogramming allows logical parallelism, uses devices efficiently but we lose correctness when there is a race condition. So, we forbid/deny logical parallelism inside critical section, so we lose some parallelism, but we regain correctness.

A solution to the critical-section problem must satisfy the following three requirements (临界区的三个原则)

  1. Mutual exclusion. 互斥 If process Pi is executing in its critical section, then no other processes can be executing in their critical sections. 如果已有进程在临界区执行,其他进程不能在其临界区执行。
  2. Progress. 前进 If no process is executing in its critical section and some processes wish to enter their critical sections, then only those processes that are not executing in their remainder sections can participate in deciding which will enter its critical section next, and this selection cannot be postponed indefifinitely. 如果没有进程在临界区,其他进程应该被允许进入临界区。
  3. Bounded waiting. 有效等待 There exists a bound, or limit, 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. 进程做出进入临界区请求后,其他进程进入临界区的次数是有上限的,进程发出请求后等待允许的时间有限。

Types of solutions to CS problem

  1. Software solutions: algorithms whose correctness relies only on the assumption that only one process/thread at a time can access a memory location/resource.
  2. Hardware solutions: rely on special machine instructions for “locking”.
  3. Operating System and Programming Language solutions (e.g.
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高纯度Coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值