避免花费很长时间来完成“太多牛奶"的情况 [英] Avoid taking a long time to finish the 'too much milk' scenario
本文介绍了避免花费很长时间来完成“太多牛奶"的情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是解决牛奶过多问题"的简单方法
The following is a simple solution to the 'too much milk problem'
lock mutex;
while (1){
lock_acquire(mutex);
if (no milk)
go and buy milk;//action-1
lock_release(mutex);
}
问题在于,action-1可能要花费大量时间才能完成,这使得等待获取互斥量的任何进程都需要等待很长时间.
The problem is that, action-1 can take a lot of time to accomplish, making any of the processes waiting to acquire the mutex to wait for a long time.
避免这种情况的一种方法是设置一个计时器,这样一旦计时器关闭,购买牛奶的过程就会在有或没有牛奶的情况下返回.如您所见,这存在问题. (例如:无法确定该过程是否已经购买牛奶并在回家的路上)
One way to avoid this is to have a timer so that the process buying milk will return with or without milk once the timer goes off. As you can see, there are problems with this. (e.g: there is no way to identify whether the process has already bought milk and on its way home)
对此有更好的解决方案吗?
Is there a better solution to this?
查看全文