ALL > Computer and Education > courses > university courses > undergraduate courses > Operating System > ZSTU-(2020-2021)-1 > student homework > 2018329621232_沈宇帆 >
homework6_沈宇帆_2018329621232 Version 0
👤 Author: by 512858048qqcom 2020-11-09 03:23:59
Semaphores are integer variables that are used to solve the critical section problem by using two atomic operations, wait and signal that are used for process synchronization.

The definitions of wait and signal are as follows −

  • WaitThe wait operation decrements the value of its argument S, if it is positive. If S is negative or zero, then no operation is performed.


wait(S)
{
   
while (S<=0);

   S
--;
}


  • SignalThe signal operation increments the value of its argument S.


signal(S)
{
   S
++;
}

Types of Semaphores

There are two main types of semaphores i.e. counting semaphores and binary semaphores. Details about these are given as follows 1. Counting Semaphores

These are integer value semaphores and have an unrestricted value domain. These semaphores are used to coordinate the resource access, where the semaphore count is the number of available resources. If the resources are added, semaphore count automatically incremented and if the resources are removed, the count is decremented.

2.Binary Semaphores

The binary semaphores are like counting semaphores but their value is restricted to 0 and 1. The wait operation only works when the semaphore is 1 and the signal operation succeeds when semaphore is 0. It is sometimes easier to implement binary semaphores than counting semaphores.

Advantages of Semaphores

Some of the advantages of semaphores are as follows

1.Semaphores allow only one process into the critical section. They follow the mutual exclusion principle strictly and are much more efficient than some other methods of synchronization.

2.There is no resource wastage because of busy waiting in semaphores as processor time is not wasted unnecessarily to check if a condition is fulfilled to allow a process to access the critical section

3.Semaphores are implemented in the machine independent code of the microkernel. So they are machine independent.

Disadvantages of Semaphores

Some of the disadvantages of semaphores are as follows

  1. Semaphores are complicated so the wait and signal operations must be implemented in the correct order to prevent deadlocks

  2. Semaphores are impractical for last scale use as their use leads to loss of modularity. This happens because the wait and signal operations prevent the creation of a structured layout for the system.

  3. Semaphores may lead to a priority inversion where low priority processes may access the critical section first and high priority processes later.


 

Example


 

Problem

There are five philosophers sitting on a round table, and a chopstick is placed on the table between every two philosophers. In the middle of the table is a bowl of rice, as shown in Figure 2-10. Philosophers devote their entire lives to thinking and eating, and philosophers do not influence others when thinking. Only when the philosopher was hungry did he try to pick up the left and right chopsticks (one by one). If the chopsticks are already in the hands of others, you need to wait. The hungry philosopher can only start eating when he has two chopsticks at the same time. When the meal is over, he puts down the chopsticks and continues thinking.

Solution

The philosophers are numbered in order from 0 to 4. The chopsticks on the left of philosopher i are numbered i, and the chopsticks on the right of philosopher i are numbered (i+l)%5.

  1. semaphore chopstick[5] = {1,1,1,1,1}; //定义信号量数组

  2. Pi(){

  3. do{

  4. P (chopstick[i] ) ; //取左边

  5. P (chopstick[(i+1) %5] )//取右边

  6. eat; //进餐

  7. V(chopstick[i]) ; //放回左边

  8. V(chopstick[(i+l)%5]); //放回右边

  9. think; //思考

  10. } while (1);

  11. }

Please login to reply. Login

Reversion History

Loading...
No reversions found.