quick.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

   

space.gif

   

space.gif

  ../images/main/bulllet_4dots_orange.gif semaphore

Semaphore is used for lock/unlock of a common resource. If resource is in unlock state the resource can be used, if it is in lock state, then it can not be used.

   

space.gif

Lets assume that in a Verification env, we have single ethernet port/driver and there are 2 packet generators using this one ethernet port, one generating normal frames and other generating flow control (pause) frames.

   

space.gif

So each of this two packet generators can use semaphone to if ethenet driver is free (unlock) state, before it can transmit the frame. So if the driver is free, then it locks the drivers and transmits the frame, once tramitted, it unlocks the drivers.

   

space.gif

When driver is in lock state, it will wait till driver is in unlock state.

   

space.gif

Semaphore in Vera supports following methods for above operations.

   

space.gif

  • Semaphore allocation
  • Using semaphore keys
  • Returing semaphore keys.
   

space.gif

Semaphore Allocation

   

space.gif

function integer alloc(SEMAPHORE, integer semaphore_id,
integer semaphore_count, integer key_count);
   

space.gif

Where

   

space.gif

  • semaphore_id : Is the ID number of the particular semaphore being created. It must be an integer value. You should generally use 0. When you use 0, a semaphore ID is automatically generated by the simulator. Using any other number explicitly assigns an ID to the semaphore being created.
  • semaphore_count : Specifies how many semaphore buckets you want to create. It must be an integer value.
  • key_count : Specifies the number of keys initially allocated to each semaphore bucket you are creating.
   

space.gif

Using semaphore keys

   

space.gif

function integer semaphore_get(NO_WAIT | WAIT,
integer semaphore_id, integer key_count);
   

space.gif

Where

   

space.gif

  • NO_WAIT : Continues code execution even if there are not enough keys available.
  • WAIT : Suspends the process until there are enough keys available, at which time execution continues.
  • semaphore_id : Specifies which semaphore to get keys from.
  • key_count : Specifies the number of keys being taken from the semaphore.
   

space.gif

If there are enough keys available, a 1 is returned and execution continues. If there are not enough keys available, a 0 is returned and the process is suspended depending on the wait option.

   

space.gif

Returning keys

   

space.gif

task semaphore_put(integer semaphore_id, integer key_count);
   

space.gif

Where

   

space.gif

  • semaphore_id : Specifies which semaphore to return the keys to.
  • key_count : Specifies the number of keys being returned to the semaphore.
   

space.gif

   

space.gif

  ../images/main/bullet_star_pink.gif Example : semaphore
   

space.gif


  1 program semaphore {
  2   // Declare a integer, which will be used for semaphore
  3   integer  bus;
  4   // Alloc it as semaphore, with 1 lock
  5   bus =  alloc(SEMAPHORE,0,1,1);
  6   fork 
  7     { agent("AGENT 0",5);}
  8     { agent("AGENT 1",20);}
  9   join all
 10 
 11 }
 12 
 13 task agent(string name, integer wait) {
 14   integer i = 0;
 15   for (i = 0 ; i < 4; i ++ ) {
 16     // Check if bus is avaible
 17     if (semaphore_get(WAIT,bus,1) == 1) {
 18       printf("[%0d] Lock bus for %s\n", get_time(LO),name);
 19       delay(wait);
 20       printf("[%0d] Release bus for %s\n", get_time(LO),name);
 21       semaphore_put(bus,1);
 22       delay(wait);
 23     }
 24   }
 25 }
You could download file semaphore.vr here
   

space.gif

  ../images/main/bullet_star_pink.gif Simulation : semaphore
   

space.gif

 [0] Lock bus for AGENT 0
 [5] Release bus for AGENT 0
 [5] Lock bus for AGENT 1
 [25] Release bus for AGENT 1
 [25] Lock bus for AGENT 0
 [30] Release bus for AGENT 0
 [35] Lock bus for AGENT 0
 [40] Release bus for AGENT 0
 [45] Lock bus for AGENT 1
 [65] Release bus for AGENT 1
 [65] Lock bus for AGENT 0
 [70] Release bus for AGENT 0
 [85] Lock bus for AGENT 1
 [105] Release bus for AGENT 1
 [125] Lock bus for AGENT 1
 [145] Release bus for AGENT 1
   

space.gif

   

space.gif

   

space.gif

   

space.gif

space2.gif

space2.gif

space2.gif

space2.gif

space2.gif

  

Copyright © 1998-2014

Deepak Kumar Tala - All rights reserved

Do you have any Comment? mail me at:deepak@asic-world.com