fork join
fork join语句可以多个线程同时并发执行,最后要等待所有的线程完成。如下图所示:
只有当进程3执行完成后,才会跳转到statement-3执行。
module fork_join;
initial begin
$display("------------------------------------------");
fork
//process 1
begin
$display($time,"\t Process 1 Started");
#5;
$display($time,"\t Process 1 Finished");
end
//process 2
begin
$display($time, "\t Process 2 Started");
#20;
$display($time, "\t Process 2 Finished");
end
join
$display($time, "\t Outside Fork-Join");
$display("------------------------------------------");
$finish;
end
endmodule
其仿真输出结果为:
fork join_any
语句的并行执行,但是当其中任意一个进程结束后,都会执行fork join_any后面的语句。如下图示,当process -2 执行完成之后,就会执行statement - 3。但是没有执行完的进程还是会继续执行。
module fork_join;
initial begin
$display("------------------------------------------");
fork
//process 1
begin
$display($time,"\t Process 1 Started");
#5;
$display($time,"\t Process 1 Finished");
end
//process 2
begin
$display($time, "\t Process 2 Started");
#20;
$display($time, "\t Process 2 Finished");
end
join_any
$display($time, "\t Outside Fork-Join");
$display("------------------------------------------");
$finish;
end
endmodule
仿真输出结果是:
fork join_none
module fork_join_none;
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 Startedt");
#20;
$display($time,"\tProcess-2 Finished");
end
join_none
$display($time,"\tOutside Fork-Join_none");
$display("-----------------------------------------------------------------");
end
endmodule
其输出结果为:
0 Outside Fork-Join_none
-----------------------------------------------------------------
0 Process-1 Started
0 Process-2 Startedt
5 Process-1 Finished
20 Process-2 Finished
wait fork
通过fork-join_any,我们执行完其中一个进程就能执行fork-join_any后面的语句,同时没完成的进程也会继续执行。问题是,当后面的语句出现如结束仿真的系统函数$finish时,很可能fork内部的进程没做完,就会结束仿真。
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
$display("-----------------------------------------------------------------");
$finish; //ends the simulation
end
endmodule
仿真结果如下:
-----------------------------------------------------------------
0 Process-1 Started
0 Process-2 Started
5 Process-1 Finished
-----------------------------------------------------------------
这应该避免,为了解决这样的问题,我们可以使用wait fork。示例如下:
module wait_fork;
initial begin
$display("----------------------------------------------------------");
fork
//process 1
begin
$display($time, "\t Process-1 Started");
#5;
$display($time, "\t Process 1 Finished");
end
//process 2
begin
$display($time, "\t Process 2 Started");
#20;
$display($time, "\t Process 2 Finshed");
end
join_any
//wait fork; // waiting for the completion of active fork threads
$display("----------------------------------------------------------");
$finish; // ends the simulation
end
endmodule
其输出结果为:
disable fork
在fork的block中,杀死或者终止一些活跃的进程。
module disable_fork;
initial begin
$display("----------------------------------------------------------");
//fork-1
fork
//process-1
begin
$display($time,"\t Process-1 of fork-1 started");
#5;
$display($time,"\t Process-1 of fork-1 Finished");
end
//process-2
begin
$display($time, "\t Process-2 of fork-1 started");
#20;
$display($time, "\t Process-2 of fork-1 Finished");
end
join_any
//fork-2
fork
//process-1
begin
$display($time,"\t Process-1 of fork-2 started");
#5;
$display($time,"\t Process-1 of fork-2 Finished");
end
//process-2
begin
$display($time, "\t Process-2 of fork-2 started");
#20;
$display($time, "\t Process-2 of fork-2 Finished");
end
join_none
disable fork;
$display("----------------------------------------------------------");
$display($time,"\t After disable-fork");
$display("----------------------------------------------------------");
end
endmodule
其输出结果为: