Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

313
Visualizações
Get stdout of already running process in Linux in C

Right now, I'm having to start an external process in C. I'm currently using posix_spawn to create the process. It is necessary that I can monitor whether or not the process has terminated. I need to also have a link to the standard out of the process. I've looked at using popen, however, it does not provide an "easy" way of getting the pid. I'm slowly going insane as it can't possibly be this hard to get the stdout of a running process in Linux.

Also, on a further note, I need help deciphering what the file_actions parameter is supposed to mean. man(3) for posix_spawn on this topic says:

If file_actions is not NULL, then the file descriptors open in the child process shall be those open in the calling process as modified by the spawn file actions object pointed to by file_actions and the FD_CLOEXEC flag of each remaining open file descriptor after the spawn file actions have been processed.

If that isn't the definition of a run-on sentence, I have no idea what is.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Since you have the PID (returned from posix_spawn) and you are running Linux, you will find the stdout of the process at /proc/<pid>/fd/1. Just open (or fopen) the file for reading.

The standard way is to use fork though. Use pipe and dup2 to get a file descriptor for reading the child's output, as in this question.

over 4 years ago · Santiago Trujillo Relatório

0

You can use posix_spawn for this, without having to use race-condition-prone, Linux-specific /proc/<pid>/fd/N. You can keep all the benefits of posix_spawn.

You were on the right track thinking about file_actions. Below is an example that prints out the child's stdout in Python-style triple quotes, as well as the child's exit code, from the parent process using posix_spawn and file_actions.

Here is an example of the example output.

child pid: 17468
child exit status: 0
child stdout:
"""Hello World!
"""

Here is the example.

#define _DEFAULT_SOURCE
#include <spawn.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>

extern char **environ;

static void dump_child_stdout(int filedes)
{
    ssize_t num_read;
    char buf[1];

    printf("child stdout:\n\"\"\"");
    for (;;)
    {
        num_read = read(filedes, buf, sizeof(buf));
        if (num_read > 0)
        {
            printf("%c", buf[0]);
        }
        else
        {
            break;
        }
    }
    printf("\"\"\"\n");
}

int main(int argc, char *argv[])
{
    int status;
    pid_t pid;
    int out[2];
    posix_spawn_file_actions_t action;
    char *args[] = {"/bin/echo", "Hello World!", NULL };

    posix_spawn_file_actions_init(&action);

    pipe(out);

    posix_spawn_file_actions_adddup2(&action, out[1], STDOUT_FILENO);
    posix_spawn_file_actions_addclose(&action, out[0]);

    status = posix_spawn(&pid, args[0], &action, NULL, args, environ);
    if (status == 0)
    {
        printf("child pid: %d\n", pid);
        if (waitpid(pid, &status, 0) < 0)
        {
            perror("waitpid");
        }
        else
        {
            if (WIFEXITED(status))
            {
                printf("child exit status: %d\n", WEXITSTATUS(status));
            }
            else
            {
                printf("child died an unnatural death.\n");
            }

            close(out[1]);
            dump_child_stdout(out[0]);
        }
    }
    else
    {
        fprintf(stderr, "posix_spawn: %s\n", strerror(status));
        close(out[1]);
    }

    posix_spawn_file_actions_destroy(&action);

    return 0;
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda