小编use*_*279的帖子

C管:父母在孩子结束前从孩子读书

下面的代码显示了一个子进程如何一个端再怎么父进程可以读取从另一端.我在实验代码后注意到的是,只有在子进程终止后,父进程才能读取数据.

是否有办法强制父进程到达前台并在子进程调用write()后立即读取数据?有没有办法在不终止孩子的情况下读取数据?

#include <stdio.h> /* For printf */
#include <string.h> /* For strlen */
#include <stdlib.h> /* For exit */

#define READ 0 /* Read end of pipe */
#define WRITE 1 /* Write end of pipe */
char *phrase = "This is a test phrase.";
main(){
    int pid, fd[2], bytes;
    char message[100];

    if (pipe(fd) == -1) { /* Create a pipe */
        perror("pipe"); 
        exit(1); 
    }
    if ((pid …
Run Code Online (Sandbox Code Playgroud)

c fork pipe

5
推荐指数
1
解决办法
6744
查看次数

标签 统计

c ×1

fork ×1

pipe ×1