如何在execlp()之后找到程序的返回值? [英] How to find the return value of a program after execlp()?

查看:503
本文介绍了如何在execlp()之后找到程序的返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c中有以下代码:

execlp("ReturnValue.c");

ReturnValue.c应该返回0到3之间的整数,并且由于execlp在程序成功运行时不会返回任何内容,我如何获得该程序的返回值?

ReturnValue.c should return an integer between 0-3, and since execlp won't return anything when the program successfully runs, how can I get the return value of that program?

推荐答案

您可以执行以下操作:

pid = fork();

if (pid == 0) {
    execlp("ReturnValue.c", ...);
}
else if (pid > 0) {
    waitpid(pid, &status, ...);

    /* extract the return status */
    WEXITSTATUS(status);
}
else { /* fork() error */ }

请参见 waitpid(3) man 3 waitpid

这篇关于如何在execlp()之后找到程序的返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