excevp 不接管子进程(C++)

问题描述 投票:0回答:1

我试图在我的代码中将 stockfish 引擎作为子进程运行。我想使用 fork() 和 execvp() 来做到这一点。我通过运行命令将 stockfish 可执行文件的位置临时添加到我的路径中

export PATH="~/Apps/stockfish_15_linux_x64_avx2:$PATH"

我检查以确保通过运行

echo $PATH
将其添加到PATH。我还可以在当前终端会话期间从任何目录运行 stockfish 可执行文件,因此这部分似乎可以正常工作。我遇到的问题是使用 execvp 从另一个可执行文件运行 stockfish。

我有以下重现错误的代码。

#include <iostream> 
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>

int main(void ) { 
    const char* argv[] = {"stockfish_15_x64_avx2", "d", NULL};
    int pid = fork(); 

    if (pid < 0) { 
        printf("Failed to fork");
    }

    else if (pid == 0){ 
        execvp(argv[0], (char* const*)argv); 
    }

    sleep( 1 ); 



    
    printf("Finished executing the parent process \n"
    " -- The child won't get here -- you will only see this once. \n");

    return 0;
}

stockfish 中的

d
命令显示当前位置的图表。在这种情况下,这将是起始位置。但是我得到的不是这张图,而是简单的

 Finished executing the parent process 
 -- The child won't get here -- you will only see this once. 
Finished executing the parent process 
 -- The child won't get here -- you will only see this once.

这告诉我 execvp 实际上并没有接管子进程。当我将参数更改为更通用的命令(如

argv[] = {"ls", "-a", NULL}
)时,代码将按预期工作。知道这里发生了什么吗?

c++ linux exec parent-child child-process
1个回答
0
投票

所以我开始使用 waitpid 来做一些错误处理,当我预感可执行文件名称中的下划线是某种问题时。果然,当我将可执行文件重命名为“stockfish15”时,一切都按预期工作。

代码现在看起来像这样

#include <iostream> 
#include <sys/types.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>

int main(void ) { 
    const char* argv[] = {"stockfish15", "d", NULL};
    pid_t pid = fork(); 
    if (pid == -1) { 
        printf("Failed to fork");
    }
    else if (pid == 0) { 
        execvp(argv[0], (char* const*) argv);
    }

    int status;
    waitpid(pid, &status, 0); 

    if (WIFEXITED(status)) { 
        printf("child terminated normally\n");
    }

    else {printf("exevp error\n");}
    
    
    printf("Finished executing the parent process \n"
    " -- The child won't get here -- you will only see this once. \n");

    return 0;
}

输出是

Stockfish 15 by the Stockfish developers (see AUTHORS file)

 +---+---+---+---+---+---+---+---+
 | r | n | b | q | k | b | n | r | 8
 +---+---+---+---+---+---+---+---+
 | p | p | p | p | p | p | p | p | 7
 +---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   | 6
 +---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   | 5
 +---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   | 4
 +---+---+---+---+---+---+---+---+
 |   |   |   |   |   |   |   |   | 3
 +---+---+---+---+---+---+---+---+
 | P | P | P | P | P | P | P | P | 2
 +---+---+---+---+---+---+---+---+
 | R | N | B | Q | K | B | N | R | 1
 +---+---+---+---+---+---+---+---+
   a   b   c   d   e   f   g   h

Fen: rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Key: 8F8F01D4562F59FB
Checkers: 
child terminated normally
Finished executing the parent process 
 -- The child won't get here -- you will only see this once. 

不过,我想知道为什么会这样。

© www.soinside.com 2019 - 2024. All rights reserved.