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

本文探讨操作系统中的经典同步问题,如有界缓冲区问题(生产者-消费者问题),并利用信号量解决并发控制问题。同时介绍了读写器问题和用餐哲学家问题及其解决方案,强调了死锁和资源分配策略的重要性。
摘要由CSDN通过智能技术生成

操作系统中同步

经典同步问题 (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

操作系统中同步

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值