Category: Subjects

Semaphore | Semaphore in OS | Binary Semaphore

Semaphores in OS-

 

Before you go through this article, make sure that you have gone through the previous article on Semaphores in OS.

 

We have discussed-

  • A semaphore is a simple integer variable used to provide synchronization among the processes.
  • There are mainly two types of semaphores-

 

 

In this article, we will discuss about Binary Semaphores.

 

Binary Semaphores-

 

A binary semaphore is implemented as-

 

struct semaphore
{
   enum value (0,1);
   Queue type L;
}

Wait (semaphore s)
{
   if (s.value == 1)
   {
      s.value=0;
   }
   else
   {
      put process (PCB) in s.L;
      sleep();
   }
}

Signal (semaphore s)
{
   if (s.L is empty)
   {
      s.value=1;
   }
   else
   {
       select a process (PCB) from s.L;
       wake up();
   }
}

 

Explanation-

 

The above implementation of binary semaphore has been explained in the following points-

 

Point-01:

 

A binary semaphore has two components-

  • An integer value which can be either 0 or 1
  • An associated waiting list (usually a queue)

 

Point-02:

 

  • The waiting list of binary semaphore contains the processes that got blocked when trying to enter the critical section.
  • In waiting list, the blocked processes are put to sleep.
  • The waiting list is usually implemented using a queue data structure.
  • Using a queue as waiting list ensures bounded waiting.
  • This is because the process which arrives first in the waiting queue gets the chance to enter the critical section first.

 

Point-03:

 

  • The wait operation is executed when a process tries to enter the critical section.
  • Then, there are two cases possible-

 

Case-01: Binary Semaphore Value = 1

 

If the value of binary semaphore is 1,

  • The value of binary semaphore is set to 0.
  • The process is allowed to enter the critical section.

 

Case-02: Binary Semaphore Value = 0

 

If the value of binary semaphore is 0,

  • The process is blocked and not allowed to enter the critical section.
  • The process is put to sleep in the waiting list.

 

Point-04:

 

  • The signal operation is executed when a process takes exit from the critical section.
  • Then, there are two cases possible-

 

Case-01: Waiting List is Empty-

 

  • If the waiting list is empty, the value of binary semaphore is set to 1.

 

Case-02: Waiting List is Not Empty-

 

  • If the waiting list is not empty, a process is chosen from the waiting list and wake up to execute.

 

Point-05:

 

Binary semaphores are mainly used for two purposes-

  • To ensure mutual exclusion.
  • To implement the order in which the processes must execute.

 

Example-

 

Wait (S1)

Process P1

Signal (S2)

Process P2

Signal (S1)

Wait (S2)

Process P3

 

In this example-

  • Two binary semaphores S1 and S2 both initialized with 0 are used.
  • The execution order of the processes is P2 → P1 → P3.

 

To gain better understanding about Binary Semaphores,

Watch this Video Lecture

 

Next Article- Practice Problems On Binary Semaphores

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Semaphore in OS | Practice Problems

Semaphore in OS-

 

Before you go through this article, make sure that you have gone through the previous article on Semaphores in OS.

 

We have discussed-

  • A semaphore is a simple integer variable used to provide synchronization among the processes.
  • There are mainly two types of semaphores-

 

 

In this article, we will discuss practice problems based on counting semaphores.

 

PRACTICE PROBLEMS BASED ON COUNTING SEMAPHORES IN OS-

 

Problem-01:

 

A counting semaphore S is initialized to 10. Then, 6 P operations and 4 V operations are performed on S. What is the final value of S?

 

Solution-

 

We know-

  • P operation also called as wait operation decrements the value of semaphore variable by 1.
  • V operation also called as signal operation increments the value of semaphore variable by 1.

 

Thus,

Final value of semaphore variable S

= 10 – (6 x 1) + (4 x 1)

= 10 – 6 + 4

= 8

 

Problem-02:

 

A counting semaphore S is initialized to 7. Then, 20 P operations and 15 V operations are performed on S. What is the final value of S?

 

Solution-

 

We know-

  • P operation also called as wait operation decrements the value of semaphore variable by 1.
  • V operation also called as signal operation increments the value of semaphore variable by 1.

 

