操作系统中同步_操作系统中的经典同步问题

操作系统中同步

经典同步问题 (Classical synchronization problem)

In this section, we present a number of different philosopher synchronization problems that are important mainly because they are examples for a large class of concurrency- control problems. These problems are used for testing nearly every new proposed synchronization scheme.

在本节中,我们提出了许多不同的哲学家同步问题,这些问题之所以重要,主要是因为它们是一大类并发控制问题的示例。 这些问题用于测试几乎所有新提出的同步方案。

有界缓冲区问题/生产者-消费者问题 (Bounded buffer problem/ producer- consumer problem)

Bounded buffer problem or producer-consumer problem is a classical synchronization problem where we have a buffer with n cells or n slots and there are 2 process producers and consumers can produce and consume one article at a time.

有界缓冲区问题或生产者-消费者问题是一个经典的同步问题,其中我们有一个带有n个单元格或n个插槽的缓冲区,并且有2个流程生产者,并且消费者可以一次生产和消费一件商品。

Write a solution using a semaphore to ensure overflow condition for producers underflow for consume and a critical section for the buffer.

使用信号量编写解决方案,以确保生产者下溢的溢出条件(消耗)和缓冲区的关键部分。

Here we have pool of n buffers.

在这里,我们有n个缓冲区的池。

Mutex - Semaphore for mutual exclusion to access buffer pool, initialized to 1.

Mutex-互斥访问缓冲池的信号量,初始化为1。

Empty - Semaphore to count empty buffer N.

-用来计数空缓冲区N的信号量。

Full - Semaphore to count fill buffer 0.

Full-信号量,用于计数填充缓冲区0。

Producer Consumer
Wait (empty)
Wait (mutex)
Critical section
Signal (mutex)
Signal (full)

制片人 消费者
Wait (empty)
Wait (mutex)
Critical section
Signal (mutex)
Signal (full)

We have used 3 semaphore e- empty cells, as well as underflow f- fixed cells as well as overflow mutex, is used for the critical section.

我们使用了3个信号量e-empty单元,以及下溢f-fixed单元以及上溢互斥体,用于关键部分。

Example:

例:

PC
while(1)
{produce();wait(e)wait(mutex)append()signal(mutex)signal(f)
}

P C
while(1)
{produce();wait(e)wait(mutex)append()signal(mutex)signal(f)
}

1. Reader/writer problem

1.读写器问题

  • There is a shared piece of text and 2 types of process in accessing this text reader and writer.

    访问此文本读取器和写入器时,有一段共享的文本和2种类型的过程。

  • There is no clash between reader and reader therefore when a reader is inside critical section then other readers may get an only entry but when a write is inside critical section then neither the reader nor the writer gets an entry.

    阅读器与阅读器之间没有冲突,因此,当某个阅读器位于关键区域内时,其他阅读器可能会获得唯一的条目,但是当某个写入位于关键区域内时,该阅读器或写入器都不会获得该条目。

  • Hence in the solution, we have used 3 resources a semaphore mutex for synchronization between writer and reader-writer.

    因此,在解决方案中,我们使用了3种资源(信号量互斥体)来实现写程序和读程序-写程序之间的同步。

  • While read count (RC) is a simple integer variable which is given security by reading semaphore which works for synchronization between reader- reader.

    读计数(RC)是一个简单的整数变量,通过读取信号量为安全性提供了安全性,该信号量可在读取器与读取器之间进行同步。

Writer

作家

while(1)
{  
wait(mutex)
write
signal(mutex)
}

Reader

读者

while(1)
{  
wait(Read)
Rc = Rc + 1;
if(Rc = = 1)
{  
wait (mutex)
} 
wait(Read)
Rc = Rc-1
if(Rc ==0)
{ 
signal(mutex)
}
signal(Read)
}

2. Dining Philosopher problem

2.用餐哲学家的问题

In this problem, there is a circular table and number of philosopher a sitting on the table. There is a chop-stick placed between every philosopher. Philosopher prior only 2 processes either they think or eat (using 2 chop-stick).

在这个问题上,有一张圆桌和一些哲学家坐在桌子上。 每个哲学家之间都有一根筷子。 哲学家仅考虑或思考了两个过程(使用两根筷子)。

