note 123915 added to ref.pcntl

From: Date: Fri, 07 Jun 2019 16:22:04 +0000
Subject: note 123915 added to ref.pcntl
Groups: php.notes 
Request: Send a blank email to php-notes+get-211195@lists.php.net to get a copy of this message
<?php /** * @file * Working demo of simulating parallel process in PHP. */ // **** **** // print "Main program started.... \n"; // Start two child processes. 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"; /** * Method to start child process. */ 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"; } // Simulate doing actual work with sleep(). sleep($executionTime); } /** * Method to notify when system unable to start a process. */ function errorOnProcessLunch() { print "Failed to lunch process \n"; } /** * Method to notify when a child process complete the task. */ function childProcessComplete($pid) { print "Child processing is done for $pid at " . date('l jS \of F Y h:i:s A') . " \n"; } /** * Method to create a new process. */ function createProcess($pname) { $pid = pcntl_fork(); if ($pid == -1) { errorOnProcessLunch(); } else if ($pid === 0) { startChildProcess($pname); exit(); // Make sure to exit. } else { startParentProcess($pid); } } /** * Method to notify when parent thread execute. */ function startParentProcess($childProcessID){ print "In parent thread created child processid $childProcessID \n"; } /** * Method to notify when all child process has completed the task. */ function allChildProcessComplete(){ print "All child processing completed \n"; } ?> ---- Server IP: 208.43.231.9 Probable Submitter: 185.85.3.134 (proxied: 2409:4062:2015:d215:1976:d865:e1d8:9f5d) ---- Manual Page -- http://php.net/manual/en/ref.pcntl.php Edit -- https://master.php.net/note/edit/123915 Del: integrated -- https://master.php.net/note/delete/123915/integrated Del: useless -- https://master.php.net/note/delete/123915/useless Del: bad code -- https://master.php.net/note/delete/123915/bad+code Del: spam -- https://master.php.net/note/delete/123915/spam Del: non-english -- https://master.php.net/note/delete/123915/non-english Del: in docs -- https://master.php.net/note/delete/123915/in+docs Del: other reasons-- https://master.php.net/note/delete/123915 Reject -- https://master.php.net/note/reject/123915 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#211195) next »