如何控制在C程序EXECL()调用后父进程的执行? [英] How to control execution of parent process after execl() call in C program?

查看:322
本文介绍了如何控制在C程序EXECL()调用后父进程的执行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有执行应用程序中使用fork()和EXECL简单的C程序()。如果execl的()无法运行该应用程序,那我也来调用子进程的父进程的函数和退出。如果execl的()成功运行应用程序,那我也显示从父进程取得成功日志。因此,父进程应该等待孩子的EXECL()调用(只是调用,而不是直到应用程序的执行结束),获取有关其状态的信息,然后再做出决定,并继续自己的执行。这里是我的code。

I have simple C program which executes an application using fork() and execl(). If execl() fails to run the application, then I have to call a function in the parent process and exit from the child process. If execl() successfully runs the application, then I have show a success log from the parent process. So, parent process should wait for the child's execl() call (just the call, not till the end of execution of the application), get some information about it status, and then make decisions and continue its own execution. Here is my code.

int main()
{
    int iExecRetVal, pid;
    pid = fork();

    if (pid == -1)
    {

    }
    else if (pid > 0)
    {
    }
    else
    {
        iExecRetVal = execl("./flute-static", "./flute-static", "-send", "-a192.168.190.1/6666", "JFlute.1.2.tar.gz", NULL);
        if (iExecRetVal == -1)
        {
            /*execl() failed, need some error handling in the parent process*/
        }
        _exit(0);
    }

    /*Parent's normal execution*/
}

int HandleSuccessFromParent()
{
    /*Should be called when exec call was successful*/
}

int HandleFailureFromParent()
{
    /*Should be called when exec call was NOT successful*/
}

我们知道execl的()不会成功返回。那么,如何调用HandleSuccessFromParent()和HandleFailureFromParent()函数中的孩子execl的()调用后正常。请帮我。

We know execl() does not return on success. So, how to call HandleSuccessFromParent() and HandleFailureFromParent() functions properly after the execl() call in the child. Please help me.

推荐答案

子进程需要与一个错误状态退出(非零; 1是共同的, EXIT_FAILURE 是标准的C)。

The child process needs to exit with an error status (non-zero; 1 is common, EXIT_FAILURE is standard C).

父进程需要等待子结束,并捕捉到孩子的退出状态,使用的 等待()
waitpid函数()

The parent process needs to wait for the child to finish, and capture the child's exit status, using wait() or waitpid().

如果您需要了解孩子是否死亡,但不想等待它完成,使用 waitpid函数() WNOHANG 一个小小的停顿后,让孩子尝试和运行(一分一秒的延迟可能是足够长的时间)。

If you need to know whether the child died but don't want to wait for it to complete, use waitpid() with WNOHANG after a small pause to let the child try and run (a sub-second delay is likely to be long enough).

这篇关于如何控制在C程序EXECL()调用后父进程的执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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