Matt
Matt

Reputation: 2350

How can I redirect stdout back to the terminal in a multi process c program?

I'm trying to write a c program that is the equivalent of the linux command ps -aux | sort -r -n -k 5 but I'm not getting any output

Here's my code

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int main(int argc, char ** argv){
    int pipes[2];
    int r;
    r = pipe(pipes);
    if (r < 0) {
        fprintf(stderr, "pipe failed\n\n"); // stderr is a FILE* variable for the standard error file (terminal) 
        exit(2);
    }
    int saved_stdout = dup(1);
    int pid = fork();
    if(pid > 0){
        // Parent
        pid = fork();
        if(pid > 0){
            // Parent
            wait(NULL);
        }else if (pid == 0){
            // Child 1
        printf("Child 1\n");

        dup2(pipes[1], 1);
        close(pipes[0]);
        close(pipes[1]);


        execlp("/bin/ps", "ps", "-aux", (char*) NULL);
        exit(0);

        }else{
            fprintf(stderr, "FORK FAILED\n\n");
            return 1;
        }
    }else if (pid == 0){
        // Child 2
        printf("Child 2\n");
        dup2(pipes[0], 0);
        close(pipes[0]);
        close(pipes[1]);
        dup2(saved_stdout, 1);
        close(saved_stdout);
        execlp("/bin/sort", "sort", "-r", "-n", "-k", "5", (char*)NULL);
        exit(0);
    }else{
        fprintf(stderr, "FORK FAILED\n\n");
        return 1;
    }
    wait(NULL);
    printf("Exiting parent\n");
}

The output I get is this

Child 1
Child 2
Exiting parent

I doesn't actually print the execlp command, I've tried saving stdout to variable saved_stdout which is the solution I found in another answer, but that doesn't seem to work.

How can I redirect stdout back to the terminal?

Upvotes: 1

Views: 241

Answers (1)

Stargateur
Stargateur

Reputation: 26697

Strange my output with your code is:

Child 1
Child 2

and the program don't stop. Or you sure that your output is valid ?

Whatever, your problem is that you don't close your pipe in your parents. Just add:

close(pipes[0]);
close(pipes[1]);

In your both parents (before your two call to wait()).

Plus saved_stdout is useless in your case, because you only change stdout in your child1. saved_stdout and 1 describe the same file in your child2.

Upvotes: 3

Related Questions