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 的过程中,用户遇到的最多的莫过于连接 ...
随机推荐
- JS:采摘自JS精粹
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- Mysql数据库登录问题:Your password has expired.
ERROR 1862 (HY000): Your password has expired. To log in you mustchange it using a client that suppo ...
- BZOJ-2002 弹飞绵羊 Link-Cut-Tree (分块)
2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec Memory Limit: 259 MB Submit: 6801 Solved: 3573 [Submi ...
- 【poj3233】 Matrix Power Series
http://poj.org/problem?id=3233 (题目链接) 题意 给出一个n×n的矩阵A,求模m下A+A2+A3+…+Ak 的值 Solution 今日考试就A了这一道题.. 当k为偶 ...
- Scrapy中的item是什么
这两天看Scrapy,看到item这个东西,觉得有点抽象,查了一下,有点明白了. Item 是保存爬取到的数据的容器:其使用方法和python字典类似, 并且提供了额外保护机制来避免拼写错误导致的未定 ...
- MyEclipse------如何查询MySQL数据库里面表的信息
testExecuteQuary.jsp <%@ page language="java" import="java.util.*" pageEncodi ...
- win7打开网页老是提示下载网页解决办法
方法:我的系统是windows 7 旗舰版, 解决方法可以自己手动去修复,方法是进入命令窗口. 开始--运行--cmd--sfc.exe--sfc/scannow 修复一下!
- spring mvc静态资源文件的引用
在页面的<title>下 <link rel="stylesheet" href="<%=request.getContextPath()%> ...
- System.gc()与Object.finalize()的区别
finalize()是由JVM自动调用的,你可以用System.gc(),但JVM不一定会立刻执行,JVM感觉内存空间有限时,才会开始执行finalize(),至于新的对象创建个数和被收集个数不同是因 ...
- 在Microsoft-IIS/10.0上面部署mvc站点的时候,出现404的错误
写在前面 在家自己弄了一个项目,想部署在电脑上用手机来访问,总是出现404的错误.路由什么的没有写错啊,最后发现是映射程序的问题,在安装的时候iis很多功能没有安装,又将iis的其他没有安装的功能勾选 ...