简体   繁体   English

我很困惑在这个使用fork()克隆进程的示例函数中如何处理execvp()

[英]I'm confused how this execvp() is handled in this sample function which uses fork() to clone a process

I have the following function from a book titled "Advanced Linux Programming". 我从一本名为“高级Linux编程”的书中获得以下功能。

int spawn (char* program, char** arg_list)
{
    pid_t child_pid;

    /* Duplicate this process. */
    child_pid = fork ();

    if (child_pid != 0)
    /* This is the parent process. */
    return child_pid;

    else {
    /* Now execute PROGRAM, searching for it in the path. */
    execvp (program, arg_list);

    /* The execvp function returns only if an error occurs. */
    fprintf (stderr, “an error occurred in execvp\n”);
    abort ();
    }
}

But I'm confused that, in cases where ls is executed successfully, the error is not printed, but in case it fails, it prints the error which is put in the line following it. 但是我感到困惑的是,在成功执行ls情况下,不会打印错误,但是在失败的情况下,它会打印错误并放置在其后一行。

My Question This line fprintf (stderr, “an error occurred in execvp\\n”); 我的问题这行fprintf (stderr, “an error occurred in execvp\\n”); is after the execvp() function, and it is expected to be executed after the execution of execvp() finishes, but it is not the case, and it is executed only if execvp() encounters an error. 是在execvp()函数之后,并且有望在execvp()的执行完成后执行,但是不是这种情况,只有在execvp()遇到错误时才执行。 It seems the function spawn() finishes as soon as it executes execvp() successfully. 似乎函数spawn()一旦成功执行execvp()完成了。 Am I right? 我对吗?

You can have a look at the manpage for execvp , it says: 您可以查看execvp的联机帮助页,其中显示:

The exec() family of functions replaces the current process image with a new process image. exec()系列函数将当前过程映像替换为新的过程映像。

So, what does that mean? 那是什么意思呢? It means, if execvp succeeds, your program wont be in memory anymore, thus it wont ever reach the error message. 这意味着,如果execvp成功,则您的程序将不再在内存中,因此将永远不会出现错误消息。 Your program in memory will be replaced by the new program (in your case ls if i understood it correctly). 您内存中的程序将被新程序替换(如果我正确理解,则为ls )。

So, if your program is able to reach the error message printout, then the execvp function will have failed. 因此,如果您的程序能够到达错误消息打印输出,则execvp函数将失败。 Otherwise the other program starts execution. 否则,其他程序将开始执行。

The reason why your programm will be still running is the fork command, which creates a copy of the process image, so you will be having two same processes running of which only one will be replaced by the command you try to execute. 您的程序将继续运行的原因是fork命令,该命令创建了进程映像的副本,因此您将同时运行两个相同的进程,其中只有一个将被您尝试执行的命令替换。 This is achieved by the if clause if (child_pid != 0) , as the fork command will duplicate the process and return the new Process ID ( PID ). 这是通过if子句if (child_pid != 0) ,因为fork命令将复制该进程并返回新的Process ID( PID )。 If this is set to 0 (see man 3 fork ), then its the new child process, if its != 0 its the parent process. 如果将其设置为0 (请参见man 3 fork ),则其为新的子进程,如果其!= 0为其父进程。 Your function there only executes execvp if its the child process, the parent process encounters an early return. 您的函数仅在其子进程,父进程遇到早期返回时才执行execvp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM