SystemVerilog UNIT-2 and
UNIT-3 part2
Fork Join processes
Dr. Sowmya K B
Deptartment of ECE
RVCE, Bengaluru
Dr. Sowmya K B 1
SystemVerilog Fork Join
fork-Join will start all the processes inside it parallel'y and wait for the
completion of all the processes.
Dr. Sowmya K B 2
SystemVerilog Fork Join
fork-Join will start all the processes inside it parallel'y and wait for the
completion of all the processes.
Dr. Sowmya K B 3
module fork_join;
initial begin
Example 1:
$display("-----------------------------------------------------------------");
fork
begin
$display($time,"\tProcess-1 Started");
#5;
$display($time,"\tProcess-1 Finished");
end
begin
$display($time,"\tProcess-2 Started");
#20;
$display($time,"\tProcess-2 Finished");
end
join
$display($time,"\tOutside Fork-Join");
$display("-----------------------------------------------------------------");
$finish;
end
endmodule Dr. Sowmya K B 4
Output:
--------------------------------------------------------------
0 Process-1 Started
0 Process-2 Started
5 Process-1 Finished
20 Process-2 Finished
20 Outside Fork-Join
---------------------------------------------------------------
Dr. Sowmya K B 5
SystemVerilog fork join_any
Fork-Join_any will be un-blocked after the completion of any of the
Process. Dr. Sowmya K B 6
SystemVerilog fork join_any
Fork-Join_any will be un-blocked after the completion of any of the
Process. Dr. Sowmya K B 7
module fork_join;
Example 1:
initial begin
$display("-----------------------------------------");
fork
begin
$display($time,"\tProcess-1 Started");
#5;
$display($time,"\tProcess-1 Finished");
end
begin
$display($time,"\tProcess-2 Started");
#20;
$display($time,"\tProcess-2 Finished");
end
join_any
$display($time,"\tOutside Fork-Join");
$display("-----------------------------------------------------------------");
end
endmodule
Dr. Sowmya K B 8
Output:
--------------------------------------------------------
0 Process-1 Started
0 Process-2 Started
5 Process-1 Finished
5 Outside Fork-Join
----------------------------------------------------------------
20 Process-2 Finished
Dr. Sowmya K B 9
SystemVerilog fork join_none
Processes inside the fork-join_none block will be started at the same time,
fork block will not wait for the completion of Process inside the fork-
join_none.
Dr. Sowmya K B 10
Example 1:
module fork_join_none;
initial begin
$display("-----------------------------------------------------------------");
fork
begin
$display($time,"\tProcess-1 Started");
#5;
$display($time,"\tProcess-1 Finished");
end
begin
$display($time,"\tProcess-2 Startedt");
#20;
$display($time,"\tProcess-2 Finished");
end
join_none
$display($time,"\tOutside Fork-Join_none");
$display("-----------------------------------------------------------------");
end Dr. Sowmya K B 11
Output:
----------------------------------------------------------------
0 Outside Fork-Join_none
-----------------------------------------------------------------
0 Process-1 Started
0 Process-2 Started
5 Process-1 Finished
20 Process-2 Finished
Dr. Sowmya K B 12
Dr. Sowmya K B 13
Dr. Sowmya K B 14
Dr. Sowmya K B 15
Dr. Sowmya K B 16
Dr. Sowmya K B 17
wait fork
wait fork; causes process to block until the
completion of all processes started from fork
blocks.
Dr. Sowmya K B 18
• wait fork example
• In the below example,
• after the completion of Process-1 (i.e, after
5ns) fork-join_any will get unblocked, the
$finish will get called and it ends the
simulation.
The simulation will get ended in the middle
of the execution of process-2, this can be
avoided with the use of wait-fork.
• The problem in this example is overcome in
example-2 with the use of wait fork;
Dr. Sowmya K B 19
module no_wait_fork;
initial begin
$display("-----------------------------------------------------------------");
fork
//Process-1
begin
$display($time,"\tProcess-1 Started");
#5;
$display($time,"\tProcess-1 Finished");
end
//Process-2
begin
$display($time,"\tProcess-2 Started");
#20;
$display($time,"\tProcess-2 Finished");
end
join_any
$display("-----------------------------------------------------------------");
$finish; //ends the simulation
end
endmodule
Dr. Sowmya K B 20
Simulator output:
-----------------------------------------------------------
0 Process-1 Started
0 Process-2 Started
5 Process-1 Finished
-----------------------------------------------------------
Dr. Sowmya K B 21
In the below example, wait fork will wait for the completion of
second thread in the fork-join_any.
module wait_fork;
initial begin
$display("-----------------------------------------------------------------");
fork
//Process-1
begin
$display($time,"\tProcess-1 Started");
#5;
$display($time,"\tProcess-1 Finished");
end
//Process-2
begin
$display($time,"\tProcess-2 Started");
#20;
$display($time,"\tProcess-2 Finished");
end
join_any
wait fork; //waiting for the completion of active fork threads
$display("-----------------------------------------------------------------");
$finish; //ends the simulation Dr. Sowmya K B 22
Simulator output:
-----------------------------------------------------------------
0 Process-1 Started
0 Process-2 Started
5 Process-1 Finished
20 Process-2 Finished
-----------------------------------------------------------------
Dr. Sowmya K B 23
disable fork
• disable fork; causes process to kill/terminate
all the active processes started from fork
blocks.
Dr. Sowmya K B 24
Example: disable fork
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
Dr. Sowmya K B 25
//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("-----------------------------------------------------------------");
Dr. Sowmya K B 26
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
-----------------------------------------------------------------
Dr. Sowmya K B 27