What Is Mutex Exclusion In Operating Systems? – POFTUT

What Is Mutex Exclusion In Operating Systems?

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.

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.

When Mutex Is Useful

Following cases are where Mutex is useful.

  • 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

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.

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.

C# Mutex Example

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

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

Java Mutex Example

Here is a Java Mutex examaple.

try {
  try {
    // do something
  } finally {
} catch(InterruptedException ie) {
  // ...

Leave a Comment