Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System Verilog fork join - Not actually parallel?

I am learning system verilog and thought separate threads are created for each process in fork/join. But, I find if I have a while loop in my first process, my second process does not start, which makes me think fork/join is not actually parallel.

class A;

task run();
$display("A started");
while(1);
endtask

endclass

class B;

task run();
$display("B started");
endtask

endclass

class C;

task run();
fork
   A.run();
   B.run(); 
join
endtask

endclass

My output is

 Class A started 

and the program is running forever. I thought it should print

 Class A started
 Class B started

and run forever. Could someone point out what I am missing here?

like image 599
Raghav Avatar asked Nov 30 '25 03:11

Raghav


2 Answers

SystemVerilog fork/join statement creates processes that are parallel only in the simulation sense. But, the processes are not parallel in the multicore sense -- the processes will not be executed on multiple threads of a processor. These processes would be scheduled on the execution queue at the same simulation time stamp. But, these would be executed on a single logical CPU, and therefore, from the CPU perspective, they would be executed sequentially.

In your code example, both class A and B run tasks are scheduled to be executed at the same simulation time. When the SystemVerilog simulator comes across the fork/join statement, it puts them on the execution queue. When you ran the simulation, your simulator started A.run first. And since A.run process goes into an infinite loop that does not yield, the simulator did not get a chance to execute B.run. Note that if you have multiple tasks scheduled at the same simulation time, the SystemVerilog LRM does not dictate which task would be executed first. Some other simulator might have executed B.run before starting A.run.

like image 54
Puneet Goel Avatar answered Dec 02 '25 16:12

Puneet Goel


You can put #0 delay in while(1), to let the second take its turn:

task run();
$display("A started");
while(1) #0;
endtask
like image 28
AldoT Avatar answered Dec 02 '25 16:12

AldoT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!