Question

The program below starts four processes. The task first waits for the last process to start executing and then waits for the first process to terminate. At that point, the parent process forcibly terminates all forked processes that have not yet completed.

program automatic test;

initial begin
     do_n_ways;
end

task do_n_ways;
    process job[1:4];

for(int i=1;i<=4;i++) 
fork
    automatic int j = i;
    job[j] = process::self();
    $display("process %d starting...",j);
    if(j==2) begin
        #100 $display("delay 2ns");
    end
join_none

for(int j = 1;j<=4;j++) begin //wait for all process starting 
    wait(job[j] != null);
end

job[1].await();               //wait for first process finish

for(int j=1;j<=4;j++) begin
    if(job[j].status != process::FINISHED) begin
        job[j].kill();
        $display("process %d killed...",j);
    end
end
endtask

endprogram

It appears that process #2 never gets killed since I never see "process 2 killed..." Why does this process continue to run?

Was it helpful?

Solution

You're missing a begin..end when starting your processes. Your fork should look like this:

fork
  automatic int j = i;
  begin // treat everything from here as an own process
    job[j] = process::self();
    $display("process %d starting...",j);
    if(j==2) begin
      #100 $display("delay 2ns");
    end
  end
join_none

By putting that begin..end, you treat all statements as belonging to one process. What your code was doing is spawning a separate process for each statement, i.e. one for the $display(...), one for the if(...) and one for assigning the job.

Here's a working example on EDA Playground: http://www.edaplayground.com/x/2f8 I changed program to module to work on that ModelSim version.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top