SystemVerilog Semaphore example

Semaphore Examples

Semaphore access with 2 keys

In the example below,
Creating semaphore with ‘4’ keys. ‘2’ keys are required to get access to the method.

At the same time, two processes will get access to the method and the other process will be blocked until the one other process puts the key.

module semaphore_ex;
  semaphore sema; //declaring semaphore sema

  initial begin
    sema=new(4); //creating sema with '4' keys
    fork
      display(); //process-1
      display(); //process-2
      display(); //process-3
    join
  end

  //display method
  task automatic display();
    sema.get(2); //getting '2' keys from sema
    $display($time,"\tCurrent Simulation Time");
    #30;
    sema.put(2); //putting '2' keys to sema
  endtask
endmodule

Simulator Output

0 Current Simulation Time
0 Current Simulation Time
30 Current Simulation Time

Click to execute on   

Putting back more keys

In the example below,
Creating semaphore with ‘1’ key, putting more number of keys back to the semaphore.

module semaphore_ex;
  semaphore sema; //declaring semaphore sema

  initial begin
    sema=new(1); //creating sema with '1' keys
    fork
      display(1); //process-1
      display(2); //process-2
      display(3); //process-3
    join
  end

  //display method
  task automatic display(int key);
    sema.get(key); //getting 'key' number of keys from sema
    $display($time,"\tCurrent Simulation Time, Got %0d keys",key);
    #30;
    sema.put(key+1); //putting 'key' number of keys to sema
  endtask
endmodule

Simulator Output

0 Current Simulation Time, Got 1 keys
30 Current Simulation Time, Got 2 keys
60 Current Simulation Time, Got 3 keys

Click to execute on   

the process with more than 1 key

In the example below, Creating a semaphore with the ‘4’ keys, the method will be blocked until it gets enough keys.

module semaphore_ex;
  semaphore sema; //declaring semaphore sema

  initial begin
    sema=new(4); //creating sema with '4' keys
    fork
      display(2); //process-1
      display(3); //process-2
      display(2); //process-3
      display(1); //process-4
    join
  end

  //display method
  task automatic display(int key);
    sema.get(key); //getting 'key' number of keys from sema
    $display($time,"\tCurrent Simulation Time, Got %0d keys",key);
    #30;
    sema.put(key); //putting 'key' number of keys to sema
  endtask
endmodule

Simulator Output

0 Current Simulation Time, Got 2 keys
0 Current Simulation Time, Got 2 keys
30 Current Simulation Time, Got 1 keys
30 Current Simulation Time, Got 3 keys

Click to execute on   

using try_get

In the example below,
Creating semaphore with ‘4’ key, try_get() will check for the keys if the keys are not available simulation will proceed(non-blocking).

module semaphore_ex;
  semaphore sema; //declaring semaphore sema

  initial begin
    sema=new(4); //creating sema with '4' keys
    fork
      display(4); //process-1
      display(4); //process-2
    join
  end

  //display method
  task automatic display(int key);
    sema.try_get(key); //getting 'key' number of keys from sema
    $display($time,"\tCurrent Simulation Time, Got %0d keys",key);
    #30;
    sema.put(key); //putting 'key' number of keys to sema
  endtask
endmodule

Simulator Output

0 Current Simulation Time, Got 4 keys
0 Current Simulation Time, Got 4 keys

Click to execute on   

❮ Previous Next ❯