PHP forking and multiple child signals

I'm trying to write a script which creates a number of forked child processes using the pcntl_* functions.

Basically, there is a single script which runs in a loop for about a minute, periodically polling a database to see if there is a task to be run. If there is one, it should fork and run the task in a separate process so that the parent isn't held up by a long-running task.

Since there possibly could be a large number of tasks ready to be run, I want to limit the number of child processes that are created. Therefore, I am keeping track of the number of processes by incrementing a variable each time one is created (and then pausing if there's too many), and then decrementing it in a signal handler. Kind of like this:

define(ticks = 1); $openProcesses = 0; // how many we have open $max = 3; // the most we want open at a time pcntl_signal(SIGCHLD, "childFinished"); while (!time_is_up()) { if (there_is_something_to_do()) { $pid = pcntl_fork(); if (!$pid) { // I am the child foo(); // run the long-running task exit(0); // and exit } else { // I am the parent ++$openProcesses; if ($openProcesses >= $max) { pcntl_wait($status); // wait for any child to exit } // before continuing } } else { sleep(3); } } function childFinished($signo) { global $openProcesses; --$openProcesses; }

This works pretty much ok most of the time, except for when two or more processes finish simultaneously - the signal handler function is only called once, which throws out my counter. The reason for this is explained by "Anonymous" in the notes of the PHP manual:

Multiple children return less than the number of children exiting at a given moment SIGCHLD signals is normal behavior for Unix (POSIX) systems. SIGCHLD might be read as "one or more children changed status -- go examine your children and harvest their status values".

My question is this: How do I examine the children and harvest their status? Is there any reliable way to check how many child processes are open at any given time?

Using PHP 5.2.9

-------------Problems Reply------------

You could have children send a SIGUSR1 to the parent when they start,then a SIGUSR2 before they exit. The other thing you are dealing with when using primitive signals is the kernel merging them, which it does not do with RT signals. In theory, ANY non-rt signal could be merged.

You might implement some kind of simple locking using sqlite, where only one child at a time can have the talking stick. Just make sure that children handle normally fatal signals so that they remain alive to free the lock.

Category:php Views:0 Time:2010-02-16
Tags: php process fork

Related post

  • Multiple child process 2009-05-18

    can someone help me about how to create multiple child processes which have the same parent in order to do "some" part of particular job? for example, an external sorting algorithm which is applied with child processes; each child process sorts a par

  • Behavior of WaitForMultipleObjects when multiple handles signal at the same time 2009-03-20

    Given: I fill up an array of handles with auto reset events and pass it off to WaitForMultipleObjects with bWaitAll = FALSE. From MSDN: “When bWaitAll is FALSE, this function checks the handles in the array in order starting with index 0, until one o

  • rails validating multiple child models in a complex form 2009-08-17

    I am using multiple child models in a complex form (a la The form works great, but I need to validate a property of the set of child models before accepting the form data into the database. I've come up

  • Expose single parent/multiple child records as one OData entry 2011-04-03

    I have a typical parent table/child table setup in which a row in the parent table can have multiple child table associated records by way of a foreign key. An example: Parent Table: ID Book 1 Chemistry 2 Biology Child Table: ID ParentId Subject 1 1

  • Selecting an entity with multiple child entities 2011-05-13

    Using Active Record/NHibernate, I'm trying to select an entity (Site) which has multiple child collections. There is only one Site with the given siteId, yet the FindAll returns the Site 28 times; it's being duplicated due to the child collections th

  • Problem with Adding multiple child objects to an existing entity in EF 2011-06-07

    I need to add multiple child objects to an existing parent Object. I am instantiating my parent object and sets it Key/Id in my UI processing layer(to which my child objects will be added). Parent parenttoModify = new Parent(); parenttoModify.Parent_

  • Tree with multiple child nodes and next node 2011-06-17

    I want to build a tree with the following characteristics: Every node can have 1 "next node". Every node can have multiple child nodes. The number of child nodes can vary from one node to the other I was thinking of a struct which looked like this: s

  • Is it possible to insert multiple child records using the entity framework? 2011-08-12

    Using entity framework. Is it possible to insert multiple child records. At the moment I am iterating through a list and adding to entity object, but only one/the last object is saved to the database. Should I just create a sproc, or is this possible

  • Drag and drop multiple child node 2012-02-27

    I had populated data in ASP.NET TreeView control. How will I drag multiple child nodes from a TreeView and drop the dragged nodes one to a DIV or any portion, using JQUERY ? I know in jquery, there are methods "draggable" and "droppable" to make this

  • share perl curses UI object variable across multiple child processes 2012-04-24

    I am writing a tool which spawns multiple child processes. In fact 3 levels of child processes to speed up the entire logic/process. To display the output in terminal I have chosen Curses::UI. The curses ui objects/widgets are created at each level o

  • Calculate the number of lines of multiple files , using Fork() , where each child runs solo 2012-04-23

    Following my previous question [here][1] , I now want to calculate the number of lines of multiple files , where to each file I want to place a process ,a child process for that matter , that will run the method calculateLines for his own file , and

  • Multiple child processes reading/writing on the same pipe 2011-08-22

    I am currently learning sockets programming using C in a Linux environment. As a project I am attempting to write a basic chat server and client. The intention is to have the server fork a process for each client that connects. The problem that I am

  • Fork, Parent and child process 2011-11-28

    In C, is it possible to have the forked() process alive indefinitely even after the parent exits? The idea of what I am trying to do is, Parent process forks a child, then exits, child keeps running in background until another process sends it a kill

  • PHP fork without having child inherit the parent's file descriptors? 2008-10-28

    I'm trying to run a shell command using the backtick operators, but the fact that the child process inherits php's open file descriptors is problematic. Is there a way to keep this from happening? I'm running PHP 5.1.2 --------------Solutions--------

  • Combine Multiple child rows into one row MYSQL 2009-07-01

    Thanks in advance, I just can't seem to get it! I have two tables Ordered_Item ID | Item_Name 1 | Pizza 2 | Stromboli Ordered_Options Ordered_Item_ID | Option_Number | Value 1 43 Pepperoni 1 44 Extra Cheese 2 44 Extra Cheese What I am looking to outp

  • Drupal Custom CCK field with multiple child fields 2009-10-28

    Is there a way of creating a composite field that can have multiple values, with each value having another group of composite values? E.g. we want to have this structure at the end: Group 1 (unlimited number of groups) Child field (unlimited children

  • Django: save multiple object signal once 2009-12-21

    I need some help with sending email when an order is placed. To illustrate the problem, following is the abstract code: class Order(models.Model): user = models.ForeignKey(User) class OrderItem(modes.Model): order = models.ForeignKey(Order, related_n

  • C# XMLReader - Multiple child collections failing Validation with XSD schema 2010-02-05

    I have a supplied XSD. I do not know enough about XSD to begin modifying it. <?xml version="1.0" encoding="utf-8"?> <xs:schema xmlns:xsi="" attributeFormDefault="unqualified" elementFormDefault="quali

  • Android Expandable lists having multiple child items in a single row and their onClick event 2010-07-19

    I could not figure out how to implement two basic functionalities with the expandable lists: 1. The onClick for the child 2. Using linear layout for the children, so that I can have multiple clickable items in same row under the parent item Can someb

Copyright (C), All Rights Reserved.

processed in 0.195 (s). 11 q(s)