#!/usr/bin/php 0) $running_jobs--; print posix_getpid() . ": SIGCHLD received, jobs left: $running_jobs\n"; pcntl_waitpid(-1, $status, WNOHANG); } function sigint_handler() { unlink(LOCK_DIRECTORY . "/update_daemon.lock"); die("Received SIGINT. Exiting.\n"); } pcntl_signal(SIGCHLD, 'sigchld_handler'); pcntl_signal(SIGINT, 'sigint_handler'); if (file_is_locked("update_daemon.lock")) { die("error: Can't create lockfile. ". "Maybe another daemon is already running.\n"); } if (!pcntl_fork()) { $lock_handle = make_lockfile("update_daemon.lock"); if (!$lock_handle) { die("error: Can't create lockfile. ". "Maybe another daemon is already running.\n"); } while (true) { sleep(100); } } while (true) { $next_spawn = $last_checkpoint + SPAWN_INTERVAL - time(); if ($next_spawn % 10 == 0) { print "[MASTER] active jobs: $running_jobs, next spawn at $next_spawn sec\n"; } if ($last_checkpoint + SPAWN_INTERVAL < time()) { for ($j = $running_jobs; $j < MAX_JOBS; $j++) { print "[MASTER] spawning client $j..."; $pid = pcntl_fork(); if ($pid == -1) { die("fork failed!\n"); } else if ($pid) { $running_jobs++; print "OK [$running_jobs]\n"; } else { pcntl_signal(SIGCHLD, SIG_IGN); pcntl_signal(SIGINT, SIG_DFL); passthru(PHP_EXECUTABLE . ' ' . CLIENT_PROCESS); exit(0); } } $last_checkpoint = time(); } sleep(1); } ?>