pipe.c

来自「中科院徐志伟老师一书《操作系统 原理·技术与编程》的源代码和习题接」· C语言 代码 · 共 33 行

C
33
字号
#include <unistd.h>
#include <stdio.h>
char *id = "pipe.c is a test unnamed_pipe example\n";
		
void main(){
int fd[2];
char buf[256]="Hello,everybody:do you understand";

int  n,  count = 0;

write(2,id,strlen(id));
if(pipe(fd)==-1){
     perror("pipe");
     exit(1);
}
switch(fork()){
    case 0:      /* child process */
	close(fd[0]);
	while((count<4096)&&((n = write(fd[1],buf,strlen(buf)))>0))
        count +=n;
	exit(0);
    case -1:
        perror("fork");
	exit(1);
default:       /* parent process */
        close(fd[1]);
        while((count<4096)&&((n = read(fd[0],buf,strlen(buf)))>0))
          count +=n;
        printf("%d bytes of data received from child process:%s\n",count,buf);
	exit(0);
}                                    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?