How can PHP efficiently detect when a C program has finished processing and generating a file?

To efficiently detect when a C program has finished processing and generating a file, one approach is to use PHP's `exec()` function to execute the C program and then check for the existence of the generated file at regular intervals until it appears. This can be achieved by using a loop that checks for the file's presence using `file_exists()` function and sleeps for a short period between each check.

<?php

// Execute the C program using exec()
exec('/path/to/your/c/program');

// Check for the existence of the generated file at regular intervals
$filePath = '/path/to/generated/file.txt';
while (!file_exists($filePath)) {
    sleep(1); // Sleep for 1 second before checking again
}

echo 'File generated successfully!';

?>