簡體   English   中英

C管:父母在孩子結束前從孩子讀書

[英]C Pipe: Parent to read from child before child ends

下面的代碼顯示了子進程如何寫入 管道端,然后顯示父進程如何從另一端讀取 我在實驗代碼后注意到的是,只有在子進程終止后 ,父進程才能讀取數據。

有沒有辦法強制父進程到達前台並在子進程調用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 = fork()) == -1) { /* Fork a child */
        perror("fork"); 
        exit(1); 
    }
    if (pid == 0) { /* Child, writer */
        close(fd[READ]); /* Close unused end */
        write(fd[WRITE], phrase, strlen(phrase)+1);
        close(fd[WRITE]); /* Close used end */
    } 
    else { /* Parent, reader */
        close(fd[WRITE]); /* Close unused end */
        bytes = read(fd[READ], message, sizeof(message));
        printf("Read %d bytes: %s\n", bytes, message);
        close(fd[READ]);  /* Close used end */
    }
}

你不對。 嘗試在關閉'child'部分中管道寫入結束之前添加sleep(120)調用並運行應用程序。

第一種可以使用一些同步技術來完成。 表示孩子應該等到父母讀取數據,或者在父母的代碼中終止。 我在代碼中添加了第二個管道,用於此同步。

寫完后的孩子只是等待從那個管道中讀取並在讀取時被阻止。

只有在父節點終止時,函數read才會返回(此處為0),因為現在read in child將獲得文件結束通知,因為在父節點退出后,父節點中的管道寫入結束關閉。 (在父終止后,寫端將自動關閉。我沒有明確地添加它)

你的第二個要求

I also want the parent to read the data immediately after the child whites to
the pipe. 

為什么你認為這不會發生? 應該總是有一些微小的時間滯后。

#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];
  int fd1[2];
  char buffer[1];
  int ret;

  if (pipe(fd) == -1) { /* Create a pipe */
    perror("pipe");
    exit(1);
  }
  if (pipe(fd1) == -1) { /* Create a pipe */
    perror("pipe");
    exit(1);
  }
  if ((pid = fork()) == -1) { /* Fork a child */
    perror("fork");
    exit(1);
  }
  if (pid == 0) { /* Child, writer */
    close(fd[READ]); /* Close unused end */
    close(fd1[WRITE]);/*Close write  end of the 2nd pipe*/
    write(fd[WRITE], phrase, strlen(phrase)+1);
    close(fd[WRITE]); /* Close used end */
    /*For synchronisation let the child try to
    read from the 2nd pipe. 
    The function read, will return 0 only when the
    parent terminates and in this we are
    not interested if at all anything read or not.
    The read statement below, will return only if the
    parent has terminated, thus ensures that the
    child terminates only after the parent*/
    ret = read(fd1[READ],buffer, 1);
  }

else { /* Parent, reader */
    close(fd[WRITE]); /* Close unused end */
    close(fd1[READ]); /*Close read end of the 2nd pipe*/
    bytes = read(fd[READ], message, sizeof(message));
    printf("Read %d bytes: %s\n", bytes, message);
    close(fd[READ]);  /* Close used end */

   }
}

你應該初始化fd [0] = 0和fd [1] = 0

下面的代碼演示,在子寫入管道后,父母可以讀取管道。 不需要子進程關閉管道或終止。

#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 = fork()) == -1) { /* Fork a child */
        perror("fork"); 
        exit(1); 
    }
    if (pid == 0) { /* Child, writer */
        close(fd[READ]); /* Close unused end */
        write(fd[WRITE], phrase, strlen(phrase)+1);
        sleep(10);
        close(fd[WRITE]); /* Close used end */
        sleep(20);
    } 
    else { /* Parent, reader */
        printf( "child ipd = %d\n\r", pid);
        close(fd[WRITE]); /* Close unused end */
        bytes = read(fd[READ], message, sizeof(message));
        printf("Read %d bytes: %s\n", bytes, message);
        close(fd[READ]);  /* Close used end */
        sleep(10);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM