1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 int main(void) { 5 int data_processed; //用于记录已经处理的数据 6 int file_pipes[2]; //定义两个文件描述符 7 char buffer[BUFSIZ + 1]; //定义一个缓冲区,以便从管道中读取数据 8 const char * some_data = "123"; //定义一个将要写入管道中的字符串 9 memset(buffer, '\0', sizeof(buffer)); //初始化读取缓冲区 10 if (pipe(file_pipes) == 0) { //创建管道 11 data_processed = write(file_pipes[1], some_data, sizeof(some_data)); 12 printf("写入%d个字符\n", data_processed); 13 data_processed = read(file_pipes[0], buffer, BUFSIZ); 14 printf("读出%d个字符。内容是:%s\n",data_processed,buffer); 15 exit(EXIT_SUCCESS); 16 } 17 return EXIT_FAILURE; 18 }