Functions
WebDev/*nix assorted functions & hacks
Search
find all
Array
-
Bash
-
Classes
-
Config
-
DateTime
-
Debug
-
dotjs
-
File
-
foooo
-
Git
-
HTML
-
Images
-
Javascript
-
Linux
-
Math
-
Misc
-
MySQL
-
OS X
-
Reading
-
Redis
-
Shell scripts
-
snippets
-
SQL
-
Strings
-
TextMate
-
Web
-
\(^o^)/ Fun!
-
_Misc hacks
-
_Pages
Shell scripts
* Readme
dict
edit_json (php)
encrypt/decrypt
pdoc
pftp - ftp a file to some place
php fork & IPC
php input cli
phplint
~/bin/inc/lib.php
10
found
auth
Name:
Category: Shell scripts
Body:
Example script that uses pcntl_fork() to fork itself a few dozen times, and System V IPC functions to create a message queue, let the children processes send data to it and the parent process read and display it. #!/usr/bin/env php <?php // Create or attach to a message queue $queue = msg_get_queue(ftok(__FILE__, 'a'), 0600); # set permissions so that only the owner can read/write to this queue # ftok() creates a message queue key based on the file's inode and a single character project identifier. The latter lets # one create several queues all based on/in the same file. $child_count = 50; // Fork 5 child processes for ($i = 0; $i < $child_count; $i++) { # pcntl_fork() splits the script at the point where it is called. The two copies of the script know what they are # based on the value of $pid: 0 for the child, positive int for the parent (it will be the child's PID) or -1 for failure. $pid = pcntl_fork(); if ($pid == -1) { die('Could not fork'); } elseif ($pid) { # Parent process // Do nothing, will wait for child messages later } else { # Child process: do all the Child things, send the results to the queue, then exit $sleepTime = rand(1, 5); sleep($sleepTime); # Send a message to the parent msg_send($queue, 1, "Child $i slept for $sleepTime seconds"); exit(0); } } # Parent process waits to receive messages from all child processes for ($i = 0; $i < $child_count; $i++) { msg_receive($queue, 0, $msgType, 1024, $message, true); echo "Received message: $message\n"; } # Destroy the message queue, otherwise it hangs around in memory. ipcs will show you current message queues. msg_remove_queue($queue); # you could also use register_shutdown_function() for this. If all else fails, use ipcrm -q <msqid>, where the # msqid shows up under the output of ipcs.
Footer