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 的过程中,用户遇到的最多的莫过于连接 ...
随机推荐
- /WEB-INF/userManage.jsp(31,82) Unterminated ${ tag
这个错误是说明${}少写了一半. Unterminated有这个的意思是你的jsp语法有错误
- 在windows 环境下对于 git 服务器的安装和使用
前言: 虽然说在团队开发的时候会有版本控制服务器,但是个人自己开发的时候,有的时候也需要有个版本控制下,比如,你改好了一个小的功能,然后在这个功能上继续扩展,结果扩展不成功,于是回到这个小功能上去.当 ...
- HashMap和Hashtable及HashSet的区别
相关文章1:HashSet,TreeSet和LinkedHashSet的区别 相关文章2:HashSet和TreeSet的区别 Hashtable类 Hashtable继承Map接口,实现一个 ...
- MyBatis架构设计及源代码分析系列(一):MyBatis架构
如果不太熟悉MyBatis使用的请先参见MyBatis官方文档,这对理解其架构设计和源码分析有很大好处. 一.概述 MyBatis并不是一个完整的ORM框架,其官方首页是这么介绍自己 The MyBa ...
- 【bzoj1042】 HAOI2008—硬币购物
http://www.lydsy.com/JudgeOnline/problem.php?id=1042 (题目链接) 题意 共有4种硬币,面值分别为c1,c2,c3,c4.某人去商店买东西,去了to ...
- 获取Trustedinstalled权限.reg
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT*\shell\runas] @="获取TrustedInstaller权限&q ...
- appium跑demo简单实例讲解
安装appium,设置 demo.pyfrom appium import webdriver #要装webdriver,方法查看http://www.cnblogs.com/sincoolvip/p ...
- ecshop后台admin路径怎么修改
ecshop后台admin路径怎么修改 ECSHOP教程/ ecshop教程网(www.ecshop119.com) 2013-03-25 ecshop如何修改后台admin路径? 大家都知道ec ...
- 把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方。Duplicate entry
把数据保存到数据库附加表 `dede_addonarticle` 时出错,请把相关信息提交给DedeCms官方.Duplicate entry ’3′ for key ‘PRIMARY’ 你的主键是不 ...
- 一种透明效果的view
设置这个view背景色: [UIColor colorWithRed: green: blue: alpha:0.3]; 效果如下: