Itega
Itega

Reputation: 43

Restore stdin after a pipe

I made a pipe using stdin and stdout to communicate but I can't figure out how to restore stdin after closing it in my father process.

Here is an example :

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

void readPBM(char *output)
{
    char tmp[1024];

    int fd[2] = {0,0};
    int pid;

    //Open the pipe for inter-process communication
    pipe(&fd[0]);

    //Fork and test if we are child or parent process
    pid = fork();
    if(pid) //Parent process
    {
        wait(NULL); //Wait for child's end
        close(fd[1]);//Close pipe's write stream
        close(0);//Close stdin
        dup(fd[0]);
        close(fd[0]);

        strcpy(output, "");// Init output at 0
        while(fgets(tmp, 1024, stdin) != NULL) //Put remainings entry in output
        {
            strcat(output, tmp);
        }
        strcat(output, "It works\n");
    }
    else if(pid == 0) //Child process
    {
        close(fd[0]);//Close pipe's read stream
        close(1);//Close stdout
        dup(fd[1]);//Duplicate stdin
        close(fd[1]);

        printf("A random string ...\n");
        exit(0);
    }
    else //Print error if fork failed
    {
        printf("Error creating a new process");
        exit(EXIT_FAILURE);
    }
}

int main(int argc, char *argv[])
{
    int i, j;

    char *str = NULL;
    char c;

    str = malloc(512 * sizeof(char*));
    readPBM(str);

    printf("%s", str);

    c = getchar();
}

I tried to save stdin using : int stdin_copy = dup(0) then restoring it but my getchar is not working.
I also tried to use freopen("/dev/stdin", "a", stdin) but it still doesn't wait for an input

Upvotes: 1

Views: 1108

Answers (1)

Itega
Itega

Reputation: 43

Using fdopen seems to work well so here is the fixed code :

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

void readPBM(char *output)
{
    FILE* fp;
    char tmp[1024];

    int fd[2] = {0,0};
    int pid;

    //Open the pipe for inter-process communication
    pipe(&fd[0]);

    //Fork and test if we are child or parent process
    pid = fork();
    if(pid) //Parent process
    {
        wait(NULL); //Wait for child's end
        close(fd[1]);//Close pipe's write stream

        fp = fdopen(fd[0], "r");

        strcpy(output, "");// Init output at 0
        while(fgets(tmp, 1024, fp) != NULL) //Put remainings entry in output
        {
            strcat(output, tmp);
        }
        strcat(output, "It works\n");
        fclose(fp);
    }
    else if(pid == 0) //Child process
    {
        close(fd[0]);//Close pipe's read stream
        close(1);//Close stdout
        dup(fd[1]);//Duplicate stdin
        close(fd[1]);

        printf("A random string ...\n");
        exit(0);
    }
    else //Print error if fork failed
    {
        printf("Error creating a new process");
        exit(EXIT_FAILURE);
    }
}

int main(int argc, char *argv[])
{
    int i, j;

    char *str = NULL;
    char c;

    str = malloc(512 * sizeof(char*));
    readPBM(str);

    printf("%s", str);

    c = getchar();
}

Upvotes: 2

Related Questions