Semaphore using 2 keys and 3 keys


module semaphore_ex;
  semaphore sema; //declaring semaphore sema
  
  initial begin
    sema=new(4); //creating sema with '4' keys 
    fork
      display("Process 1"); //process-1
      display("Process 2"); //process-2
      display("Process 3"); //process-3
      display("Process 4"); //process-4
    join
  end
  
  //display method
  task automatic display(string msg);
    sema.get(2); //getting '2' keys from sema
    $display($time,"\tGot the key for %s",msg);
   // else
     // $display("\t No key ");
    #30;
    sema.put(2); //putting '2' keys to sema
    $display($time,"\tPutting back the key by %s",msg);
  endtask
endmodule

RESULT: for sema.get(2)
                   0	Got the key for Process 1
                   0	Got the key for Process 2
                  30	Putting back the key by Process 1
                  30	Putting back the key by Process 2
                  30	Got the key for Process 3
                  30	Got the key for Process 4
                  60	Putting back the key by Process 3
                  60	Putting back the key by Process 4
           V C S   S i m u l a t i o n   R e p o r t

THis is the result when you use sema.get(2), But when we run the same program with sema.get(3). The processes 1,2 and 3 are expected to run simultaneously but we get a different result. Why is that?

RESULT: For sema.get(3)
                   0	Got the key for Process 1
                  30	Putting back the key by Process 1
                  30	Got the key for Process 2
                  60	Putting back the key by Process 2
                  60	Got the key for Process 3
                  90	Putting back the key by Process 3
                  90	Got the key for Process 4
                 120	Putting back the key by Process 4
           V C S   S i m u l a t i o n   R e p o r t

Is there a way to know the number of keys available which are not being used?
Or to know how many keys are remaining and can be used for other processes to be used?

In reply to Shruti Kamble:

I assume you also changed sema.put(2) to (3).

If there are initially 4 keys, and each process needs 3 available keys, then only one process can run at a time.

Also see Semaphore understanding | Verification Academy

In reply to dave_59:

Hi Dave ,
Thank you for the reply.
It did solve my doubt.

I have another question regarding the keys.
Is there a way to know how many keys are being used apart from the statement sema.get(3);Is there a function to print the value of the keys in use and those that are not in use?

In reply to Shruti Kamble:
No. get() and put() are atomic test and set operations. The results from any other method could be invalid as soon as they are returned.

It’s up to you how you manage the total number of keys available. A semaphore is a resource manager, and there are many ways to manage the number of resources over time.

In reply to dave_59:

In reply to Shruti Kamble:
No. get() and put() are atomic test and set operations. The results from any other method could be invalid as soon as they are returned.

Thank you dave … :)

In reply to dave_59:

Hello Dave,
Thanks you for the replay. But I couldn’t understand why each process needs 3 available keys?
To be clear: it created 4 keys then at a time it can get 3 keys. And put it back.

Can you please clear the doubt ?

Thank you in advance.

In reply to Himanshu Patra:

The example is contrived. You can manage the keys as needed. For example, keys could represent blocks of memory available, and the process requests a number of blocks in terms of keys.