Thus,

Final value of semaphore variable S

= 7 – (20 x 1) + (15 x 1)

= 7 – 20 + 15

= 2

 

Problem-03:

 

A shared variable x, initialized to zero, is operated on by four concurrent processes W, X, Y, Z as follows. Each of the processes W and X reads x from memory, increments by one, stores it to memory and then terminates. Each of the processes Y and Z reads x from memory, decrements by two, stores it to memory, and then terminates. Each process before reading x invokes the P operation (i.e. wait) on a counting semaphore S and invokes the V operation (i.e. signal) on the semaphore S after storing x to memory. Semaphore S is initialized to two. What is the maximum possible value of x after all processes complete execution?

  1. -2
  2. -1
  3. 1
  4. 2

 

Solution-

 

The given question may be pictorially represented as-

 

 

Initially, counting semaphore S is initialized with value 2.

 

Now, We have been asked the maximum possible value of x after all the processes complete execution.

 

Clearly,

  • Processes W and X increments the value of x.
  • Processes Y and Z decrements the value of x.

 

To obtain the maximum value of x, the processes must execute in such a way that-

  • Only the impact of the processes W and X remains on the value of x.
  • The impact of processes Y and Z gets lost on the value of x.

 

STRATEGY

 

  • First of all, make the process W read the value of x = 0.
  • Then, preempt the process W.
  • Now, schedule process Y and process Z to execute one by one.
  • After executing them, reschedule process W.
  • Now, when process W gets scheduled again, it starts with value x = 0 and increments this value.
  • This is because before preemption it had read the value x = 0.
  • The updates from the processes Y and Z gets lost.
  • Later, execute process X which again increments the value of x.
  • Here, the lost update problem has been exploited.

 

This is achieved as-

 

Step-01:

 

  • Process W arrives.
  • It executes the wait(S) operation and the value of S decrements by 1. Now, S = 1.
  • It reads the value x = 0.
  • It gets preempted.

 

Step-02:

 

  • Process Y arrives.
  • It executes the wait(S) operation and the value of S decrements by 1. Now, S = 0.
  • It reads the value x = 0.
  • It decrements the value of x by 2. Now, x = 0 – 2 = -2.
  • It writes the value x = -2 in the memory.
  • It executes the signal(S) operation and the value of S increments by 1. Now, S = 1.
  • Now, execution of process Y is completed.

 

Step-03:

 

  • Process Z arrives.
  • It executes the wait(S) operation and the value of S decrements by 1. Now, S = 0.
  • It reads the value x = -2.
  • It decrements the value of x by 2. Now, x = – 2 – 2 = -4.
  • It writes the value x = -4 in the memory.
  • It executes the signal(S) operation and the value of S increments by 1. Now, S = 1.
  • Now, execution of process Z is completed.

 

Step-04:

 

  • Process W gets scheduled again.
  • It resumes its execution from where it left.
  • Before preemption it had already read the value x = 0.
  • Now, it increments the value of x by 1. Now, x = 0 + 1 = 1.
  • It writes the value x = 1 in the memory.
  • It executes the signal(S) operation and the value of S increments by 1. Now, S = 2.
  • Now, execution of process W is completed.

 

Step-05:

 

  • Process X arrives.
  • It executes the wait(S) operation and the value of S decrements by 1. Now, S = 1.
  • It reads the value x = 1.
  • It increments the value of x by 1. Now, x = 1 + 1 = 2.
  • It writes the value x = 2 in the memory.
  • It executes the signal(S) operation and the value of S increments by 1. Now, S = 2.
  • Now, execution of process X is completed.

 

Thus,

  • Final value of x = 2.
  • This is the maximum possible value of x that can be achieved after executing all the 4 processes.
  • Option (D) is correct.

 

Problem-04:

 

In problem-03, what is the minimum possible value of x after all processes complete execution?

  1. -4
  2. -2
  3. 2
  4. 4

 

Solution-

 

To obtain the minimum value of x, the processes must execute in such a way that-

  • Only the impact of the processes Y and Z remains on the value of x.
  • The impact of processes W and X gets lost on the value of x.

 

