# Confusion in fork join ... disable fork

**URL:** <https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [June 26, 2013, 6:12pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662 "2013-06-26T18:12:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnamikaSharma](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/ee59a6/32.png) [@AnamikaSharma](https://verificationacademy.com/forums/u/AnamikaSharma)\
**Post date:** [June 26, 2013, 6:12pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/1 "2013-06-26T18:12:25Z")

</div>

Hello ,  
I am unable to get the exact functionality of disable fork.  
Example ;

```auto
task t1();
  fork
    begin : a1
    #1;
    $display("a1"); 
    end
    begin: a2
    #2;
    $display("a2"); 
    end
  join_none
  fork
    begin : a3
    #5;
    $display("a3"); 
    end  
    begin : a4
    #6;
    $display("a4"); 
    end 
  join_any
  disable fork

  fork
    begin : a5
    #5;
    $display("a5"); 
    end  
    begin : a6
    #6;
    $display("a6"); 
    end 
  join_any
  endtask

```

Now will this disable fork will disable a1 and a2 also??? Will there be any output in this case??? And, What will happen to a5 and a6???

---

<div class="post-metadata">

**Author:** ![Sumit\_Jain](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/ba9def/32.png) [@Sumit\_Jain](https://verificationacademy.com/forums/u/Sumit_Jain)\
**Post date:** [June 26, 2013, 7:23pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/2 "2013-06-26T19:23:37Z")

</div>

Hi Anamika,  
disable fork statement terminates all active child processes, grandchild processes etc. where it is called.

No, disable fork shall terminate only a3 process because when it is called only a3 process is active.  
Both a5 and a6 process shall be continued because disable fork has been executed just before.

The outputs would be:-  
a1,  
a2,  
a3,  
a5,  
a6

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 26, 2013, 8:38pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/3 "2013-06-26T20:38:38Z")

</div>

It is best to start from the root thread, which is started by an **initial** or **always** block.  
module top;

```auto
initial begin : i1
           fork : f1
              #10 $display("f1")
           join_none
           t1();
        end
endmodule

```

The **disable fork** statements kills the children of the currently running thread. (i.e the thread executing the **disable fork** statement).

In this example, the parent thread is enclosed by the scope i1. It starts a child thread f1, then calls task t1. Inside task t1, the first fork starts threads a1 and a2, then the second fork starts a3 and a3. Since the second fork has join\_any, it waits for one of the threads to complete before executing the **disable fork** statement.

The **disable fork** will not execute until thread a3 completes at time 5, and only threads f1 and a4 are still active and will be killed. Had you used **join\_none** instead of **join\_any** , or used longer delays then the disable fork statement would have also killed threads a1, a2 and a3.

The threads a4 and a5 are forked after the disable fork, so it has no effect on those threads.

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 27, 2013, 2:44pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/5 "2013-06-27T14:44:11Z")

</div>

_In reply to [RameshRP](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/4):_

Yes, I said that.

---

<div class="post-metadata">

