C语言 如何正确使用fork、exec、wait

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19099663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:35:09  来源:igfitidea点击:

how to correctly use fork, exec, wait

cshellexec

提问by Collin

The shell i'm writing needs to execute a program given to it by the user. Here's the very shortened simplified version of my program

我正在编写的 shell 需要执行用户提供给它的程序。这是我的程序的非常缩短的简化版本

int main()
{
    pid_t pid = getpid(); // this is the parents pid

    char *user_input = NULL;
    size_t line_sz = 0;
    ssize_t  line_ct = 0; 

    line_ct = getline(&user_input, &line_sz, stdin); //so get user input, store in user_input

    for (;;)
    {
        pid_t child_pid = fork(); //fork a duplicate process

        pid_t child_ppid = getppid(); //get the child's parent pid

        if (child_ppid == pid) //if the current process is a child of the main process
        {
            exec(); //here I need to execute whatever program was given to user_input
            exit(1); //making sure to avoid fork bomb
        }

        wait(); //so if it's the parent process we need to wait for the child process to finish, right?

    }
}
  1. Have I forked the new process & checked to see if it's a child process correctly
  2. What exec could I use here for what I'm trying to do? What is the most simple way
  3. What are my arguments to wait? the documentation I'm looking at isn't helping much
  1. 我是否分叉了新进程并检查它是否是正确的子进程
  2. 我可以在这里使用什么 exec 来完成我想要做的事情?最简单的方法是什么
  3. 我等待的理由是什么?我正在查看的文档没有多大帮助

Assume the user might input something like ls, ps, pwd

假设用户可能会输入诸如 ls、ps、pwd 之类的内容

Thanks.

谢谢。

Edit:

编辑:

const char* hold = strdup(input_line);
char* argv[2]; 

argv[0] = input_line;
argv[1] = NULL;

char* envp[1];
envp[0] = NULL;

execve(hold, argv, envp);

回答by Kerrek SB

Here's a simple, readable solution:

这是一个简单易读的解决方案:

pid_t parent = getpid();
pid_t pid = fork();

if (pid == -1)
{
    // error, failed to fork()
} 
else if (pid > 0)
{
    int status;
    waitpid(pid, &status, 0);
}
else 
{
    // we are the child
    execve(...);
    _exit(EXIT_FAILURE);   // exec never returns
}

The child can use the stored value parentif it needs to know the parent's PID (though I don't in this example). The parent simply waits for the child to finish. Effectively, the child runs "synchronously" inside the parent, and there is no parallelism. The parent can query statusto see in what manner the child exited (successfully, unsuccessfully, or with a signal).

如果孩子parent需要知道父母的 PID(虽然我在这个例子中没有),它可以使用存储的值。父母只是等待孩子完成。实际上,子进程在父进程内部“同步”运行,没有并行性。父级可以查询status以查看子级退出的方式(成功、不成功或有信号)。