<?php
print "Main program started.... \n";
createProcess("Job1");
createProcess("Job2");
print " *** Child Process started *** \n";
while (TRUE) {
$pid = pcntl_waitpid(0, $status, WNOHANG);
if ($pid > 0) {
childProcessComplete($pid);
}else if($pid === -1){
print " *** Child Process Completed *** \n";
allChildProcessComplete();
exit();
}
}
print "Main program end. \n";
function startChildProcess($childProcessName) {
$executionTime = rand(5, 10);
if($childProcessName === "Job1"){
print "Starting Job1 on processId " .getmypid()." at " . date('l jS \of F Y h:i:s A') . " expected time $executionTime seconds\n";
}else if($childProcessName === "Job2"){
print "Starting Job2 on processId " .getmypid()." at " . date('l jS \of F Y h:i:s A') . " expected time $executionTime seconds\n";
}
sleep($executionTime);
}
function errorOnProcessLunch() {
print "Failed to lunch process \n";
}
function childProcessComplete($pid) {
print "Child processing is done for $pid at " . date('l jS \of F Y h:i:s A') . " \n";
}
function createProcess($pname) {
$pid = pcntl_fork();
if ($pid == -1) {
errorOnProcessLunch();
}
else if ($pid === 0) {
startChildProcess($pname);
exit(); }
else {
startParentProcess($pid);
}
}
function startParentProcess($childProcessID){
print "In parent thread created child processid $childProcessID \n";
}
function allChildProcessComplete(){
print "All child processing completed \n";
}
?>