Confusion about threads

I have doubts about threads spawning between begin end and fork-join`

module top;
  initial
    begin // parent process
      fork
        #5 $display("A"); //child process 1
        #7 $display("B"); //child process 2
      join_any
        	fork
          		#7 $display("C"); //child process 3
        	join_none
        	disable fork;
    end
endmodule

here child process 2 and 3 got killed as I expected

module top;
  initial
    begin // parent process
      fork
        #5 $display("A"); //child process 1
        #7 $display("B"); //child process 2
      join_any
fork
        	fork
          		#7 $display("C"); //child process 3
        	join_none
        	disable fork;
join
    end
endmodule

Output :
A
B
C
I am expecting only child process 3 will be killed but it is not killing any of the thread why?

module top;
  initial
    begin // parent process 1
      fork
        #5 $display("A"); //child of process 1
        #7 $display("B"); //child of process 1
      join_any
begin// child process 3
        	fork
          		#7 $display("C"); //child of process 3
        	join_none
        	disable fork;
end
    end
endmodule

Output: A
I just want to clarify begin end inside begin end will create new process or begin end inside begin end wont create a new process. If begin end will also a process then only child of process 3 has to kill but here child of process 1 and child of process 3 threads are killing please clarify my doubts

fork join wait till completion of thread in code 2, hence 3 processes are printed.

5ns A
7ns B
12ns C

begin end didnt wait for completion of process 3 and process 2 in second thread, hence only A is displayed.

5ns A

begin/end is not a process; it is a compound statement. Each statement inside a fork creates a separate process. In your second example, the disable fork is one of two statements inside a fork. It is a separate process with no children.

2 Likes