管道:

 #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. charles修改接口返回值

    我们在测试app时,如果想看大数据量的展示情况,可以通过charles修改接口返回值来实现. 步骤1:手机连接代理 步骤2:app端请求接口,查看charles抓包情况 步骤3:选择想要修改返回值的接 ...

  2. @Autowired注解和静态方法

    @Autowired注解入static属性时,出现NullPointerException异常. 使用构造方法可解决: @Component public class Test { private s ...

  3. CentOS7 安装VNC

    系统环境:CentOS Linux release 7.6.1810Kernel:3.10.0-957.el7.x86_64系统现状:最小化安装,没有安装任何图形支持软件 安装图形化支持 不建议安装G ...

  4. BZOJ1058或洛谷1110 [ZJOI2007]报表统计

    BZOJ原题链接 洛谷原题链接 STL 本题可以直接使用\(\mathtt{STL\ multiset}\)水过去. 因为本题插入数的操作实际上就是将原数列分为\(n\)段,在每一段的末尾插入数,所以 ...

  5. docker常用操作备忘

    一.docker安装 参考资料:阿里云镜像加速1. 安装/升级Docker客户端 curl -fsSL https://get.docker.com | bash -s docker --mirror ...

  6. mvc @helper 创建用户自定义html

    转载地址:https://www.cnblogs.com/caofangsheng/p/5670071.html

  7. 一、PyQt5基础概念与安装配置

    一.初识PyQt5 对于桌面程序开发,用户图形界面(GUI)的设计非常重要.一款美观.易用的用户界面可以很大程度上提高对使用这的友好度.由于Python最初是作为脚本语言开发,并没有GUI功能.但Py ...

  8. 对于新版本的webstorm对vue的支持

    webstorm 对于官方vue的支持,直到2017.1,这个版本,之后的版本不能直接安装vue插件,这时候就需要自己手动新建vue模板了

  9. 每日一练ACM 2019.04.13

    2019.04.13 第1002题:A+B Proble Ⅱ Problem DescriptionI have a very simple problem for you. Given two in ...

  10. linux 安装mysql5.7.25

    这两天一直在弄mysql.一直安装.终于可以安装一个成一个了.哈哈哈 自己又写了个脚本希望对大家有所帮助 脚本非常简单 不错操作起来也很容易 重要提示 我的linux 是centos7.不是6. 7和 ...