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.
Please help me out in this.
-Thanks
In reply to deepi202:
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.
In reply to dave_59:
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.
For second approach, I am looking for a solution.
It will be better if you can help me.
-Thanks
There are 2 problems:
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.