This question was asked during an interview. Can you help me with question. I tried putting the threads in two different initial blocks but couldn’t get the required result.
this is a sample of the code i tried:
module tb();
initial
process(a);
initial
process(b);
endmodule
The above can lead to a race condtion and will not get the desired result. Can you suggest some other way to achieve it.
Since the restriction is on usage of fork join , I assume you are free to use
fork join_any and fork join_none
initial begin
fork
begin
#2 ;
$display("#2 Done") ;
end
begin
#4 ;
$display("#4 Done") ;
end
join
$display("Fork join completes ");
end
The above fork join functionality can be achieved via ::
initial begin
fork
begin
#2 ;
$display("#2 Done") ;
end
begin
#4 ;
$display("#4 Done") ;
end
join_none
wait_fork ;
$display("Fork join completes ");
end
module test;
event event_h;
initial
begin
wait(event_h.triggered);
$display("[%0t] Executing process 1",$stime);
end
initial
begin
wait(event_h.triggered)
$display("[%0t] Executing process 2",$stime);
end
initial
begin
#1;
-> event_h;
end
endmodule
Just a try, Not sure whether this is an appropriate answer.
You can also use semaphore to sync up 2 threads or mailbox.
But to me, it is not good way that interviewer asks not to use language all features. This means that interviewer does not spend time to prepare good question.
It is like drive the car without using acceleration.
Think twice before accepting the offer.
Love that analogy about driving a car; I’ve been searching for something like that.
Sometimes it’s more about about making sure you have enough information to attempt a solution rather than actually providing a solution.
A car can’t go from stationary to moving with acceleration. Did you really mean “without putting pressure on the accelerator pedal?”
If someone or something pushes my car from behind, or coast down a hill, is that still considered “driving”? Or does the car need to be self-propelled?
Back to the fork-join question, there are an infinite number of way to write code with a fork-join, and for some of those, removing the fork would have no change in the behavior.
initial fork
$display("hello");
join
Removing or replacing the fork/join with begin-end would not change the behavior. So I would have asked for a particular example of fork-join usage that they wanted implemented without using fork-join.
Hi,
I came across this question and it looks like this implementation also works on the simulator without events and using tasks instead forked off across different initial begin blocks.
This can implement both the fork-join and fork-join_any construct. I seem to have tested this on simulator and it looks like this too works.
module sv;
integer i=0;
initial begin : thread1
task1();
i++;
end : thread1
initial begin : thread2
task2;
i++;
end : thread2
initial begin // thread for fork_join
$display("just before waiting time %0d", $time);
// variation1: for fork_join need both thread1 and thread2 to complete
wait(i==2); // wait till both the tasks complete
$display("completed the wait at time %0d", $time); // execute this line only after both task1 and task2 which are forked off at same time.
//you can add code here that can synchronize only after both complete.
end
initial begin //thread for fork join_any
// variation2: for fork_join_any to execute after any of the thread1 and thread2 to complete
//NOTE: DONT USE wait(i==1) as they may be some race condition if both the threads complete at the same time and you may miss the trigger as it would jump from 0-> 2 directly if both start and end at the same time.
wait(i>0); // wait till both the tasks complete
$display("completed the wait at time %0d", $time); // execute this line only after both task1 and task2 which are forked off at same time.
//you can add code here that can synchronize after one of the thread works
end
task task1();
$display("inside task1 at time %0d", $time);
#20;
$display("completed task1 at time %0d, i:%0d", $time, i);
endtask
task task2();
$display("inside task2 at time %0d", $time);
#30;
$display("completed task2 at time %0d, i: %0d", $time,i);
endtask
endmodule