# Semaphore

**URL:** <https://verificationacademy.com/forums/t/semaphore/38242>\
**Category:** SystemVerilog\
**Tags:** semaphore-keys, SystemVerilog\
**Created:** [October 19, 2020, 4:41am UTC](https://verificationacademy.com/forums/t/semaphore/38242 "2020-10-19T04:41:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![anveshdangeti](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/8baadc/32.png) [@anveshdangeti](https://verificationacademy.com/forums/u/anveshdangeti)\
**Post date:** [October 19, 2020, 4:41am UTC](https://verificationacademy.com/forums/t/semaphore/38242/1 "2020-10-19T04:41:53Z")

</div>

```verilog
when i am creating the semaphore with negative number i.e. sema = new(-1).what will happened?
the below is the example:
module semaphore_ex;
  semaphore sema; //declaring semaphore sema
  
  initial begin
    sema=new(-1); //creating sema with '-1' key
    fork
      display("process-1"); //process-1
      display("process-2"); //process-2
    join
  end
  
  //display method
  task automatic display(string name);
    sema.get(); //getting '1' key from sema
    $display("[%0d] Lock get semBus for %s", $time,name);
    #30;
    sema.put(); //putting '1' key to sema
    $display("[%0d] Lock put semBus for %s", $time,name);
    
  endtask
endmodule
output:
[0] Lock get semBus for process-1
[0] Lock get semBus for process-2
[30] Lock put semBus for process-1
[30] Lock put semBus for process-2
why the output is happened like that? Explain me?

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 19, 2020, 4:21pm UTC](https://verificationacademy.com/forums/t/semaphore/38242/2 "2020-10-19T16:21:12Z")

</div>

_In reply to [anvesh dangeti](https://verificationacademy.com/forums/t/semaphore/38242):_

I didn’t think it was possible, but the four commercial simulators on [EDAPlayground.com](http://EDAPlayground.com) each exhibit a different behavior if the new() argument is negative:

1. Throws an error and exits.
2. Throws a warning and sets the value to 0.
3. Treats the argument as a signed integer and waits for a positive number of keys for the get() to proceed.
4. Treats the argument as an unsigned integer, which is the results you are seeing: 232-1 keys

I believe 3) matches closest to what the LRM currently specifies, but 1) should probably be made the default behavior. I submitted a [clarification request](https://accellera.mantishub.io/view.php?id=7233) to the IEEE SystemVerilog committee.

---

<div class="post-metadata">

**Author:** ![ABD\_91](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/c77e96/32.png) [@ABD\_91](https://verificationacademy.com/forums/u/ABD_91)\
**Post date:** [October 19, 2020, 6:19pm UTC](https://verificationacademy.com/forums/t/semaphore/38242/3 "2020-10-19T18:19:10Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/semaphore/38242/2):_

Hi Dave ,

**Shouldn’t the description/behavior match that of a mailbox new() with Negative Argument ?  
i.e Combination of 2 and 3**

```verilog

LRM 15.4.1 :: The bound shall be positive. Negative bounds are illegal and can result in indeterminate behavior, but implementations can issue a warning.

```

All the licensed ones I observe use the default value of 0 for the mailbox ( i.e Unbounded )

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 19, 2020, 7:08pm UTC](https://verificationacademy.com/forums/t/semaphore/38242/4 "2020-10-19T19:08:03Z")

</div>

_In reply to [ABD\_91](https://verificationacademy.com/forums/t/semaphore/38242/3):_

It could but it does not currently. Mailbox argument is slightly different.
