Saturday 2 September 2017

wait fork in system verilog

In System Verilog, fork .. join keyword is used for parallel processing. To control these parallel processes, various constructs are provided in the language. One of them is "wait .. fork" and its usage is described in below example.

"wait .. fork" is used to block the execution until all sub-processes (processes created by current process) are completed.

Below is an example in which 2 parallel processes are going on. First process A require 100 time duration and another process B needs 50 time duration. As "fork .. join_any" is used, the process B is completed at 50. To wait to completed other sub-processes (process A), "wait fork" is used which blocks the execution until process A is also completed. It unblocks the execution after 100 time duration once process A and B. The output of Example-1 is shown exactly the same behavior. 

Example-1:
initial begin
    fork
        begin
            #100; // process A
        end
        begin
            #50; // process B
        end
    join_any
    $display($time," Waiting for fork completion.");
    wait fork;
    $display($time," Wait fork done.");
end

Output of Example-1:
50 Waiting for fork completion.
100 Wait fork done.

Here, if any process contains any other "fork .. join_none" or "fork .. join_any" sub-processes, then this "wait fork" statement will wait for completion of all the "fork" sub-processes. Below is the similar example as given above. In it, "fork .. join_none" contains process C which takes 200 time to complete. So the "wait fork" will take 200 time duration to unblock the entire process. Output of example-2 indicates the same behavior.  

Example-2:
initial begin
    fork
        #200; // process C
    join_none
    fork
        begin
            #100; // process A
        end
        begin
            #50; // process B
        end
    join_any
    $display($time," Waiting for fork completion.");
    wait fork;
    $display($time," Wait fork done.");
end

Output of Example-2:
 50 Waiting for fork completion.
 200 Wait fork done.