c - 在 while 循环中使用 execvp 和 fork 时无限循环

标签 c fork execvp

我正在尝试编写一个小的 shell 程序,我希望它在调用另一个程序(通过 execvp())后保持事件状态。我希望 fork 系统调用“复制”进程,创建发行(父)进程的几乎相同的副本(子)。因此,当父进程因 excevp() 调用而终止时,子进程仍将存活。

在我附在下面的代码中,我读取了用户输入(程序和要执行的参数),然后我希望父叉将执行程序,子叉将等待用户的下一个输入,但是在我第一次插入输入(要执行的程序及其参数)时,程序陷入无限循环。当子 fork 获得控制权时,它会执行之前的程序并且不会等待新的输入(似乎在第一个 fgets() 之后它不再等待用户输入)。

这是代码中有问题的部分:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "LineParser.h"
#include <limits.h>
#include <unistd.h>

char buf[PATH_MAX];
char buf_user[2048];
int pid;

void execute(cmdLine* pCmdLine) {
   int pid = fork();
   if(pid != 0) {
     printf("I'm the parent.\n");
       if(execvp(pCmdLine->arguments[0], pCmdLine->arguments) == -1) {
         perror("execute failed");
         _exit(1);
       }
   } else {
     freeCmdLines(pCmdLine);        
   }
}

int main(int argc, char *argv[])
{  
    while(1) {
        getcwd(buf, PATH_MAX);
        printf("%s> ", buf);
        fgets(buf_user, 2048, stdin);
        fflush(stdin);
        if(strcmp(buf_user, "quit\n") == 0) {
            exit(0);
        }
        cmdLine* parsedCmd = parseCmdLines(buf_user);
        execute(parsedCmd);
    }
    return 0;
}

cmdLine结构:

#define MAX_ARGUMENTS 256

typedef struct cmdLine
{
    char *const arguments[MAX_ARGUMENTS]; /* command line arguments (arg 0 is the command) */
    int argCount;       /* number of arguments */
    char const *inputRedirect;  /* input redirection path. NULL if no input redirection */
    char const *outputRedirect; /* output redirection path. NULL if no output redirection */
    char blocking;  /* boolean indicating blocking/non-blocking */
    int idx;                /* index of current command in the chain of cmdLines (0 for the first) */
    struct cmdLine *next;   /* next cmdLine in chain */
} cmdLine;

/* Parses a given string to arguments and other indicators */
/* Returns NULL when there's nothing to parse */ 
/* When successful, returns a pointer to cmdLine (in case of a pipe, this will be the head of a linked list) */
cmdLine *parseCmdLines(const char *strLine);    /* Parse string line */

/* Releases all allocated memory for the chain (linked list) */
void freeCmdLines(cmdLine *pCmdLine);       /* Free parsed line */

/* Replaces arguments[num] with newString */
/* Returns 0 if num is out-of-range, otherwise - returns 1 */
int replaceCmdArg(cmdLine *pCmdLine, int num, const char *newString);

最佳答案

您不应该在父级中执行 execvp,而应在子级中执行。

您的终端等待父级完成。在不等待其子项结束的情况下完成父项会导致 zombie process .

修改您的代码以便在子级中执行命令并等待它在父级中完成,这在我测试时解决了您的问题。

我建议您阅读ma​​n 3 wait!

关于c - 在 while 循环中使用 execvp 和 fork 时无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55774920/

相关文章:

c++ - 最大subarray_problem理解

c - 选择数组中的范围,C

c - 文本未打印在输出上

node.js - fork Electron 过程需要 "empty" Electron

c - 如何正确退出子进程?

ubuntu - g++: 尝试执行 'cc1plus' 时出错:execvp: 没有这样的文件或目录

c - 如何在 if-else 语句中使用多个 execvp 调用?

c - PTHREAD_COND_INITIALIZER - 函数 pthread_cond_wait() 分支到慢速路径

shell - 无法将 ‘const char*’ 转换为 ‘char* const*’

c - 帮助在 C 中实现 GNU Readline