Видимость возвращаемого значения функции после вызова fork () - PullRequest
0 голосов
/ 12 января 2020

При использовании fork() для создания нового дочернего процесса мы получаем обратно pid_t, который указывает, находимся ли мы в дочернем процессе (pid == 0) или в родительском процессе (pid > 0) после вызова fork() заканчивается. У меня есть небольшая проблема с тем, чтобы обдумать это в целом, но я изо всех сил пытаюсь найти ответ на следующий вопрос:

  • Когда вызов функции fork() в функции, будет вызывающий код этой функции (всегда) видит возвращаемые значения из child зелья кода после fork()?

Чтобы проиллюстрировать, смотрите следующую функцию, которую я написал. Я не уверен, что когда-либо вызывающий процесс / код получит одно из возвращаемых значений -1, которые могут быть возвращены из части кода else if (pid == 0) { ... }.

/*
 * Opens the process `cmd` similar to popen() but does not invoke a shell.
 * Instead, wordexp() is used to expand the given command, if necessary.
 * If successful, the process id of the new process is being returned and the 
 * given FILE pointers are set to streams that correspond to pipes for reading 
 * and writing to the child process, accordingly. Hand in NULL for pipes that
 * should not be used. On error, -1 is returned.
 */
pid_t popen_noshell(const char *cmd, FILE **out, FILE **err, FILE **in)
{
    if (!cmd || !strlen(cmd)) return -1;

    // 0 = read end of pipes, 1 = write end of pipes
    int pipe_stdout[2];
    int pipe_stderr[2];
    int pipe_stdin[2];

    if (out && (pipe(pipe_stdout) < 0)) return -1;
    if (err && (pipe(pipe_stderr) < 0)) return -1;
    if (in  && (pipe(pipe_stdin)  < 0)) return -1;

    pid_t pid = fork();
    if (pid == -1)
    {
        return -1;
    }
    else if (pid == 0) // child
    {    
        // redirect stdout to the write end of this pipe
        if (out)
        {
            if (dup2(pipe_stdout[1], STDOUT_FILENO) == -1) return -1;
            close(pipe_stdout[0]); // child doesn't need read end
        }
        // redirect stderr to the write end of this pipe
        if (err)
        {
            if (dup2(pipe_stderr[1], STDERR_FILENO) == -1) return -1;
            close(pipe_stderr[0]); // child doesn't need read end
        }
        // redirect stdin to the read end of this pipe
        if (in)
        {
            if (dup2(pipe_stdin[0], STDIN_FILENO) == -1) return -1;
            close(pipe_stdin[1]); // child doesn't need write end
        }

        wordexp_t p;
        if (wordexp(cmd, &p, 0) != 0) return -1;

        execvp(p.we_wordv[0], p.we_wordv);
        _exit(1);
    }
    else // parent
    {
        if (out)
        {
            close(pipe_stdout[1]); // parent doesn't need write end
            *out = fdopen(pipe_stdout[0], "r");
        }
        if (err)
        {
            close(pipe_stderr[1]); // parent doesn't need write end
            *err = fdopen(pipe_stderr[0], "r");
        }
        if (in)
        {
            close(pipe_stdin[0]); // parent doesn't need read end
            *in = fdopen(pipe_stdin[1], "w");
        }
        return pid;
    }
}

1 Ответ

2 голосов
/ 12 января 2020

fork копирует весь стек вызовов, поэтому, если один из этих операторов return -1 выполняется в дочернем элементе, он будет возвращен вызывающей стороне popen_noshell в дочернем элементе (а также в родительском элементе). Это, вероятно, не то, что вы хотите, и их, вероятно, следует заменить на _exit(-1)

...