管道:

 #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. excel 上传读写到数据库

    <HTML> <div class="input-group"> <form id="abc" action="http ...

  2. Android面试准备20190422

    1.即时推送原理,采用的push推送模式,保持一个长连接,服务端和客户端连接后不再断开.所谓长连接,即是在一个TCP上可以连续发送多个数据包,在TCP连接保持期间,如果没有数据包发送,需要双方发送检测 ...

  3. node.js的Promise库-bluebird示例

    前两天公司一哥们写了一段node.js代码发给我,后面特意提了一句“写的不太优雅”.我知道,他意思是回调嵌套回调,因为当时比较急也就没有再纠结.然而内心中总记得要解决这个问题.解决node.js的回调 ...

  4. 进军的socket

    在学socket有时候我们会遇到这种问题: 解决方法一: 在服务端中加入:severTCP.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) ...

  5. CSS样式内容

    CSS代码规范:尽量不要在标签内使用样式代码. .css文档内部声明不换行可以节省内存. 谨记: 常犯的错误是.html文档和.css样式表都写好了,但总会忘记插入样式表. 1.字体的样式 ​  2. ...

  6. day 2:计算机的基础知识,编程语言分类

    本节内容 1,计算机的容量 2,编程语言介绍1,计算机的容量 1位 = 1bit 8bit = 1byte = 1字节 1024bytes = 1k bytes = 1KB 1024个 1024KB ...

  7. 不在sudoer里解决办法 和 RHEL 挂载NTFS硬盘

    输入su 切换到root用户 打开/etc/sudoers sudo vim sudoers 在root    ALL=(ALL:ALL) ALL 下边比着写一个自己的用户名就可以了 下载 可以到ht ...

  8. [Hadoop]Hadoop章2 HDFS原理及读写过程

    HDFS(Hadoop Distributed File System )Hadoop分布式文件系统. HDFS有很多特点: ① 保存多个副本,且提供容错机制,副本丢失或宕机自动恢复.默认存3份. ② ...

  9. super()调用父类构造方法

    super()表示调用父类中的构造方法 1.子类继承父类,子类的构造方法的第一行,系统会默认编写super(),在调用子类的构造方法时,先调用父类的无参数构造方法 2.如果父类中只有有参数构造方法,那 ...

  10. T-SQL流程控制语句

    文章目录 if else语句 简单case语句 搜索式case语句 while语句 if else语句 格式: IF 布尔表达式 BEGIN END ELSE BEGIN END 示例: DECLAR ...