Example #1
0
File: fork.php Project: xqy/php
function fork_one()
{
    $pid = pcntl_fork();
    if ($pid == 0) {
        //子进程处理
        do_task();
        exit;
    } else {
        if ($pid > 0) {
            //主进程
            $pids[$pid] = $pid;
            //            $shmid = shmop_open(864, 'c', 0755, 1024);
            //            shmop_write($shmid, "Hello World!", 0);
            //            shmop_close($shmid);
            posix_kill($pid, SIGUSR1);
        }
    }
}
function callback($id)
{
    global $num_runs;
    global $dsn_map;
    $db_map = array();
    $db_map[master] =& MDB2::connect($dsn_map[master]);
    if (PEAR::isERROR($db_map[master])) {
        die($db_map[master]->getMessage());
    }
    if (is_array($dsn_map[slave])) {
        foreach ($dsn_map[slave] as $dsn_slave) {
            $db_slave =& MDB2::connect($dsn_slave);
            if (PEAR::isERROR($db_slave)) {
                die($db_slave->getMessage());
            }
            $db_map[slave][] = $db_slave;
        }
    }
    $cnt = 0;
    $times = array();
    // wait for the parent to HUP me
    pcntl_signal(SIGHUP, create_function('', 'return 1;'));
    sleep(600);
    while ($cnt < $num_runs) {
        $t0 = microtime(true);
        do_task($db_map);
        $t1 = microtime(true) - $t0;
        $times[] = $t1;
        $cnt++;
    }
    // cleanup
    $db_map[master]->disconnect;
    if (is_array($db_map[slave])) {
        foreach ($db_map[slave] as $db_slave) {
            $db_slave->disconnect;
        }
    }
    $num = count($times);
    $tot = array_sum($times);
    $avg = $tot / $num;
    $r = array($id, $num, min($times), max($times), $avg, $tot);
    return $r;
}
function callback($id)
{
    global $num_runs;
    global $dsn_map;
    $db_map = array();
    $db_map[master] = mysql_connect($dsn_map[master][0], $dsn_map[master][1], $dsn_map[master][2]);
    mysql_select_db($dsn_map[master][3], $db_map[master]);
    if (is_array($dsn_map[slave])) {
        foreach ($dsn_map[slave] as $dsn_slave) {
            $db_slave = mysql_connect($dsn_slave[0], $dsn_slave[1], $dsn_slave[2]);
            mysql_select_db($dsn_slave[3], $db_slave);
            $db_map[slave][] = $db_slave;
        }
    }
    $cnt = 0;
    $times = array();
    // wait for the parent to HUP me
    pcntl_signal(SIGHUP, create_function('', 'return 1;'));
    sleep(600);
    while ($cnt < $num_runs) {
        $t0 = microtime(true);
        do_task($db_map);
        $t1 = microtime(true) - $t0;
        $times[] = $t1;
        $cnt++;
    }
    // cleanup
    mysql_close($db_map[master]);
    if (is_array($db_map[slave])) {
        foreach ($db_map[slave] as $db_slave) {
            mysql_close($db_slave);
        }
    }
    $num = count($times);
    $tot = array_sum($times);
    $avg = $tot / $num;
    $r = array($id, $num, min($times), max($times), $avg, $tot);
    return $r;
}