**Author:** ![AshishP](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@AshishP](https://verificationacademy.com/forums/u/AshishP)\
**Post date:** [June 29, 2013, 10:49pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/6 "2013-06-29T22:49:58Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/5):_

Hi Dave,  
In the example u mentioned above, thread f1 won’t get killed.  
Here is what IEEE 1800-2009 std, clause “9.6.3 Disable fork statement”, pg 174 says:

“The disable fork statement terminates all descendants of the calling process as well as  
================  
the descendants of the process’s descendants.”

Here is my understanding:  
Since calling process i.e. process which called “disable fork” is “t1”, all threads inside t1 and their descendants will get killed. Since thread f1 is not a descendant thread of t1 it should not get killed. I confirmed the same by simulating it w/ VCS too.

Thanks,  
Ashish

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [June 30, 2013, 12:19am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/7 "2013-06-30T00:19:06Z")

</div>

_In reply to [AshishP](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/6):_

Calling a task or function does not create a new process; only a **fork** can do that. In this case, t1 is a task activation, not new process. So process f1 and a1-a6 are all descendents of the same parent process.

---

<div class="post-metadata">

**Author:** ![AshishP](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@AshishP](https://verificationacademy.com/forums/u/AshishP)\
**Post date:** [July 1, 2013, 5:26pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/8 "2013-07-01T17:26:36Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/7):_

Hi Dave,  
Then is it a bug in VCS since it behaves the way it’s mentioned in comment #6 which doesn’t match w/ what you mentioned in comment #7?

Thanks,  
Ashish

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [July 1, 2013, 6:09pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/9 "2013-07-01T18:09:52Z")

</div>

_In reply to [AshishP](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/8):_

Since I work for Mentor, I can comment on Questa’s behavior, and as a member of the IEEE SystemVerilog 1800 committee, I can comment on my interpretation of the LRM, and whether it is correctly worded. In this case, Questa behavior matches my interpretation of the standard, and VCS does not.

---

<div class="post-metadata">

**Author:** ![AshishP](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/e9c0ed/32.png) [@AshishP](https://verificationacademy.com/forums/u/AshishP)\
**Post date:** [July 1, 2013, 7:34pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/10 "2013-07-01T19:34:16Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/9):_

Thanks, Dave.

---

<div class="post-metadata">

**Author:** ![Sailaja](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@Sailaja](https://verificationacademy.com/forums/u/Sailaja)\
**Post date:** [August 6, 2014, 6:29am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/11 "2014-08-06T06:29:00Z")

</div>

_In reply to [AshishP](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/10):_

Hi Dave,

On similar lines, can you explain what should be the ideal behavior for the below code (I was thinking disable fork should disable ff2, please clarify):

```verilog
class a_bus_monitor;
   bit my_event1;
   int iter;
 
   task my_monitor();
      fork : ft1
         forever begin:ff1
            #5ns;
            $display("Passed 5 ns now");
            iter++;

            if (iter == 20) begin
               $display("Breaking because iter is 20");
               break;
            end
         end

         forever begin:ff2
            @(my_event1);
            $display("At time %0t found an event on my_event1 (= %0d)",$time,my_event1);

            if (my_event1 == 0) begin
               $display("Disabling fork now");
               disable fork; // ???Should not this disable fork disable ff2???
            end
         end
      join
      
   endtask:my_monitor;
 
endclass 
 
module top;
   a_bus_monitor abm1;
 
   initial begin: i1
      abm1 = new();
      fork:fm1
         begin:fmf1
            abm1.my_monitor();
         end
         begin:fmf2
            #6ns;
            abm1.my_event1 = 1;
            #10ns;
            abm1.my_event1 = 0;
            #20ns;
            abm1.my_event1 = 1;
            #6ns;
            abm1.my_event1 = 0;
         end
      join

      $display("main fork has been joined");
   end 
endmodule 

```

Questasim 10.3b gives below output:

# Passed 5 ns now

# At time 6 found an event on my\_event1 (= 1)

# Passed 5 ns now

# Passed 5 ns now

# At time 16 found an event on my\_event1 (= 0)

# Disabling fork now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# At time 36 found an event on my\_event1 (= 1)

# Passed 5 ns now

# At time 42 found an event on my\_event1 (= 0)

# Disabling fork now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Passed 5 ns now

# Breaking because iter is 20

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 6, 2014, 4:54pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/12 "2014-08-06T16:54:28Z")

</div>

_In reply to [Sailaja](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/11):_

No.

> The **disable fork** statements kills the children of the currently running thread. (i.e the thread executing the **disable fork** statement).

Your **ff2** thread is a single **forever** statement. You can use the **break** statement instead to get out of the forever loop.

---

<div class="post-metadata">

**Author:** ![Sailaja](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/47e85d/32.png) [@Sailaja](https://verificationacademy.com/forums/u/Sailaja)\
**Post date:** [August 7, 2014, 5:09am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/13 "2014-08-07T05:09:44Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/12):_

Got it…  
Thanks

---

<div class="post-metadata">

**Author:** ![sandy1664](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@sandy1664](https://verificationacademy.com/forums/u/sandy1664)\
**Post date:** [August 13, 2014, 9:29am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/14 "2014-08-13T09:29:00Z")

</div>

_In reply to [Sailaja](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/13):_

Hi Dave,

Here in the below code if i get timer\_flag as ‘0’ even before the completion of thread named label, I am trying to disable that process. As per my analysis it should disable that particular iteration and again it has to wait for timer\_flag to be 1 to continue, and it has to wait for time run\_duration before making expired bit 1 from the last valid time\_flag that it receives .But this logic is calculating run\_duration from the first valid timer\_flag.(Consider if re-setting timer\_flag to 0 for two times at 0ns, 5ns and 10ns and run\_duration value is 50ns,then the expired bit should get assert at 60ns but its happening at 50ns itself,which is not my requirement.) How does it happens even after disabling label thread?

Could you please provide some clarity on this.

Code::

```verilog
fork 
  forever
  begin :label //{
      wait(timer_flag==1);
      #(run_duration);
      expired=1;
      timer_flag=0;
  end //}

  forever
  begin //{
  @(timer_flag);
     if(timer_flag==0)
     begin
     ovm_report_info(get_type_name,$psprintf("Disabling label thread at time %.6t", $realtime));
     disable label;
     end
   end //}
 join //}

```

Thanks

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [August 13, 2014, 7:16pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/15 "2014-08-13T19:16:48Z")

</div>

_In reply to [sandy1664](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/14):_

You need to show how you reset the timer flag. If the timer\_flag was set to 0 and back to 1 in zero delay, there is no guarantee that the second forever loop would see timer\_flag==0 and disable the label

```auto
...
timer_flag = 0;
timer_flag <= 1; // this make sure it is 0 for one delta cycle.
...

```

---

<div class="post-metadata">

**Author:** ![sandy1664](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/s/e8c25b/32.png) [@sandy1664](https://verificationacademy.com/forums/u/sandy1664)\
**Post date:** [August 14, 2014, 5:50am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/16 "2014-08-14T05:50:44Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/15):_

Everytime i reset the timer , the timer\_flag becomes 0 and after we start the timer again by making timer\_falg to 1 after 1ps delay.

---

<div class="post-metadata">

**Author:** ![arpkum](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/ec9cab/32.png) [@arpkum](https://verificationacademy.com/forums/u/arpkum)\
**Post date:** [August 14, 2014, 9:52am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/17 "2014-08-14T09:52:05Z")

</div>

_In reply to [Sumit Jain](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/16):_

Hi Sumit ,  
please correct me if i am wrong but I believe this code will give output as :-  
a1  
a2  
a3  
a5  
I tried with Vcs simulator , above is the output i got , I am not seeing any a6 there.

Thanks  
Arpit

---

<div class="post-metadata">

**Author:** ![Kuma](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/k/a587f6/32.png) [@Kuma](https://verificationacademy.com/forums/u/Kuma)\
**Post date:** [May 10, 2016, 6:18am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/18 "2016-05-10T06:18:50Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/17):_

Hi Dave,

For the below example , as main task is calling the dev\_state task which contains disable\_fork .  
Then as per LRM Std 1800-2012 , disable fork will kill all the remaining threads of main task also right .

So the output should be  
5 THREAD 1  
10 THREAD 3  
10 After task call

Could you please let me know whether my understanding is correct

```verilog
module test; 
initial begin 
           main(); 
        end 
task main();
  fork 
    #5 $display($time,,"THREAD 1"); 
    #25 $display($time,,"THREAD 2"); 
  join_any 
  dev_state(); 
  $display($time,,"After task call"); 
  #100 $finish; 
endtask 

task dev_state();
  fork 
     #5 $display($time,,"THREAD 3");
     #10 $display($time,,"THREAD 4"); 
  join_any 
  disable fork;
endtask 
endmodule

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [May 10, 2016, 6:36am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/19 "2016-05-10T06:36:53Z")

</div>

_In reply to [kvssrohit](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/18):_  
Correct, sort of. It will kill all remaining child threads of the initial block. A task call is not a thread.

Please use [code tags](https://verificationacademy.com/forums/announcements/please-format-your-code-code-and/code-tags) to make your code easier to read. I have added them for you.

---

<div class="post-metadata">

**Author:** ![desaivs1994](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@desaivs1994](https://verificationacademy.com/forums/u/desaivs1994)\
**Post date:** [October 18, 2016, 9:41am UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/20 "2016-10-18T09:41:50Z")

</div>

Hi Dave,

I wanted to know if a disable fork inside a task can kill threads launched by another task using fork…join\_none. For example: In the following code, if task a2() is called 10 units after calling task a1(), will the threads inside a1() be killed?

```verilog
class alpha;
  
  task a1();
    fork
      #(20) $display("run1");
      #(30) $display("run2");
    join_none
  endtask: a1

  task a2();
    disable fork;
  endtask: a2

endclass: alpha

module top;
  alpha abc;
  initial begin
    abc = new();
    abc.a1();
    #(10) abc.a2();
  end
endmodule

```

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [October 18, 2016, 4:14pm UTC](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/21 "2016-10-18T16:14:19Z")

</div>

_In reply to [desaivs1994](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662/20):_  
As I said above, Calling a task or function does not create a new thread; only a fork can do that. So whether a disable is “inside” a task has no relevance - it’s the thread that matters.

[Next page](https://verificationacademy.com/forums/t/confusion-in-fork-join-disable-fork/28662.md?page=2)
