SystemVerilog disable fork

disable fork

disable fork; causes the process to kill/terminate all the active processes started from fork blocks.

disable fork example

In the below example,

On execution of the disable fork, all the active process will get terminated.
Process-2 of fork-1, Process-1, and Process-2 of fork-2 will get terminated.

module disable_fork;

  initial begin
    $display("-----------------------------------------------------------------");

    //fork-1
    fork
      //Process-1
      begin
        $display($time,"\tProcess-1 of fork-1 Started");
        #5;
        $display($time,"\tProcess-1 of fork-1 Finished");
      end
      
      //Process-2
      begin
        $display($time,"\tProcess-2 of fork-1 Started");
        #20;
        $display($time,"\tProcess-2 of fork-1 Finished");
      end
    join_any

    //fork-2
    fork
      //Process-1
      begin
        $display($time,"\tProcess-1 of fork-2 Started");
        #5;
        $display($time,"\tProcess-1 of fork-2 Finished");
      end
      //Process-2
      begin
        $display($time,"\tProcess-2 of fork-2 Started");
        #20;
        $display($time,"\tProcess-2 of fork-2 Finished");
      end
    join_none    

    disable fork;
    
    $display("-----------------------------------------------------------------");
      $display($time,"\tAfter disable-fork");
    $display("-----------------------------------------------------------------");
  end
endmodule

Simulator Output

-----------------------------------------------------------------
0 Process-1 of fork-1 Started
0 Process-2 of fork-1 Started
5 Process-1 of fork-1 Finished
-----------------------------------------------------------------
5 After disable-fork
-----------------------------------------------------------------

Click to execute on

 Example-2

In the below example,

sub_process started from process-2 will get terminated during the execution of disable fork.

module disable_fork;
  initial begin
    $display("-----------------------------------------------------------------");

    fork
      //Process-1
      begin
        $display($time,"\tProcess-1 of fork-1 Started");
        #5;
        $display($time,"\tProcess-1 of fork-1 Finished");
      end
      //Process-2
      begin
        sub_process();
      end
    join_any   
    disable fork;
  
    $display("-----------------------------------------------------------------");
    $display($time,"\tAfter disable-fork");
    $display("-----------------------------------------------------------------");
  end
  //Sub-Process
  task sub_process;
    $display($time,"\tSub-Process Started");
    #10;
    $display($time,"\tSub-Process Finished");
  endtask   
endmodule

Simulator Output

-----------------------------------------------------------------
0 Process-1 of fork-1 Started
0 Sub-Process Started
5 Process-1 of fork-1 Finished
-----------------------------------------------------------------
5 After disable-fork
-----------------------------------------------------------------

Click to execute on

❮ Previous Next ❯