【发布时间】:2019-11-24 11:33:17
【问题描述】:
我想知道 timestamp 是否可以用于解决进程同步问题,当竞争条件发生时?
下面是 entry 和 的算法>exit section为每个想要进入临界区的进程。入口部分使用 FCFS(先到先服务)技术来提供对关键部分的访问权限。
interested[N] is shared array of N integers where N is number of processes.
// This section executed when process enters critical section.
entry_section (int processNumber) {
interested [processNumber] = getCurrentTimeStamp (); // Gets the current timestamp.
index = findOldestProcessNumber (interested); // Find the process number with least timestamp.
while (index != processNumber);
}
// This section executed when process leaves critical section.
exit_section (int processNumber) {
interested [processNumber] = NULL;
}
在我看来,这个算法满足同步的所有条件,即互斥、进度、有界等待和可移植性。那么,我说的对吗?
感谢您抽出宝贵时间。
【问题讨论】:
-
您在测试时发现了什么?
-
@MartinJames 我没有用任何语言实现它,但我在想这是否可以做到?
-
@MartinJames 对于 2 个进程有一个很好的解决方案,即满足问题中提到的所有条件的彼得森算法,但我认为这个解决方案也可以适用于 2 个以上的进程。
-
为什么有人对这个问题投了反对票?问这种类型的问题是不是不对?
-
@Shiv - 我只是想知道,您的流程共享哪些数据?如果两个或多个进程在完全相同的时间戳使用共享数据会发生什么?如果由于某种原因两个或多个进程以相同的时间戳开始会发生什么?这可能会发生,因为 2Ghz CPU 每秒可以执行 20 亿个周期。所以从技术上讲,多个操作可以在同一毫秒内发生。因此,如果您的时间戳只达到秒,那么这个问题将变得最糟糕。
标签: process operating-system synchronization