systemverilog学习 ---- 进程

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

其输出结果为:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值