Nested Forever loop

I am facing an unexpected behavior, I have a need to use a nested forever loop (can be forever + while(1)) which is used in a monitor block (1st forever in monitor which call a task and inside task the fork join_any and forever/while(1) loop combination is used) needed to capture the data for a specified time and after that, it should break out and look for 2nd start. But for the code which I wrote looks like the clock checks all starts to break down after a while (maybe the problem is w.r.t my understanding of the Simulator). Please help me understand and possibly fix the issue. I have written a code below that shows this behavior. In the code, I have tried other combinations like splitting forever loop, adding edge checks etc(put as comments) but the same behavior in all cases.


module forever_loop;

  int a;
  int b;
  int c;
  int d;
  logic clk = 0;
  
    initial begin // clock generation
      forever  #10clk = ~clk; 
    end
  
  
initial begin

//    $display("-----------------------------------------------------------------");
  forever begin 
    d++;
    $display("\t%t, Value of d=%0d ",$realtime, d);
//      b <= a;
//      c <= b;
    @(posedge clk iff (d!=0));
	
      fork
      
        forever begin
//        forever @(posedge clk) begin
          $display("\t%t, Value of a=%0d,c=%0d, b = %0d ",$realtime, a, c, b);
          a <= a+1;
          b <= a;
          c <= b;
//        @ (posedge clk);
//        end
    
//        forever begin
          $display("\t%t, Value of a=%0d",$realtime, a);
          if (c != b)
          $display("\t%t, Value of c=%0d, b = %0d",$realtime, c, b);
          @(posedge clk);
        end
      
        begin
         #10000ns;
         $display("\t%t, Expired current capture",$realtime);
        end
    join_any
  end 
//    $display("-----------------------------------------------------------------");
  end
  
//  initial begin
//    #500000 $finish;
//  end      
endmodule

Output:

  1. At begning of simulation

vsim -voptargs=+acc=npr

run -all

0, Value of d=1

10, Value of a=0,c=0, b = 0

10, Value of a=0

30, Value of a=1,c=0, b = 0

30, Value of a=1

50, Value of a=2,c=0, b = 1

50, Value of a=2

50, Value of c=0, b = 1

70, Value of a=3,c=1, b = 2

70, Value of a=3

70, Value of c=1, b = 2

90, Value of a=4,c=2, b = 3

90, Value of a=4

90, Value of c=2, b = 3

110, Value of a=5,c=3, b = 4

  1. After entering 2nd time the Foreverloop

10010, Expired current capture

10010, Value of d=2

10010, Value of a=500,c=498, b = 499

10010, Value of a=500

10010, Value of c=498, b = 499

10010, Value of a=500,c=498, b = 499

10010, Value of a=500

10010, Value of c=498, b = 499

10030, Value of a=501,c=499, b = 500

10030, Value of a=501

10030, Value of c=499, b = 500

10030, Value of a=501,c=499, b = 500

10030, Value of a=501

10030, Value of c=499, b = 500

10050, Value of a=502,c=500, b = 501

10050, Value of a=502

10050, Value of c=500, b = 501

10050, Value of a=502,c=500, b = 501

10050, Value of a=502

10050, Value of c=500, b = 501

10070, Value of a=503,c=501, b = 502

  1. After re-entring 3rd time in the forever loop

19990, Value of c=997, b = 998

20010, Expired current capture

20010, Value of d=3

20010, Value of a=1000,c=998, b = 999

20010, Value of a=1000

20010, Value of c=998, b = 999

20010, Value of a=1000,c=998, b = 999

20010, Value of a=1000

20010, Value of c=998, b = 999

20010, Value of a=1000,c=998, b = 999

20010, Value of a=1000

20010, Value of c=998, b = 999

20030, Value of a=1001,c=999, b = 1000

20030, Value of a=1001

20030, Value of c=999, b = 1000

20030, Value of a=1001,c=999, b = 1000

20030, Value of a=1001

20030, Value of c=999, b = 1000

20030, Value of a=1001,c=999, b = 1000

20030, Value of a=1001

20030, Value of c=999, b = 1000

20050, Value of a=1002,c=1000, b = 1001

20050, Value of a=1002

20050, Value of c=1000, b = 1001

20050, Value of a=1002,c=1000, b = 1001

20050, Value of a=1002

20050, Value of c=1000, b = 1001

20050, Value of a=1002,c=1000, b = 1001

20050, Value of a=1002

20050, Value of c=1000, b = 1001

20070, Value of a=1003,c=1001, b = 1002

In reply to varunmr:

I’m not exactly sure what your probem is, but perhaps you meant to put in a disable fork after the join_any.

It would help if you showed the code exactly the way you ran it deleting the commented out statements—they are distracting. Also shorten the time out so we can quickly get the output and and you can show the difference between what is actually shown versus what you expected to see.