簡體   English   中英

從wexitstatus收到錯誤的退出代碼

[英]Wrong exit code received from wexitstatus

我正在使用PCNTL在ubuntu服務器上的PHP中對一個大腳本進行多處理。
這是代碼(經過簡化和注釋)

function signalHandler($signo = null) {
    $pid = posix_getpid();
    switch ($signo) {
        case SIGTERM:
        case SIGINT:
        case SIGKILL:
            // a process is asked to stop (from user or father)
            exit(3);
            break;
        case SIGCHLD:
        case SIGHUP:
            // ignore signals
            break;
        case 10: // signal user 1
            // a process finished its work
            exit(0);
            break;
        case 12: // signal user 2
            // a process got an error.
            exit(3);
            break;
        default:
            // nothing
    }
}

public static function run($nbProcess, $nbTasks, $taskFunc, $args) {
    $pid = 0;
    // there will be $nbTasks tasks to do, and no more than $nbProcess children must work at the same time
    $MAX_PROCESS = $nbProcess;
    $pidFather = posix_getpid();

    $data = array();

    pcntl_signal(SIGTERM, "signalHandler");
    pcntl_signal(SIGINT, "signalHandler");
//  pcntl_signal(SIGKILL, "signalHandler"); // SIGKILL can't be overloaded
    pcntl_signal(SIGCHLD, "signalHandler");
    pcntl_signal(SIGHUP, "signalHandler");
    pcntl_signal(10, "signalHandler"); // user signal 1
    pcntl_signal(12, "signalHandler"); // user signal 2

    for ($indexTask = 0; $indexTask < $nbTasks ; $indexTask++) {
        $pid = pcntl_fork();
        // Father and new child both read code from here

        if ($pid == -1) {
            // log error
            return false;
        } elseif ($pid > 0) {
            // We are in father process
            // storing child id in an array
            $arrayPid[$pid] = $indexTask;
        } else {
            // We are in child, nothing to do now
        }

        if ($pid == 0) {
            // We are in child process

            $pidChild = posix_getpid();

            try {
                //$taskFunc is an array containing an object, and the method to call from that object
                $ret = (array) call_user_func($taskFunc, $indexTask, $args);// similar to $ret = (array) $taskFunc($indexTask, $args);

                $returnArray = array(
                                    "tasknb" => $indexTask,
                                    "time" => $timer,
                                    "data" => $ret,
                );
            } catch(Exception $e) {
                // some stuff to exit child
            }

            $pdata = array();
            array_push($pdata, $returnArray);
            $data_str = serialize($pdata);

            $shm_id = shmop_open($pidChild, "c", 0644, strlen($data_str));
            if (!$shm_id) {
                // log error
            } else {
                if(shmop_write($shm_id, $data_str, 0) != strlen($data_str)) {
                    // log error
                }
            }
            // We are in a child and job is done. Let's exit !
            posix_kill($pidChild, 10); // sending user signal 1 (OK)
            pcntl_signal_dispatch();
        } else {
            // we are in father process,
            // we check number of running children
            while (count($arrayPid) >= $MAX_PROCESS) {
                // There are more children than allowed
                // waiting for any child to send signal
                $pid = pcntl_wait($status);
                // A child sent a signal !

                if ($pid == -1) {
                    // log error
                }

                if (pcntl_wifexited($status)) {
                    $statusChild = pcntl_wexitstatus($status);
                } else
                    $statusChild = $status;

                // father ($pidFather) saw a child ($pid) exiting with status $statusChild (or $status ?)
                //                                                                ^^^^          ^^^^^^
                //                                                                (=3)  (= random number ?)
                if(isset($arrayPid[$pid])) {
                    // father knows this child
                    unset($arrayPid[$pid]);
                    if ($statusChild == 0 || $statusChild == 10 || $statusChild == 255) {
                        // Child did not report any error
                        $shm_id = shmop_open($pid, "a", 0, 0);
                        if ($shm_id === false)
                            // log error
                        else {
                            $shm_data = unserialize(shmop_read($shm_id, 0, shmop_size($shm_id)));
                            shmop_delete($shm_id);
                            shmop_close($shm_id);
                            $data = array_merge($data, $shm_data);
                        }
                        // kill (again) child
                        posix_kill($pid, 10);
                        pcntl_signal_dispatch();;
                    }
                    else {
                        // Child reported an error
                    }
                }
            }
        }
    }
}

我面臨的問題與wexitstatus返回的值有關。
為簡單起見,有一個父進程,必須創建200個線程。
他一次使一個進程,如果實際運行的線程超過8個,則等待一個進程完成。
我添加了許多日志,所以我看到一個孩子完成了工作。
我看到它叫posix_kill($pidChild, 10);
我看到信號處理程序被signal user 1調用(這導致exit(0) )。
我看到父親醒來,但是當他從wexitstatus獲取返回的代碼時,他看到了代碼3,因此認為孩子出錯了,而它退出了,代碼為0 !!。
pid是好孩子的pid。
也許我誤會了信號的工作原理……有什么線索嗎?

我發現了問題。
在我的應用程序中,使用register_shutdown_function(myFrameworkShutdownFunction) “平穩”地關閉了腳本。
因此, exit(0)不會立即停止子進程。 它首先進入myFrameworkShutdownFunction ,並將返回代碼0轉換為代碼3(由於變量配置錯誤)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM