[Aspell] [C] [Pipes]父级和子级-管道重定向-Unix C

时间:2018-11-30 01:53:40

标签: c unix pipe

代码:

int pf[2], fp[2];

pipe(pf);
pipe(fp);
pid_t parent = getpid();
pid_t child = fork();
char str[20];
char word[20] = "aspell -a test";
char buffer[100];
int count;
//dup2(0, pf[0]);

if (child != 0) {
    char resu[100];

    printf("I am the parent process. My PID is %d\n", parent);

    sleep(5);
    printf("Escrevi a palavra...\n");
    write(pf[1], "teste\n", strlen("teste\n"));
    close(pf[1]);
    sleep(2);
    count = read(fp[0], resu, strlen(resu));
    close(fp[0]);


    printf("resultado: %s\n", resu);

else {
    //printf("I am the child process. My PID is %d\n", getpid());
    //printf("    My parent's PID is %d\n", parent);
    execlp("aspell", "aspell", "-a", NULL);
    dup2(pf[0], 0);
    dup2(1, fp[1]);
    close(1);
    //close(pf[0]);

    execlp("aspell", "aspell", "-a", NULL);

    sleep(2);

    printf("Child process terminating\n");   
}

对于上述代码,它始终会打印

  

结果:

为什么会这样? fp [0]无法正确读取?

“ resultado:”应该是子进程的标准输出,只是看起来是垃圾,还是什么也没有。

0 个答案:

没有答案