首页 > 解决方案 > 时间戳可以用于同步具有竞争条件的进程吗?

问题描述

我想知道当竞争条件发生时,时间戳是否可以用来解决进程同步问题?
下面是每个想要进入临界区的进程的进入退出部分的算法。入口部分使用 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;
    }

据我说,这个算法满足同步的所有条件,即互斥、进度、有界等待和可移植性。那么,我说的对吗?

感谢您抽出时间。

标签: processoperating-systemsynchronization

解决方案


简短而甜蜜,这是这种方法的两个问题。

  1. 您的所有进程都在忙于等待。这意味着即使进程不能进入临界区,它仍然不能休息。这意味着 os 调度程序需要不断地调度所有感兴趣的进程,即使它们没有产生有意义的输出。这会损害性能和功耗。
  2. 这是最大的。不能保证两个进程不会有相同的时间戳。这可能不太可能,但当您想要保证互斥以防止竞争条件时,可能性并不是您要寻找的。

推荐阅读