操作系统 互斥到底是什么_操作系统中互斥排除是什么?

《操作系统 互斥到底是什么_操作系统中互斥排除是什么?》

操作系统 互斥到底是什么

Operating systems are mainly runs processes and applications to share resources of the system. These resources may CPU, RAM, Network Connection etc. Mutual Exclusion Object a.k.a. Mutex is a program object that allows and manages multiple process and applications to share these system resources simultaneously.

操作系统主要是运行进程和应用程序以共享系统资源。 这些资源可能包括CPU,RAM,网络连接等。互斥对象(也称为互斥对象)是一个程序对象,它允许并管理多个进程和应用程序以同时共享这些系统资源。

示例案例 (Example Case)

Log files are used to store information about events created by services, programs and applications. Single log file can be populated by multiple programs. If in the same time two program try to access log file and add some event this will create a collusion. This should be managed and the log file which is a resources should be shared between this programs. There are different libraries for different programming languages in order to create Mutex.

日志文件用于存储有关服务,程序和应用程序创建的事件的信息。 单个日志文件可以由多个程序填充。 如果在同一时间两个程序尝试访问日志文件并添加一些事件,则将创建共谋。 应该对此进行管理,并且应在此程序之间共享作为资源的日志文件。 为了创建Mutex,针对不同的编程语言有不同的库。

当互斥锁有用时 (When Mutex Is Useful)

Following cases are where Mutex is useful.

以下是Mutex有用的情况。

  • Writing to the single file by multiple processes.

    通过多个过程写入单个文件。

  • Reading an writing data to the network resource like network interface

    将写入数据读取到网络资源(如网络接口)

  • Web server threads writing to a file

    Web服务器线程写入文件

临界区(Critical Section)

Shared resource area is named as Critical Section . Critical section can be accessed by multiple Threads or applications. We can lock critical sections in case of any thread or application will access it. After the source usage is completed we can unlock the Critical Section.

共享资源区域称为Critical Section 。 关键部分可以由多个线程或应用程序访问。 我们可以锁定关键部分,以防任何线程或应用程序访问它。 在完成源使用后,我们可以解锁关键部分。

比赛条件 (Race Condition)

We have learned Critical Section in previous part. If two process thread try to access same resource in the same time and try to change in the same time this will create a Race Condition . Two or more process thread will create a race to access to the same resource.

我们已经在上一部分中学习了临界部分。 如果两个进程线程试图同时访问同一资源并试图同时进行更改,这将创建Race Condition 。 两个或多个进程线程将创建争用访问同一资源的竞争。

C#Mutex示例 (C# Mutex Example)

In this part we will look a C# program which uses Mutex order to manage object access by locking the object.

在这一部分中,我们将看一个C#程序,该程序使用互斥锁命令通过锁定对象来管理对象访问。

private static readonly Object instanceLock = new Object();
private static MySingleton instance;
public static MySingleton Instance
{
    lock(instanceLock)
    {
        if(instance == null)
        {
            instance = new MySingleton();
        }
        return instance;
    }
}

Java Mutex示例 (Java Mutex Example)

Here is a Java Mutex examaple.

这是Java Mutex示例。

try {
  mutex.acquire();
  try {
    // do something
  } finally {
    mutex.release();
  }
} catch(InterruptedException ie) {
  // ...
}

翻译自: https://www.poftut.com/what-is-mutex-exclusion-in-operating-systems/

操作系统 互斥到底是什么

    原文作者:cunjiu9486
    原文地址: https://blog.csdn.net/cunjiu9486/article/details/109076682
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