zxCoder
V2EX  ›  问与答

关于 execlp()和 waitpid()的一个问题

  •  
  •   zxCoder · Sep 12, 2020 · 836 views
    This topic created in 2071 days ago, the information mentioned may be changed or developed.

    我在读一个 OJ 判题核心的代码,里面关于特判的代码,fork 了一个子进程,调用 execlp()去执行 spj,没有报错,但是父进程里 waitpid()就报错了,返回值是-1,错误码 10,no child processes,这是怎么回事

        int status = 0;
        pid_t pid_spj = fork();
        if (pid_spj < 0) {
            LOG_WARNING("error for spj failed, %d:%s", errno, strerror(errno));
            output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_COMPARE_SPJ_FORK);
            exit(judge_conf::EXIT_COMPARE_SPJ_FORK);
        } else if (pid_spj == 0) {
            freopen(file_spj.c_str(), "w", stdout);
            if (EXIT_SUCCESS == malarm(ITIMER_REAL, judge_conf::spj_time_limit)) {
                log_close();
                if (execlp((spj_path + "/" + problem::spj_exe_file).c_str(),
                           problem::spj_exe_file.c_str(), input_file.c_str(),
                           output_file.c_str(), output_file_std.c_str(), NULL) < 0) {
                    printf("spj execlp error\n");
                }
            }
        } else {
            //no child process
            if (waitpid(pid_spj, &status, 0) < 0) {
                LOG_BUG("waitpid failed, %d:%s", errno, strerror(errno));
                output_result(judge_conf::OJ_SE, -errno, judge_conf::EXIT_COMPARE_SPJ_WAIT);
                exit(judge_conf::EXIT_COMPARE_SPJ_WAIT);
            }
        }
    
    
    1 replies    2020-09-12 11:38:56 +08:00
    Nitroethane
        1
    Nitroethane  
       Sep 12, 2020
    错误码给你说了啊,没有子进程。waitpid 函数是父进程等待子进程的结束,如果返回 ECHILD,也就是 10,说明调用进程没有 PID 为 pid_spj 的子进程
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1519 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 35ms · UTC 16:47 · PVG 00:47 · LAX 09:47 · JFK 12:47
    ♥ Do have faith in what you're doing.