管道:

 #include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<wait.h> /*管道没有名字,只能用于有亲缘关系的进程间通信*/ typedef struct stu
{
int id;
char name[];
}Stu; int main()
{
int pfd[];
int ret=pipe(pfd);//创建管道(管道只能在父进程中创建) if(ret<)
perror("pipe");//判断是否创建成功 int pid=fork();//创建子进程
if (pid<)
perror("fork"); else if(pid>)//父进程写文件
{
close(pfd[]);//关闭读文件描述符
Stu s[];
int i;
for(i=;i<;i++)
{
printf("id=");
scanf("%d",&s[i].id);getchar();
printf("name=");
scanf("%s",s[i].name);getchar();
}
write(pfd[],s,*sizeof(Stu));//写文件,放入s数组
printf("id\tname\n"); close(pfd[]);//关闭写文件
waitpid(pid,NULL,);//等待子进程结束
return ;
} else if(pid==)//子进程读文件
{
close(pfd[]);//关闭写文件标识符
int i;
Stu s2[];
while()
{
ret=read(pfd[],s2,*sizeof(Stu));//读取文件放入s2数组
if(ret<)//判断读取是否成功
{
perror("read");
}
if(ret==)//判断是否读取完毕
{
printf("read over\n");
exit();
} else//输出读取文件
{
for(i=;i<;i++)
printf("%d\t%s\n",s2[i].id,s2[i].name);
i++;
}
}
close(pfd[]);//关闭读文件
return ;
}
}

运行结果:

id=1001    //---------------------------输入(写文件)
name=aaa
id=
name=bbb
id=
name=ccc
id=
name=ddd
id=
name=eee//---------------------------输出(读文件)
id name
aaa
bbb
ccc
ddd
eee
read over

FIFO(有名管道):

代码1:写文件程序

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>
typedef struct stu
{
int id;
char name[];
}Stu; void fun(int sig)
{
printf("中断..\n");
return;
} int main()
{
int fd;
int ret;
char buf[];
signal(SIGPIPE,fun);//给一个已经关闭的终端发送信息会产生13信号 //创建管道
ret = mkfifo("file_f",);
if(ret<)
{
if(errno != EEXIST)//判断创建是否出错(除去已经创建错误)
{
perror("mkfifo");
return -;
}
}
printf("mkfifo success.\n"); //打开管道
printf("open ..\n");
fd = open("file_f",O_WRONLY);//只写方式打开
printf("open fd = %d\n",fd);
if(fd<)
{
printf("open error.\n");
} //写操作 Stu s[];int i;
//while(1)
printf("input the len\n");//输入创建多少个数据
int len;
scanf("%d",&len);
for(i=;i<len;i++)
{
printf("id="); scanf("%d",&s[i].id);getchar();
printf("name=");
scanf("%s",s[i].name);getchar();
} ret = write(fd,s,len*sizeof(struct stu) );//写入到数组中
if(ret<)
{
printf("write error.\n");
goto END;
} printf("write success.\n"); END:
close(fd);
return ;
}

代码2:读文件程序:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h> typedef struct stu
{
int id;
char name[];
}Stu; int main()
{
int fd;
int ret;
//创建
ret = mkfifo("file_f",);
if(ret<)
{
if(errno!=)
{
perror("mkfifo");
return -;
}
}
printf("mkfifo success.\n"); //打开管道
printf("open ..\n");
fd = open("file_f",O_RDONLY);
printf("open fd = %d\n",fd);
if(fd<)
{
printf("open error.\n");
}
printf("id\tname\n"); printf("in put the len\n");
int len;
scanf("%d",&len);//输入要读取几个数据 Stu s[];int i;
ret = read(fd,s,sizeof(Stu)*len);
for(i=;i<len;i++)
{
if(ret<)
{
printf("read error.\n");
goto END;
}
if(ret == )
{
printf("pipe broken.\n");
goto END;
}
if(s[i].id==)// 一旦数据读取完毕就退出
{
goto END;
}
printf("%d\t%s\n",s[i].id,s[i].name);//打印读取到的数据
}
END:
printf("read success\n");
close(fd);
return ;
}

运行结果:右边输入,左边输出

另一种FIFO(边读边写)

代码1:

 #include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h> void fun(int sig)
{
printf("中断..\n");
return;
} int main()
{
int fd;
int ret;
char buf[]; signal(SIGPIPE,fun);
//创建
ret = mkfifo("file_f",);
if(ret<)
{
if(errno != EEXIST)
{
perror("mkfifo");
return -;
}
}
printf("mkfifo success.\n"); //打开管道
printf("open ..\n");
fd = open("file_f",O_WRONLY);
printf("open fd = %d\n",fd);
if(fd<)
{
printf("open error.\n");
} //写操作
while()
{ fgets(buf,,stdin);//输入 ret = write(fd,buf,strlen(buf)+ );
if(ret<)
{
printf("write error.\n");
goto END;
}
}
printf("write success.\n"); END:
close(fd);
return ;
}

代码2:

 #include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h> int main()
{
int fd;
int ret;
char buf[]; //创建
ret = mkfifo("file_f",);
if(ret<)
{
if(errno != EEXIST)
{
perror("mkfifo");
return -;
}
}
printf("mkfifo success.\n"); //打开管道
printf("open ..\n");
fd = open("file_f",O_RDONLY);
printf("open fd = %d\n",fd);
if(fd<)
{
printf("open error.\n");
} while()
{
ret = read(fd,buf,);//读取输出
if(ret<)
{
printf("read error.\n");
goto END;
}
if(ret == )
{
printf("pipe broken.\n");
goto END;
}
printf("read buf:%s\n",buf);
}
END:
close(fd);
return ;
}

