module fork_join;
initial begin
fork begin
// ---Process 1--- //
begin
for(int j=1; j <=10; ++j)
begin
$display( $time,"\tProcess-1 Started and j = %0d", j);
end
end
// ---Process 2--- //
begin
$display($time,"\tProcess-2 Started");
#2;
$display($time,"\tProcess-2 Finished");
end
end join_any
$display($time,"\tOutside Fork-Join");
$display("-----------------------------------------------------------------");
end
endmodule
I wanted two things to be done for my code.
To generate all the display inside process 1 at different time
Also, if either of two process finishes first it should come out and disable the other process.
Lets say, if process 2 finishes first and by that time only 5 times the for loop got exectued, then it should stop rest of the thread and print “Outside Fork-Join”.
My current task is to check which one is happening first and then come out of fork disabling the other process.
You need to be more specific about what a different time means. If you put a #1 in front of the $display statement, then each time through the loop is 1 time unit.
You can put disable fork; after the join_any to kill off the other process.
Okay, forget about the fork join.
The logic I want to code is for below explanation.
I have two process, one is having “for” loop and another is delay.
For loop can go till any value, lets say for simplicity it is running for 1000 values and its taking 10 cycles to finish this.
And my delay is of 5 cycles only.
Now above this two, whichever is finishing early I want to come out and stop another and continue with the next statements.
So, there can be two possibilities only,
For loop can finish early and then it can come out and then we can disable the delay process. In this case we will 1000 times for loop is executing
Delay is coming early and by that time for loop is executed for 600 values only. In that case I want to stop the for loop as soon as delay comes.
a) you don’t have any delay in fork containing for loop. So it will finish in 0 time. If that fork is scheduled first, it’ll finish the for loop and then implement the fork with delay. So you need to add some delay in fork with for loop to see the behaviour listed in case 2 above.
b) Put a disable_fork after join_any to disable the other thread. Otherwise it’ll still be running.
Your original approach looked fine, you just had to put the threads in two separate begin ends, and use disable fork.
I have used an array to push data in the loop, so that we can actually see if the data remained as it is even after the disable fork
module testbench;
int arr [int];
initial begin
fork
begin : loop
for (int i = 0; i< 8; i++) begin
$display($time, "ns : Pushing i = %d in arr", i);
arr[i] = i;
#2;
// end
end
end
begin : timeout
#10;
$display($time, "ns : Timeout has occurred");
end
join_any
disable fork;
$display($time, "ns : Size of arr = %d", arr.size());
end
endmodule
Hi @chr_sue
The problem statement mentioned by @deepi202 specifically mentioned in point number 2 that
Delay is coming early and by that time for loop is executed for 600 values only. In that case I want to stop the for loop as soon as delay comes.
For that purpose I had added a disable fork. If you don’t disable it, the fork will be exited, but the loop thread will still run before the simulation ends.