This can be achieved if processes execute in the following manner-

 

Step-01:

 

  • Process Y arrives.
  • It executes the wait(S) operation and the value of S decrements by 1. Now, S = 1.
  • It reads the value x = 0.
  • It gets preempted.

 

Step-02:

 

  • Process W arrives.
  • It executes the wait(S) operation and the value of S decrements by 1. Now, S = 0.
  • It reads the value x = 0.
  • It increments the value of x by 1. Now, x = 0 + 1 = 1.
  • It writes the value x = 1 in the memory.
  • It executes the signal(S) operation and the value of S increments by 1. Now, S = 1.
  • Now, execution of process W is completed.

 

Step-03:

 

  • Process X arrives.
  • It executes the wait(S) operation and the value of S decrements by 1. Now, S = 0.
  • It reads the value x = 1.
  • It increments the value of x by 1. Now, x = 1 + 1 = 2.
  • It writes the value x = 2 in the memory.
  • It executes the signal(S) operation and the value of S increments by 1. Now, S = 1.
  • Now, execution of process X is completed.

 

Step-04:

 

  • Process Y gets scheduled again.
  • It resumes its execution from where it left.
  • Before preemption it had already read the value x = 0.
  • Now, it decrements the value of x by 2. Now, x = 0 – 2 = -2.
  • It writes the value x = -2 in the memory.
  • It executes the signal(S) operation and the value of S increments by 1. Now, S = 2.
  • Now, execution of process Y is completed.

 

Step-05:

 

  • Process Z arrives.
  • It executes the wait(S) operation and the value of S decrements by 1. Now, S = 1.
  • It reads the value x = -2.
  • It decrements the value of x by 2. Now, x = -2 – 2 = -4.
  • It writes the value x = -4 in the memory.
  • It executes the signal(S) operation and the value of S increments by 1. Now, S = 2.
  • Now, execution of process Z is completed.

 

Thus,

  • Final value of x = -4.
  • This is the minimum possible value of x that can be achieved after executing all the 4 processes.

 

Thus, Option (A) is correct.

 

To watch video solutions and practice other problems,

Watch this Video Lecture

 

Next Article- Binary Semaphores

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Semaphore | Semaphore in OS | Counting Semaphore

Process Synchronization-

 

Before you go through this article, make sure that you have gone through the previous article on Process Synchronization.

 

We have discussed-

  • Process Synchronization provides a synchronization among the processes.
  • Synchronization mechanisms allow the processes to access critical section in a synchronized manner.
  • This avoids the inconsistent results.

 

Semaphores in OS-

 

  • A semaphore is a simple integer variable.
  • It is used to provide synchronization among multiple processes running concurrently.

 

Types of Semaphores-

 

There are mainly two types of semaphores-

 

 

  1. Counting Semaphores
  2. Binary Semaphores or Mutexes

 

In this article, we will discuss about Counting Semaphores.

Learn about Binary Semaphores.

 

Counting Semaphores-

 

A counting semaphore is implemented as-

 

struct semaphore
{
   int value;
   Queue type L;
}

Wait (semaphore s)
{
   s.value = s.value - 1;
   if (s.value < 0)
   {
      put process (PCB) in L;
      sleep();
   }
   else
      return;
}

Signal (semaphore s)
{
   s.value = s.value + 1;
   if (s.value <=0 )
   {
       select a process (PCB) from L;
       wake up();
   }
}

 

Explanation-

 

The above implementation of counting semaphore has been explained in the following points-

 

Point-01:

 

A counting semaphore has two components-

  • An integer value
  • An associated waiting list (usually a queue)

 

Point-02:

 

The value of counting semaphore may be positive or negative.

  • Positive value indicates the number of processes that can be present in the critical section at the same time.
  • Negative value indicates the number of processes that are blocked in the waiting list.

 

Point-03:

 

  • The waiting list of counting semaphore contains the processes that got blocked when trying to enter the critical section.
  • In waiting list, the blocked processes are put to sleep.
  • The waiting list is usually implemented using a queue data structure.
  • Using a queue as waiting list ensures bounded waiting.
  • This is because the process which arrives first in the waiting queue gets the chance to enter the critical section first.

 

