c - 在管道之后恢复标准输入

标签 c pipe stdin

我使用 stdin 和 stdout 制作了一个管道来进行通信,但我不知道如何在我的父亲进程中关闭它后恢复 stdin。

这是一个例子:

#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();
}

我尝试使用以下命令保存标准输入:int stdin_copy = dup(0)然后恢复它,但我的 getchar 不起作用。
我还尝试使用freopen("/dev/stdin", "a", stdin)但它仍然不等待输入

最佳答案

使用 fdopen 似乎效果很好,所以这里是固定代码:

#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();
}

关于c - 在管道之后恢复标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41141823/

相关文章:

c - C语言读取文件时出现段错误

Python os.pipe 与 multiprocessing.Pipe

c - while 循环内的 If 语句无法执行并退出程序

c - 如何在c中获取文件长度?

linux - bash |管道到 bash 函数

bash - 如何将 tar 压缩操作通过管道传输到 aws s3 cp?

python - 在子进程中使用标准输入

python - 使用 subprocess.Popen() 从文件中读取 ssh 输入

linux - 当从我的 c 程序中调用时,如何将数据传递给使用 stdin/stdout 的 zlib 的 zpipe 可执行文件?

c - 需要澄清有关 vfork 使用的信息