Linux匿名管道与命名管道
http://blog.chinaunix.net/uid-26000296-id-3408970.html
/*
* \File
* main.c
* \Descript
* father-process reads input file and sends to child-process by anonymous-pipe
* client-process transform and write into output file
*/ #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h> FILE* fp_in = NULL;
FILE* fp_out = NULL;
#define TESTF_IN "test.dat"
#define TESTF_OUT "out.dat"
#define MAX_LINE 512
#define OP_LEN 100 /*
* \File
* trans.h
* \Descript
*
*/ #ifndef __TRANS_H__
#define __TRANS_H__ int trans_lower2upper(char* buf_in, char* buf_out, int len); #endif /*
* \Func
* main
* \Descript
*
*/ int main(char argc, char* argv[])
{
int pidstatus;
int fdfile_in, fdfile_out; int fdpipe_a[];
int fdpipe_b[];
int pid_trans = -, pid_write = -; /* Create anonymous-pipe */
if( (pipe(fdpipe_a) < ) || (pipe(fdpipe_b) < )) //创建两个管道
{
printf("open pipe failed.\n");
exit();
} if ( (fp_in = fopen(TESTF_IN, "r")) < ) //打开test.dat文件,用于读
{
printf("open input file failed: %s\n", TESTF_IN);
exit();
} if ( (fp_out = fopen(TESTF_OUT, "w")) < ) //新建out.dat文件,用于写
{
printf("open input file failed: %s\n", TESTF_OUT);
exit();
} if ( (pid_trans = fork()) && (pid_write = fork()) ) //创建两个子进程,父进程工作内容
{
/*
* PARENT_PROCESS:
* read data from in-file, write it into anonymos-pipe.
*/ int s_read = , s_write;
char bufparent[]; while((s_read = fread(bufparent, sizeof(char), OP_LEN ,fp_in) ) ) //读test.dat文件
{
printf("***** %d, %s\n", s_read, bufparent);
if ( (s_write = write(fdpipe_a[], bufparent, s_read)) < )//向管道写
{
printf("write pipe failed.\n");
exit();
}
memset(bufparent, , );
if( feof(fp_in) != ) //检查文件是否结束
{
printf("\n***** read test.dat over ******\n");
exit();
}
}
}
else if ( pid_trans == )
{
/*
* TRANS_PROCESS:
* read anonymous-pipe, transcode, write anonymos-pipe.
*/ char buftrans_in[], buftrans_out[];
int size_read, size_write;
int ret; while(size_read = read(fdpipe_a[], buftrans_in, OP_LEN)) //读管道
{
ret = trans_lower2upper(buftrans_in, buftrans_out, size_read); if ( (size_write = write(fdpipe_b[], buftrans_out, size_read)) < ) //写管道
{
printf("trans-process write failed.\n");
exit();
}
}
}
else if ( pid_write == )
{
/*
* WRITE_PROCESS:
* read anonymous-pipe, write it into out-file
*/
int s_read, s_write;
char bufwrite[]; while ( s_read = read(fdpipe_b[], bufwrite, OP_LEN) ) //读管道
{
if( (s_write = fwrite(bufwrite, sizeof(char), s_read, fp_out)) < ) //写out.dat文件
{
printf("..... write error.\n");
exit();
}
if(s_read < OP_LEN)
{
break;
}
}
}
else
{
printf("fork process failed.\n");
exit();
} waitpid(pid_trans, &pidstatus, );
waitpid(pid_write, &pidstatus, ); fclose(fp_out);
fclose(fp_in); int s_read2 = ;
char bufparent2[];
if ( (fp_out = fopen(TESTF_OUT, "r")) < ) //新建out.dat文件,用于写
{
printf("open input file failed: %s\n", TESTF_OUT);
exit();
} s_read2 = fread(bufparent2, sizeof(char), OP_LEN ,fp_out);
printf("***** %d, %s\n", s_read2, bufparent2);
if( feof(fp_out) != ) //检查文件是否结束
{
printf("\n***** pirint out.dat over ******\n");
exit();
} fclose(fp_out); return ; } /*
* \Func
* trans_lower2upper
* \Descript
* Lowercase turn uppercase
*/
int trans_lower2upper(char* buf_in, char* buf_out, int buf_len) //逐个字符转换成大写字母
{
int len = buf_len;
char* cp_in = buf_in;
char* cp_out = buf_out;
char atom;
char offset; while(len--)
{
atom = *(cp_in++); if( (atom >= 'a') && (atom <= 'z') )
{
offset = atom - 'a';
atom = 'A' + offset;
}
*(cp_out++) = atom;
} return ;
}
运行结果如下

出现的问题是 打印out.dat文件内容时,打印了wujing@Ubuntu:$
http://www.cnblogs.com/biyeymyhjob/archive/2012/11/03/2751593.html
http://blog.csdn.net/ljianhui/article/details/10202699
Linux匿名管道与命名管道的更多相关文章
- 操作系统-进程通信(信号量、匿名管道、命名管道、Socket)
进程通信(信号量.匿名管道.命名管道.Socket) 具体的概念就没必要说了,参考以下链接. 信号量 匿名管道 命名管道 Socket Source Code: 1. 信号量(生产者消费者问题) #i ...
- shell 匿名管道和命名管道
管道的特点:如果管道中没有数据,那么取管道数据的操作就会滞留,直到管道内进入数据,然后读出后才会终止这一操作:同理,写入管道的操作如果没有读取管道的操作,这一动作也会滞留. 1,匿名管道 匿名管道使用 ...
- Linux环境进程间通信(一):管道及命名管道
linux下进程间通信的几种主要手段: 管道(Pipe)及有名管道(named pipe):管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- Linux系统编程之命名管道与共享内存
在上一篇博客中,我们已经熟悉并使用了匿名管道,这篇博客我们将讲述进程间通信另外两种常见方式--命名管道与共享内存. 1.命名管道 管道是使用文件的方式,进行进程之间的通信.因此对于管道的操作,实际上还 ...
- 【windows 操作系统】进程间通信(IPC)简述|无名管道和命名管道 消息队列、信号量、共享存储、Socket、Streams等
一.进程间通信简述 每个进程各自有不同的用户地址空间,任何一个进程的全局变量在另一个进程中都看不到,所以进程之间要交换数据必须通过内核,在内核中开辟一块缓冲区,进程1把数据从用户空间拷到内核缓冲区,进 ...
- Linux学习笔记25——命名管道(FIFO)
1 命名管道(FIFO) 管道应用的一个重大缺陷就是没有名字,因此只能用于亲缘进程之间的通信.后来从管道为基础提出命名管道(named pipe,FIFO)的概念,该限制得到了克服.FIFO不同于管道 ...
- Linux 命名管道
前文中笔者介绍了管道,本文接着介绍命名管道.文中演示所用环境为 Ubuntu 18.04 desktop. 命名管道(named pipe)又被称为先进先出队列(FIFO),是一种特殊的管道,存在于文 ...
- linux命名管道通信过程
前一个道,这节学习命名管道. 二命名管道 无名管道仅仅能用来在父子进程或兄弟进程之间进行通信,这就给没有亲缘关系的进程之间数据的交换带来了麻烦.解决问题就是本节要学习的还有一种管道通信:命名管道. 命 ...
- SQL Server 连接问题圣经-命名管道
SQL Server 连接问题圣经-命名管道 (1) APGC DSD Team 12 Jan 2011 1:24 AM 3 一.前言 在使用SQL Server 的过程中,用户遇到的最多的莫过于连接 ...
随机推荐
- Netbeans 中的编译器相关配置
gcc-core:C 编译器 gcc-g++:C++ 编译器 gdb:GNU 调试器 make:"make" 实用程序的 GNU 版本
- 【poj1804】 Brainman
http://poj.org/problem?id=1804 (题目链接) 题意 求逆序对 Solution1 归并排序. 每次合并时计算逆序对. 代码1 // poj1804 #include&l ...
- 哈希加密算法 MD5,SHA-1,SHA-2,SHA-256,SHA-512,SHA-3,RIPEMD-160 - aTool
一.MD5哈希加密算法 atool.org MD5即Message-Digest Algorithm 5(信息-摘要算法 5),用于确保信息传输完整一致.是计算机广泛使用的散列算法之一(又译摘要算法. ...
- Jsonp简单认识(后端使用的是asp.net mvc)
一.Jsonp简介:由于浏览器基于安全有同源策略(同源策略阻止从一个源加载的文档或脚本获取或设置另一个源加载的文档的属性)机制,所以前端无法使用Ajax来获取来获取其他域名下返回的数据,而Jsonp可 ...
- Yii2 如何使用事件
原文地址:http://www.fancyecommerce.com/2016/04/29/yii2-%E4%BD%BF%E7%94%A8event-1-%EF%BC%8C%E5%A6%82%E4%B ...
- Linux里如何查找文件内容 (转)
Linux查找文件内容的常用命令方法. 从文件内容查找匹配指定字符串的行: $ grep "被查找的字符串" 文件名例子:在当前目录里第一级文件夹中寻找包含指定字符串的.in文件g ...
- SQL Server 2005 发布 订阅 (配置实例[图])(转载)
2.1 发布&订阅 1. 测 试环境: Item 发布机 A 订阅机 B OS Windows 2003 Server Windows 2003 Server S ...
- ASP.NET MVC 站点设置.html 为起始页
1. 删除 controller="XX" 2. 确保你的工程根目录下的*.htm或*.html文件名在IIS默认文档中存在 搞定
- 关于设置SQLPLUS提示符样式的方法----登陆配置文件,动态加载提示符
工作中用到 sqlplus mdsoss/mdsoss, 所以来了解一下sqlplus (C shell .cshrc文件里中alisa) 关于设置SQLPLUS提示符样式的方法 12638阅读 1评 ...
- mysql 查询技巧
查出来的结果每一行显示一条,中间以*号分隔. select * from tableName limit 10 \G mysql 随机取数据 SELECT * FROM table_name ORDE ...