Point-04:

 

  • The wait operation is executed when a process tries to enter the critical section.
  • Wait operation decrements the value of counting semaphore by 1.

 

Then, following two cases are possible-

 

Case-01: Counting Semaphore Value >= 0

 

  • If the resulting value of counting semaphore is greater than or equal to 0, process is allowed to enter the critical section.

 

Case-02: Counting Semaphore Value < 0

 

  • If the resulting value of counting semaphore is less than 0, process is not allowed to enter the critical section.
  • In this case, process is put to sleep in the waiting list.

 

Point-05:

 

  • The signal operation is executed when a process takes exit from the critical section.
  • Signal operation increments the value of counting semaphore by 1.

 

Then, following two cases are possible-

 

Case-01: Counting Semaphore <= 0

 

  • If the resulting value of counting semaphore is less than or equal to 0, a process is chosen from the waiting list and wake up to execute.

 

Case-02: Counting Semaphore > 0

 

  • If the resulting value of counting semaphore is greater than 0, no action is taken.

 

Point-06:

 

  • By adjusting the value of counting semaphore, the number of processes that can enter the critical section can be adjusted.
  • If the value of counting semaphore is initialized with N, then maximum N processes can be present in the critical section at any given time.

 

Point-07:

 

  • To implement mutual exclusion, the value of counting semaphore is initialized with 1.
  • It ensures that only one process can be present in the critical section at any given time.

 

Point-08:

 

In a system,

  • Consider n units of a particular non-shareable resource are available.
  • Then, n processes can use these n units at the same time.
  • So, the access to these units is kept in the critical section.
  • The value of counting semaphore is initialized with ‘n’.
  • When a process enters the critical section, the value of counting semaphore decrements by 1.
  • When a process exits the critical section, the value of counting semaphore increments by 1.

 

In such scenarios, value of counting semaphore is initialized with value greater than 1.

 

Point-09:

 

  • Other names by which wait operation may be referred : Down operation, P operation.
  • Other names by which signal operation may be referred : Up operation, V operation, Release operation.

 

To gain better understanding about Counting Semaphores,

Watch this Video Lecture

 

Next Article- Practice Problems On Counting Semaphores

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Contiguous Memory Allocation | Practice Problems

Contiguous Memory Allocation-

 

Before you go through this article, make sure that you have gone through the previous articles on Static Partitioning and Dynamic Partitioning.

 

There are two popular techniques used for contiguous memory allocation-

 

 

In this article, we will discuss practice problems based on Contiguous Memory Allocation.

 

PRACTICE PROBLEMS BASED ON CONTIGUOUS MEMORY ALLOCATION-

 

Problem-01:

 

Consider six memory partitions of size 200 KB, 400 KB, 600 KB, 500 KB, 300 KB and 250 KB. These partitions need to be allocated to four processes of sizes 357 KB, 210 KB, 468 KB and 491 KB in that order.

Perform the allocation of processes using-

  1. First Fit Algorithm
  2. Best Fit Algorithm
  3. Worst Fit Algorithm

 

Solution-

 

According to question,

The main memory has been divided into fixed size partitions as-

 

 

Let us say the given processes are-

  • Process P1 = 357 KB
  • Process P2 = 210 KB
  • Process P3 = 468 KB
  • Process P4 = 491 KB

 

Allocation Using First Fit Algorithm-

 

In First Fit Algorithm,

  • Algorithm starts scanning the partitions serially.
  • When a partition big enough to store the process is found, it allocates that partition to the process.

 

The allocation of partitions to the given processes is shown below-

 

Step-01:

 

 

Step-02:

 

 

Step-03:

 

 

Step-04:

 

  • Process P4 can not be allocated the memory.
  • This is because no partition of size greater than or equal to the size of process P4 is available.

 

Allocation Using Best Fit Algorithm-

 

In Best Fit Algorithm,

  • Algorithm first scans all the partitions.
  • It then allocates the partition of smallest size that can store the process.

 

The allocation of partitions to the given processes is shown below-

 

Step-01:

 

 

Step-02:

 

 

Step-03:

 

 

Step-04:

 

 

Allocation Using Worst Fit Algorithm-

 