Pi()
while(1)
{ 
think
P(s)
Wait (chop-stick[i])
Wait (chop-stick(i+1 % 5)
V(s)
Eat
Signal( chop-stick[i]
Signal (chop-stick(i+1 % 5)
Think
}

Solution using semaphore for philosopher synchronization

使用信号量实现哲学家同步的解决方案

Solution suffers from dead-lock and the following modification can be done:

解决方案陷入僵局,可以进行以下修改:

  1. Allow n-1 philosopher to sit on the table.

    n-1位哲学家坐在桌子上。

  2. Allow n+1 chop-stick to be put on the table.

    允许将n + 1根筷子放在桌子上。

  3. n-1 philosopher picks the left chop-stick first(the right and then the last philosopher pick the right first and left or vice-versa.

    n-1位哲学家首先选择左筷子(右再选择最后一位哲学家,然后右再选择最后一位)。

  4. Division can be done between add an even number philosopher.

    可以在除以偶数的哲学家之间完成除法。

  5. Take one more semaphore and consider 2 wait operation as a critical section.

    再增加一个信号量,并将2个等待操作视为关键部分。

Improved implementation

改进的实施

Here, there will be 2 improvements:

在这里,将有2处改进:

  • The implementation will become effective as there is no wastage of CPU clock cycles.

    该实现将变得有效,因为不会浪费CPU时钟周期。

  • The bounded wait will also be ensured as it uses strictly follow First come first serve.

    由于严格遵循“先到先得”的原则,因此也将确保一定的等待时间。

P(s)
{
s = s-1
if (s<0)
{
block();
Add it to the queue();
}
}

 P(s)
{
s = s-1
if (s<0)
{
block();
Add it to the queue();
}
}

翻译自: https://www.includehelp.com/operating-systems/classical-synchronization-problem.aspx

操作系统中同步

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/545506.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

算法设计与分析复习第一二章(时间复杂度和蛮力法)

算法复习一二章第一章时间复杂度第二章蛮力法&#xff08;1&#xff09;查找问题顺序查找&#xff08;2&#xff09;排序问题选择排序起泡排序&#xff08;3&#xff09;组合问题0-1bag问题概述&#xff08;略&#xff09;&#xff08;4&#xff09;图问题哈密顿回路TSP问题&am…

有序集合使用与内部实现原理

有序集合类型 (Sorted Set) 相比于集合类型多了一个排序属性 score(分值),对于有序集合 ZSet 来说,每个存储元素相当于有两个值组成的,一个是有序结合的元素值,一个是排序值。有序集合的存储元素值也是不能重复的,但分值是可以重复的。 当我们把学生的成绩存储在有序集…

Ubuntu12环境下Thin+rails(4)+ruby(2)+nginx+mysql 配置

Ubuntu12环境下Thinrails(4)ruby(2)nginxmysql配置1&#xff0e; 前提条件&#xff1a;已经正确安装了ubuntu12并且更行了源。2&#xff0e; 安装过程&#xff1a;2.1 安装ruby前的准备&#xff1a;1.1修改 /etc/apt/sources.list文件改为mirrors.163.com保存退出…

Oracle 游标的练习

--1、什么是游标&#xff1f;使用游标的基本步骤是什么&#xff1f; /*挡在PL/SQL块中执行查询语句&#xff08;SELECT&#xff09;和数据操纵语句&#xff08;DML&#xff09;时&#xff0c;Oracle会在内存中分配一个缓冲区&#xff0c;缓冲区中包含了处理过程的必需信息&…

集合使用与内部实现原理

集合类型 (Set) 是一个无序并唯一的键值集合。 之所以说集合类型是一个无序集合,是因为它的存储顺序不会按照插入的先后顺序进行存储,如下代码所示: 127.0.0.1:6379> sadd myset v2 v1 v3 #插入数据 v2、v1、v3 (integer) 3 127.0.0.1:6379> smembers myset #查询数…

parse 日期_日期parse()方法以及JavaScript中的示例

parse 日期JavaScript Date parse()方法 (JavaScript Date parse() method) parse() method is a Date class method, it is used to parse a given date string and returns the total number of milliseconds since 01st January 1970 (midnight) to given date string. pars…

ORA-01002 提取违反顺序

ORA-01002 提取违反顺序 ORA-01002 ORA-01002: fetch out of sequence Cause: This error means that a fetch has been attempted from a cursor which is no longer valid. Note that a PL/SQL cursor loop implicitly does fetches, and thus may also cause this error. Th…

Android 友盟SDK 终极解决报错:SocialSDK_QQZone_2.jar contains native libraries that

转自&#xff1a;http://bbs.umeng.com/thread-6552-1-2.html 报错信息&#xff1a;The library SocialSDK_QQZone_2.jar contains native libraries that will not run on the device.解决方案&#xff1a;此问题和Eclipse环境有关&#xff0c;按照如下步骤操作即可Eclipse-&g…

Redis 持久化——AOF

使用 RDB 持久化有一个风险,它可能会造成最新数据丢失的风险。因为 RDB 的持久化有一定的时间间隔,在这个时间段内如果 Redis 服务意外终止的话,就会造成最新的数据全部丢失。 可能会操作 Redis 服务意外终止的条件: 安装 Redis 的机器停止运行,蓝屏或者系统崩溃;安装 R…

数组的fill方法_数组fill()方法以及JavaScript中的示例

数组的fill方法JavaScript fill()方法 (JavaScript fill() method) fill() method is used fill the array with a given value. fill()方法用于使用给定值填充数组。 Syntax: 句法&#xff1a; array.fill(value, [start_index], [end_index]);Parameters: 参数&#xff1a…

第四章文件管理

第四章文件管理4.1_2初识文件4.1_2文件的逻辑结构无结构文件有结构文件&#xff08;1&#xff09;顺序文件&#xff08;2&#xff09;索引文件索引顺序文件多级索引顺序文件4.1_3文件目录文件控制块FCB&#xff08;2&#xff09;单级目录&#xff08;3&#xff09;两级目录结构…

current of 使用

--Where Current Of语句允许你更新或者是删除最后由cursor取的记录declarecursor c_emp is select * from emp2 for update;beginfor v_emp in c_emp loopif substr(v_emp.ename,1,1)S thenupdate emp2 set comm nvl(comm,0)1000 where current of c_emp;end if;end loop;comm…

免费的管理页面模板

2019独角兽企业重金招聘Python工程师标准>>> Free Bootstrap Admin Templates for Designers 1. Admin Lite AdminLTE - 是一个完全响应式管理模板。基于Bootstrap3的框架。高度可定制的&#xff0c;易于使用。支持很多的屏幕分辨率适合从小型移动设备到大型台式机。…

Redis 持久化——RDB

Redis 的读写都是在内存中,所以它的性能较高,但在内存中的数据会随着服务器的重启而丢失,为了保证数据不丢失,我们需要将内存中的数据存储到磁盘,以便 Redis 重启时能够从磁盘中恢复原有的数据,而整个过程就叫做 Redis 持久化。 Redis 持久化也是 Redis 和 Memcached 的主…

c# 命名空间命名规范_C#命名空间能力问题和解答 套装2

c# 命名空间命名规范1) Can we create a nested namespace in C#.NET? YesNo Answer & Explanation Correct answer: 1Yes Yes, we can create a nested namespace in C#.NET. 1)我们可以在C&#xff03;.NET中创建嵌套的名称空间吗&#xff1f; 是 没有 答案与解释 正确…

MATLAB使用教程

MATLAB使用教程2.1.1 MATLAB系统环境&#xff08;1&#xff09;命令行窗口&#xff08;2&#xff09;工作区窗口2.2.1MATLAB数值数据&#xff08;1&#xff09;强制转换如转换为int整形&#xff08;2&#xff09;判断变量类型&#xff08;3&#xff09;复型&#xff08;4&#…

Oracle笔记:循环及游标

循环及退出循环&#xff1a; --while--初值while 条件loop循环体;循环变量的变化;end loop;--breakif 条件 thenexit;end if;--continue<<label>>....if 条件 thengoto label;end if; --例declarei integer;j integer;begin j:1;<<b>> while j<9l…

Redis 事务深入解析

作为关系型数据库中一项非常重要的基础功能——事务,在 Redis 中是如何处理并使用的? 前言 事务指的是提供一种将多个命令打包,一次性按顺序地执行的机制,并且保证服务器只有在执行完事务中的所有命令后,才会继续处理此客户端的其他命令。 事务也是其他关系型数据库所必…

Java Thread类的最终void join()方法与示例

线程类最终void join() (Thread Class final void join()) This method is available in package java.lang.Thread.join(). 软件包java.lang.Thread.join()中提供了此方法。 join() method is applicable when a thread wants to wait until completing some other thread the…

解决myeclipse中新导入的工程旁出现红色感叹号的问题

2019独角兽企业重金招聘Python工程师标准>>> 或许很多像我这样的java初学者在使用myeclipse时出现新导入的工程旁边有红色的感叹号。 1.问题一般就是java build path 设置不正确的问题。解决步骤如下&#xff1a; 右击工程找到Build Path——>Configure Build Pa…