PHP防止代码在高并发下同时运行两次

1st Method with just 1 script via browser ( Works perfect )

$monitor_pid = "/tmp/a.lock";


$fp = fopen( $monitor_pid, 'a+' );
if ( flock( $fp, LOCK_EX | LOCK_NB ) )
{
    $pid = intval( trim( fgets( $fp ) ) );



    //RUN CODE ONCE AT A TIME


    fflush( $fp );
    flock( $fp, LOCK_UN );
}
else
{
    exit;
}

fclose( $fp );

2nd Method with shell_exec that calls another script ( Doesnt work )

<?php
 shell_exec("php test.php");
?>

And the test.php

$monitor_pid = "/tmp/a.lock";


$fp = fopen( $monitor_pid, 'a+' );
if ( flock( $fp, LOCK_EX | LOCK_NB ) )
{
    $pid = intval( trim( fgets( $fp ) ) );



    //RUN CODE ONCE AT A TIME


    fflush( $fp );
    flock( $fp, LOCK_UN );
}
else
{
    exit;
}

fclose( $fp );

Seems like in the 2nd method, the process does not care about my lock. Why that is happening? Is there any other alternative?

Thanks