In Worst Fit Algorithm,

  • Algorithm first scans all the partitions.
  • It then allocates the partition of largest size to the process.

 

The allocation of partitions to the given processes is shown below-

 

Step-01:

 

 

Step-02:

 

 

Step-03:

 

  • Process P3 and Process P4 can not be allocated the memory.
  • This is because no partition of size greater than or equal to the size of process P3 and process P4 is available.

 

To watch video solution, click here.

 

Problem-02:

 

Consider the following heap (figure) in which blank regions are not in use and hatched regions are in use-

 

 

The sequence of requests for blocks of size 300, 25, 125, 50 can be satisfied if we use-

  1. Either first fit or best fit policy (any one)
  2. First fit but not best fit policy
  3. Best fit but not first fit policy
  4. None of the above

 

Solution-

 

The allocation follows variable size partitioning scheme.

 

Let us say the given processes are-

  • Process P1 = 300 units
  • Process P2 = 25 units
  • Process P3 = 125 units
  • Process P4 = 50 units

 

Allocation Using First Fit Algorithm-

 

The allocation of partitions to the given processes is shown below-

 

Step-01:

 

 

Step-02:

 

 

Step-03:

 

 

Step-04:

 

 

Allocation Using Best Fit Algorithm-

 

The allocation of partitions to the given processes is shown below-

 

Step-01:

 

 

Step-02:

 

 

Step-03:

 

 

Step-04:

 

  • Process P4 can not be allocated the memory.
  • This is because no partition of size greater than or equal to the size of process P4 is available.

 

Thus,

  • Only first fit allocation policy succeeds in allocating memory to all the processes.
  • Option (B) is correct.

 

To watch video solution, click here.

 

Next Article- Introduction to Paging

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Contiguous Memory Allocation | Dynamic Partitioning

Contiguous Memory Allocation-

 

Before you go through this article, make sure that you have gone through the previous article on Contiguous Memory Allocation.

 

We have discussed-

  • In contiguous memory allocation, a process can be stored only in a contiguous fashion.
  • There are two popular techniques used for contiguous memory allocation-

 

 

In this article, we will discuss about Dynamic Partitioning.

 

Dynamic Partitioning-

 

  • Dynamic partitioning is a variable size partitioning scheme.
  • It performs the allocation dynamically.
  • When a process arrives, a partition of size equal to the size of process is created.
  • Then, that partition is allocated to the process.

 

Partition Allocation Algorithms-

 

  • The processes arrive and leave the main memory.
  • As a result, holes of different size are created in the main memory.
  • These holes are allocated to the processes that arrive in future.

 

Partition allocation algorithms are used to decide which hole should be allocated to the arrived process.

 

Popular partition allocation algorithms are-

 

 

  1. First Fit Algorithm
  2. Best Fit Algorithm
  3. Worst Fit Algorithm

 

We have already discussed these algorithms in our previous article.

 

Important Points-

 

Point-01:

 

For dynamic partitioning,

  • Worst Fit Algorithm works best.
  • This is because space left after allocation inside the partition is of large size.
  • There is a high probability that this space might suit the requirement of arriving processes.

 

Point-02:

 

For dynamic partitioning,

  • Best Fit Algorithm works worst.
  • This is because space left after allocation inside the partition is of very small size.
  • There is a low probability that this space might suit the requirement of arriving processes.

 

Translating Logical Address into Physical Address-

 

The following diagram illustrates the steps of translating logical address into physical address-

 

 

The steps are same as we have discussed in our previous article.

 

Advantages-

 

The advantages of dynamic partitioning are-

  • It does not suffer from internal fragmentation.
  • Degree of multiprogramming is dynamic.
  • There is no limitation on the size of processes.

 

Disadvantages-

 

The disadvantages of dynamic partitioning are-

  • It suffers from external fragmentation.
  • Allocation and deallocation of memory is complex.

 

To gain better understanding about Contiguous Memory Allocation,

Watch this Video Lecture

 

Next Article- Practice Problems On Contiguous Memory Allocation

 

Get more notes and other study material of Operating System.

Watch video lectures by visiting our YouTube channel LearnVidFun.