Wednesday, 23 November 2016

Wait for more than one processes of fork...join_none to complete

In our project some time we want to wait for more than one process which is invoked from fork...join_none or join_any before proceeding further.

How to achieve this using SystemVerilog constructs, that we will understand through an example.

Let's go through below code,
-------------------------------------------------------------------------------------
class abc;
semaphore s;
function new();
s = new(0); // Initialized with zero key
endfunction : new
task multiple_process();
fork
begin : process1
#5;
$display ($time," process1");
s.put(1);
end
begin
#10;
$display ($time," process2");
s.put(1);
end
begin
#15;
$display ($time," process3");
s.put(1);
end
begin
#12;
$display ($time," process4");
s.put(1);
end
join_none // You can use join_any also
s.get(2); // wait for any two process to be completed
disable fork; // kills remaining process
s = null; // removes semaphore (if you plan to reuse s)
$display ($time," multiple_process completed");
endtask : multiple_process
endclass : abc
module top();
abc A;
initial begin
A = new();
A.multiple_process();
end
endmodule : top
//Output:
// 5 process1
// 10 process2
// 10 multiple_process completed

-------------------------------------------------------------------------------------

In above code, class abc is having one method named multiple_process().
In API, within fork...join_none 4 process are invoked in parallel.
Each process puts one key into semaphore just before it's completed (Initially semaphore doesn't have any key).

After fork...join_none, I am waiting for semaphore to get at least two key ().

No comments:

Post a Comment