gpt4 book ai didi

c - 用C为linux写一个shell,某个进程的exec是无限循环的

转载 作者:太空宇宙 更新时间:2023-11-04 08:46:31 25 4
gpt4 key购买 nike

这是相关的代码片段:

if(!strcmp(args[0],"run")){
pid_t pid = fork();
if(pid == 0){
execvp(args[1], args);
fprintf(stdout, "Process could not be found in this directory\n");
kill((int)getpid(), SIGKILL);
}
else{
if(pid < 0)
exit(1);

fprintf(stdout, "PID of Process = %d\n", pid);
int success = waitpid(pid, &childExitStatus, WUNTRACED | WCONTINUED);

if(success == -1)
exit(EXIT_FAILURE);
}

}

现在,当我运行 Libre Office 数学之类的进程时,它只会打开它的一个实例。但是,当我尝试使用此代码打开 xterm 时,它将继续一遍又一遍地执行,在我执行中断和退出之前创建许多 xterm 实例。我没有看到任何会导致这种情况发生的循环。对为什么会这样有任何见解吗?

最佳答案

execvp() 调用不正确,因为它在开始时传递了一个附加参数“run”。这样执行xterm时,效果类似于xterm xterm,xterm使用xterm作为shell。新的 xterm 继承了一个 SHELL 环境变量,使它启动另一个 xterm,直到限制用完。

关于c - 用C为linux写一个shell,某个进程的exec是无限循环的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21486123/

25 4 0