输出结果:

管道/FIFO的更多相关文章

  1. Linux学习笔记25——命名管道(FIFO)

    1 命名管道(FIFO) 管道应用的一个重大缺陷就是没有名字,因此只能用于亲缘进程之间的通信.后来从管道为基础提出命名管道(named pipe,FIFO)的概念,该限制得到了克服.FIFO不同于管道 ...

  2. 进程间通信系列 之 命名管道FIFO及其应用实例

    进程间通信系列 之 概述与对比   http://blog.csdn.net/younger_china/article/details/15808685  进程间通信系列 之 共享内存及其实例   ...

  3. Linux进程间通信之管道(pipe)、命名管道(FIFO)与信号(Signal)

    整理自网络 Unix IPC包括:管道(pipe).命名管道(FIFO)与信号(Signal) 管道(pipe) 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道 ...

  4. 进程间通信___命名管道(FIFO)

    命名管道(FIFO) 基本概念 命名管道和一般的管道基本相同,但也有一些显著的不同: 命名管道是在文件系统中作为一个特殊的设备文件而存在的. 不同祖先的进程之间可以通过管道共享数据. 当共享管道的进程 ...

  5. 命名管道FIFO

    首先我得检讨一下自己,这几天有些颓呀,打不起精神,板子出了点问题,果真自学还是很困难呀,硬件方面难解决呀,理想与现实还是很有差距的,伤透了,凌乱了. 一直在理解进程间通信的问题.发现上次忽略了一个问题 ...

  6. linux有名管道fifo,进程间通信

    命名管道(FIFO)不同于无名管道之处在于它提供了一个路径名与之关联,以 FIFO 的文件形式存在于文件系统中,这样,即使与 FIFO 的创建进程不存在亲缘关系的进程,只要可以访问该路径,就能够彼此通 ...

  7. 进程间通信IPC-命名管道FIFO

    FIFO又被称为命名管道,未命名的管道只能在两个相关的进程之间使用,而这两个相关的进程还要有一个共同创建了它们的祖先进程,但是FIFO,不相关的进程之间也能交换数据. FIFO是一种文件类型.通过st ...

  8. 有名管道FIFO

    管道和FIFO的特征之一是它们的数据是一个字节流.这是UNIX的原生I/O模型.进程往其中写入的是字节流,系统不对它作解释. FIFO不存数据,只是通过它找到内核文件. 一.建立有名管道 1.命令mk ...

  9. 命名管道FIFO和mkfifo函数

    进程间通信必须通过内核提供的通道,而且必须有一种办法在进程中标识内核提供的某个通道,前面讲过的匿名管道是用打开的文件描述符来标识的.如果要互相通信的几个进程没有从公共祖先那里继承文件描述符,它们怎么通 ...

随机推荐

  1. Linux-01

    Linux各目录的作用 /bin/ 存放系统命令的目录,普通用户和超级用户都可以执行.不过放在/bin下的命令在单用户模式下也可以执行 /sbin/ 保存和系统环境设置相关的命令,只有超级用户可以使用 ...

  2. model 字段参数 choice

    class Banner(NewsBase): ''' 轮播图 ''' PRI_CHOICES = [ # 优先级的限制选择范围 (1,'第一级'), (2,'第二级'), (3,'第三级'), (4 ...

  3. Swift Realm 完整使用记录

    新项目用到了数据库,本来之前用的都是 SQL,但是语法写的实在是恶心,所以使用 Realm 尝试一下. 1.我使用的 pod 库,所以先 pod 库安装一下,安装完别忘了先编译一下,不然 import ...

  4. C#导入c++ dll报找不到dll文件 masm32调用c++类库

    最近需要在C#下调用一个c++ dll库,不管怎样dllimport就是报错找不到该dll文件,路径.函数名称.参数.dllimport参数逐个检查确认无误也无济于事,无奈想用其他语言调用试试,由于是 ...

  5. js如何获取点击<li>标签里的内容值

    路:为li对象添加单击事件→事件触发后利用innerHTML获取li的文本.实例演示如下: 1.HTML结构 <ul id="test"> <li>Glen ...

  6. SVN chechout 错误: xxx is not valid as filename in directory

    转载:https://blog.csdn.net/ClementAD/article/details/47838989 意思就是Linux系统允许文件或文件夹的名字包含空格,而windows是不允许的 ...

  7. css3(display)

    实现下拉 <!DOCTYPE html><html><head> <meta charset="utf-8"> <style ...

  8. 20170529计划---统计业务量并生成EXCEL通过邮件发送

    每个月都要统计这些业务量的东东,烦死了,赶紧通过python写一个来搞定吧,三天搞定吧,未完待续哈. 2017-5-29 19:50粗略地做了一个思维导图哈 终于第三天完成啦 #encoding=ut ...

  9. Springboot访问静态资源

    转载 http://blog.csdn.net/catoop/article/details/50501706

  10. UI交互设计关键词:情感化设计与心理

    情感化设计,一定有一个关键词.情感,是指人对周围事物和自身以及对自己行为的态度,它是人对客观事物的一种特殊的反映形式,是主体对外界刺激给予肯定或否定的心理反应,也是对客观事物是否符合自己需要的态度或体 ...