Is fork join and fork join_any with wait fork perform the same operation?

I feel that with respect to operation of fork join and fork join_any with wait fork, both will serve the same purpose. That is the operation is same.

Fork_join :

module  main ; 
 initial 
  begin 
   #(10); 
   $display(" BEFORE fork time = %2d ",$time ); 
   fork 
    begin 
        $display("current time in Thread-1 is %0t",$time);
	# (20); 
	$display("Thread-1 time = %2d # 20 ",$time ); 
    end 
    begin 
        $display("current time in Thread-2 is %0t",$time);
	#(10); 
	$display("Thread-2 time = %2d # 10 ",$time ); 
    end 
    begin
        $display("current time in Thread-3  is %0t",$time);
	$display("Thread-3 time = %2d # 5 ",$time ); 
    end 
   join
   $display("time = %2d Outside the main fork ",$time ); 
  end 
endmodule

wait fork program:

module main ; 
 initial 
  begin 
   #(10); 
   $display(" BEFORE fork time = %2d ",$time ); 
   fork 
    begin 
        $display("current time in Thread-1 is %0t",$time);
	# (20); 
	$display("Thread-1 time = %2d # 20 ",$time ); 
    end 
    begin 
        $display("current time in Thread-2 is %0t",$time);
	#(10); 
	$display("Thread-2 time = %2d # 10 ",$time ); 
    end 
    begin
        $display("current time in Thread-3  is %0t",$time);
	$display("Thread-3 time = %2d # 5 ",$time ); 
    end 
   join_any
   $display("time = %2d Outside the main fork ",$time );
   wait fork;
   $display("time = %2d After the wait fork ",$time );
  end 
endmodule

Please correct me if i am wrong.

-Tarun

In reply to perumallatarun:

It is not the same.

In your second case: fork-join_any, right after your Thread-3 execution at time 10units, you are out of the main fork block

In your first case, it waits for all 3 Threads to execute and you will be out of your main-fork block at 30 time units

In reply to SaphEr:

but the final thing in both the test cases is same, it will wait for all the threads to complete.

In reply to perumallatarun:

In reply to SaphEr:
but the final thing in both the test cases is same, it will wait for all the threads to complete.

Yes, they are same. When you use “fork…join_any” and “wait_fork”, the program will wait at “wait_fork” statement until all threads in “fork…join_any” are completed.

In reply to perumallatarun:

if you have some task instead of following timestamp print, then test behavior is different.


// $display("time = %2d Outside the main fork ",$time );
checking_method(); // for instance

Basically, the timing you exit fork-join block is totally different, so I think this 2 cases are different.

In reply to javatea:

In reply to perumallatarun:
if you have some task instead of following timestamp print, then test behavior is different.


// $display("time = %2d Outside the main fork ",$time );
checking_method(); // for instance

Basically, the timing you exit fork-join block is totally different, so I think this 2 cases are different.

Because this post owner wanted to compare “fork…join” and “fork…any” + “wait fork”.
If you put the statement at the place like that, of course they are different.

In reply to cuonghl:

so the timing exit the fork-join block are the same?

I understood your concern in it. Can anyone please give me an exact example where we can find the necessity of having wait fork.

In reply to perumallatarun:
Typically, wait fork is used with fork/join_none.

repeat(32) fork
             my_process_task;
           join_none
wait fork; //waits for all 32 processes to complete 

In reply to perumallatarun:

I had two blocks A & B to be executed. Block A to be executed if any one of the threads in the fork is executed. And another Block B to be executed after all the threads in the fork are executed. So, I placed Block A after fork … join_any; and Block B after fork … join_any+wait fork;

In reply to kmohansan:

In reply to perumallatarun:
I had two blocks A & B to be executed. Block A to be executed if any one of the threads in the fork is executed. And another Block B to be executed after all the threads in the fork are executed. So, I placed Block A after fork … join_any; and Block B after fork … join_any+wait fork;

Don’t forget that Block A and Block B also are affected each other in some cases, then be careful where you place Block A and Block B after fork block.

In reply to kmohansan:

In reply to perumallatarun:
I had two blocks A & B to be executed. Block A to be executed if any one of the threads in the fork is executed. And another Block B to be executed after all the threads in the fork are executed. So, I placed Block A after fork … join_any; and Block B after fork … join_any+wait fork;

fork
    begin /*..T1. */ end
    //....
    begin /*..Tn. */ end
join_any
//BLOCK A
wait fork;
//BLOCK B

In reply to Rahulkumar Patel:

I hope that we can use fork … join_any+wait fork; for this type of verification

In reply to Rahulkumar Patel:

No, this is not good. If you put the Block B statement like that, then Block B is only executed after all threads in fork/join_any AND “Block A” are finished. It is being affected by the time to execute “Block A”.

Here is my idea:


semaphore s = new (0);
fork
    begin 
      /*..T1. */
      s.put(1)
    end
    begin 
      /*..T2. */
      s.put(1)
    end
    begin 
      /*..T3. */
      s.put(1)
    end
join_any

fork 
    begin
      //BLOCK A
    end
    begin
      s.get(3);
      //BLOCK B
    end
join_none