# Automatic variables in fork

**URL:** <https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041>\
**Category:** SystemVerilog\
**Tags:** SystemVerilog\
**Created:** [September 20, 2015, 2:31am UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041 "2015-09-20T02:31:24Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![user116](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/9fc29f/32.png) [@user116](https://verificationacademy.com/forums/u/user116)\
**Post date:** [September 20, 2015, 2:31am UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/1 "2015-09-20T02:31:24Z")

</div>

hi,  
I am having questions about how automatic variables work in fork statement in for loop. Referring to the code below, the simulator (VCS) is displaying 0,1,2 for program test (case1) however, for all other programs (test1-test3), it is displaying 3,3,3 though variable k is automatic in all cases. Can someone help me and explain this behaviour?

1. 

```verilog
module automatic test;
initial begin
for (int j = 0; j < 3; j++)
fork //begin
automatic int k = j;
// k = j;
$write (k);
//end
join_none
#0 $display ("\n test");
end
endmodule

```

1. 

```verilog
module automatic test1;
initial begin
for (int j = 0; j < 3; j++)
fork begin
automatic int k = j;
// k = j;
$write (k);
end
join_none
#0 $display ("\n test1");
end
endmodule

```

1. 

```verilog
module automatic test2;
initial begin
for (int j = 0; j < 3; j++)
fork begin
automatic int k = j;
k = j;
$write (k);
end
join_none
#0 $display ("\n test2");
end
endmodule

```

1. 

```verilog
module automatic test3;
initial begin
for (int j = 0; j < 3; j++)
fork //begin
automatic int k = j;
k = j;
$write (k);
//end
join_none
#0 $display ("\n test3");
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:** [September 20, 2015, 5:51am UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/2 "2015-09-20T05:51:00Z")

</div>

The thing to know here is that automatic variables have a lifetime based on an activation scope. They are created and initialized when entering that scope, and disappear when leaving that scope. The key thing to know is the a scope remains active until reaching the procedural end of that scope, or after all child process have completed, whichever comes last.

Cases 2 and 3 are essentially the same. The activation scope for **k** is the begin/end block inside the fork/join\_none. There are actually 3 activations of k, one for each process forked off. None of those processes activate until after the parent initial process reaches the #0 delay, which is after the for-loop has reached its final value j==3. The only difference with case 3 is you initialize each of the **k** ’s to 3 as in case 2, but then the first statement of the process is to assign it to 3 again. The variable **k** disappears when each process terminates.

In cases 1 and 4, the activation scope for **k** is entering the fork/join\_none. As with cases 2 and 3, there will be 3 activations of **k** , but this time the activation starts immediately with each iteration of the for-loop. So **k** gets initialized with the current value of **j** ; 0, 1, and 2. The lifetime of each **k** continues until the process spawn by the fork/join\_none terminates.

The problem with case 4 is there are two processes spawned by each activation of the fork/join\_none: the assignment k = j, and the $write(k). The assignment to each **k** will take the final value of j==3 since the process does not start until the initial process reaches the #0 delay. But now you have a race condition with the process with the $write(k) statement. If the write executes first, you will see the same results as case 1: the values 1, 2, and 3. But if the assignment statement executes first, k gets the final value of j==3, and you get the same results as case 3.

---

<div class="post-metadata">

**Author:** ![user116](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/9fc29f/32.png) [@user116](https://verificationacademy.com/forums/u/user116)\
**Post date:** [September 20, 2015, 8:13am UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/3 "2015-09-20T08:13:00Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/2):_

Hi Dave,  
thanks a lot for the explanation. It is clear now. I have a related question:  
If I move the automatic variable declaration and initialization outside fork statement, as given below:

case 5:

```verilog
program automatic test4;
  initial begin
    for (int j = 0; j < 3; j++) begin
      automatic int k = j; 
      fork //begin
       
        // k = j;
 
        $write (k);
      //end
      join_none
    end
    #0 $display ("\n test");
  end
 
endprogram

```

Then the result it displays is 2 2 2 (since the final value of k after the for loop completes will be 2, which will be used by $write (k)). However, in the Chris Spear book (system verilog for verification, second edition) on page 226/227, it says case 1 and case 5 should give same result. This is confusing. Can you please comment on this.

regards,  
-sunil

---

<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:** [September 20, 2015, 5:05pm UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/4 "2015-09-20T17:05:00Z")

</div>

_In reply to [puranik.sunil@tcs.com](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/3):_

BTW, please use [code tags](https://verificationacademy.com/forums/announcements/please-format-your-code-code-and/code-tags) to format your code. I have done that for you in your post.

Yes, case 1 and 5 should work the same. The only difference would be visibility of the variable k after the fork statement if there were other statements. Your code works fine for me in ModelSim/Questa. It prints 2 1 0. (any order would be correct).

Also, I do not recommend the use of [program blocks](https://blogs.sw.siemens.com/verificationhorizons/2009/05/07/programblocks/). Just use a module.

Dave

---

<div class="post-metadata">

**Author:** ![user116](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/9fc29f/32.png) [@user116](https://verificationacademy.com/forums/u/user116)\
**Post date:** [September 21, 2015, 2:24am UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/5 "2015-09-21T02:24:39Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/4):_

Hi Dave,

I have run this code (test4)on the VCS simulator and but it prints 2, 2, 2.  
Now based on the explanation you have given earlier, the activation scope for k (in program test4) is the begin…end block of for loop. So k will get values 0,1,2 and these values will be used by $write(k) and it should print 0,1,2. This is unlike cases 2 and 3 above where k is activated in begin end block inside fork, which gets scheduled after #0 delay and by that time j would have reached 3. So it uses j ==3 for all 3 processes and prints 3,3,3 (let me know if this understanding is correct).  
I am not clear why VCS is giving different result. I have tried replacing program with module statement, but it makes no difference.  
Also you have said “The only difference would be visibility of the variable k after the fork statement if there were other statements.” in your comment. Can you please elaborate on this.

thanks  
-sunil

---

<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:** [September 21, 2015, 4:34am UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/6 "2015-09-21T04:34:10Z")

</div>

_In reply to [puranik.sunil@tcs.com](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/5):_

Your understanding is correct, but your tool’s understanding is not. Please contact your vendor for tool specific issues.

---

<div class="post-metadata">

**Author:** ![user116](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/u/9fc29f/32.png) [@user116](https://verificationacademy.com/forums/u/user116)\
**Post date:** [September 22, 2015, 1:39am UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/7 "2015-09-22T01:39:25Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/6):_

HI Dave,  
I have one more question on automatic variables:  
System verilog LRM 3.1 says “In a data\_declaration that is not within the procedural context, it shall be illegal to use the automatic keyword”. Does it mean that automatic variables can be declared only in procedural blocks and any variable not within procedural block is static by default?  
I understand procedural blocks are - initial, always,final, begin…end, fork…join, always\_comb, always\_latch, always\_ff, task, function etc.  
Are there any more of them?  
rgs,  
-sunil

---

<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:** [September 22, 2015, 4:42pm UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/8 "2015-09-22T16:42:44Z")

</div>

_In reply to [puranik.sunil@tcs.com](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/7):_

Please get a copy of the [latest SystemVerilog 1800-2012 LRM](https://blogs.sw.siemens.com/verificationhorizons/2024/03/04/get-your-free-copy-of-the-ieee-1800-2023-systemverilog-lrm/)

Yes, automatic variables can only be declared in a procedural block - those are the only blocks that have a potential lifetime shorter than the entire simulation.

I think etc. covers all of them. :)

---

<div class="post-metadata">

**Author:** ![mchal9e3](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mchal9e3](https://verificationacademy.com/forums/u/mchal9e3)\
**Post date:** [February 14, 2020, 8:49pm UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/9 "2020-02-14T20:49:11Z")

</div>

So, I have a related question, primarily out of curiosity as I solved my issue. I had code similar to case 1 above that worked fine in a test class; however, the comp errored out in a task declared in the base test class - didn’t like the automatic int declaration. HOWEVER, I then realized that the fork in my test class was named so I added a name to the base test class task version. Bingo, compiles - we’ll see if it runs. Why does naming the fork fix the compile problem?

---

<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:** [February 14, 2020, 9:03pm UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/10 "2020-02-14T21:03:15Z")

</div>

_In reply to [mchal9e3](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/9):_

Without seeing the code or the specific error message it is difficult to comment on what the problem might be.

---

<div class="post-metadata">

**Author:** ![mchal9e3](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/m/50afbb/32.png) [@mchal9e3](https://verificationacademy.com/forums/u/mchal9e3)\
**Post date:** [February 14, 2020, 9:43pm UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/11 "2020-02-14T21:43:03Z")

</div>

Well, this is a never mind, I went back to reproduce the error and FAILED. Must have been an env or moldy/corrupt build issue. This is a good thread to jog my memory about this issue tho, it’s pretty easy to get bitten by it. In fact, I got the original attempt to work in my test because I ‘forgot’ to put a begin after the fork as I had intended.

---

<div class="post-metadata">

**Author:** ![Aashish\_Itani](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/a/848f3c/32.png) [@Aashish\_Itani](https://verificationacademy.com/forums/u/Aashish_Itani)\
**Post date:** [December 27, 2020, 9:34pm UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/12 "2020-12-27T21:34:30Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/11):_

When you say “None of those processes activate until after the parent initial process reaches the #0 delay” for case 2 and 3, what is the reason for that? Does not all forked processes execute at zero time and the #0 delay line run at in the end?

---

<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:** [December 28, 2020, 6:50am UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/13 "2020-12-28T06:50:41Z")

</div>

_In reply to [Aashish Itani](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/12):_

Because the LRM says so. To keep things more deterministic, section _9.3.2 Parallel blocks_ says the processes do not start until the parent process suspends or terminates.

---

<div class="post-metadata">

**Author:** ![vickydhudashia](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/v/e274bd/32.png) [@vickydhudashia](https://verificationacademy.com/forums/u/vickydhudashia)\
**Post date:** [February 9, 2022, 12:11pm UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/14 "2022-02-09T12:11:53Z")

</div>

_In reply to [dave\_59](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/13):_

Hi Dave, with regard to fork join\_none, I have gone through some threads asked in the forum but I still don’t understand why in this code j = 0 is not getting printed. Would you please help me understand this?

```verilog

module top;
	initial begin
		for (int j=0; j<3; j++) begin
			#1;
			fork
				$display($time, " j = %0d",j);
			join_none
		end
	end
endmodule

```

Result:

```verilog

                   1 j = 1
                   2 j = 2
                   3 j = 3

```

---

<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:** [February 9, 2022, 3:57pm UTC](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/15 "2022-02-09T15:57:07Z")

</div>

_In reply to [vickydhudashia](https://verificationacademy.com/forums/t/automatic-variables-in-fork/31041/14):_

This happens because the process inside the fork/join\_none (the $display) does not start until the parent process suspends or terminates.

The first process does not start until the second iteration suspends at the #1.  
The second process does not start until the third iteration suspends at the #1.  
The third process does not start until exiting the loop and reaching the end of the initial.

In all three cases j has already advanced by +1. This is why we always recommend creating a separate automatic variable that has the loop iterator value at the point when the process gets scheduled, not when it starts executing.
