无名管道
- 无名管道是半双工的,只能读或者写;
- 无名管道只能在相关的,有共同祖先的进程间使用;
- 一个fork()或者exec()调用创建的子进程继承了父进程文件描述符;
- 打开和关闭管道:
int pipe(int filedes[2]);
成功:打开两个文件描述符,保存在一个整数数组中,第一个文件描述符用于读取数据,第二个文件描述符用于写于数据,出错返回-1;关闭用close()函数。
例:
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(int arg,char *args[]){
int fd[2];
char buf[100];
int len;
pipe(fd);
memset(buf,0,sizeof(buf));
int pid=fork();
if(pid==0){
close(fd[1]);//关闭写文件描述符
while((len=read(fd[0],buf,sizeof(buf)))>0){
write(STDOUT_FILENO,buf,len);
}
close(fd[0]);
}
else{
close(fd[0]);//关闭读文件描述符
strcpy(buf,"hello world\n");
write(fd[1],buf,sizeof(buf));//在管道里写入hello world
close(fd[1]);
waitpid(pid,NULL,0);//防止子进程僵死
}
return 0;
}
结果:
xin@xin-Lenovo-V3000:~/code/guandao/guandao1$ ./guandao1
hello world
(2)有名管道
(a)持久稳定的,存在文件系统中,能让无关联的进程交换数据;
(b)mkfifo[option]name 建立一个名为name的有名管道;
有名管道入门
接下来我们介绍有名管道的一个简单用法,我们在终端输入:
xin@xin-Lenovo-V3000:~$ mkfifo fifo1
xin@xin-Lenovo-V3000:~$ ls -l
总用量 52
drwxrwxr-x 2 xin xin 4096 8月 21 09:43 abc
-rw-rw-r-- 1 xin xin 10240 8月 21 09:49 abc.tar
drwxrwxr-x 16 xin xin 4096 8月 23 10:15 code
prw-rw-r-- 1 xin xin 0 8月 23 17:20 fifo1
drwxr-xr-x 2 xin xin 4096 7月 31 20:55 公共的
drwxr-xr-x 2 xin xin 4096 7月 31 20:55 模板
drwxr-xr-x 2 xin xin 4096 7月 31 20:55 视频
drwxr-xr-x 3 xin xin 4096 8月 7 23:12 图片
drwxr-xr-x 2 xin xin 4096 7月 31 20:55 文档
drwxr-xr-x 2 xin xin 4096 7月 31 21:16 下载
drwxr-xr-x 2 xin xin 4096 7月 31 20:55 音乐
drwxr-xr-x 2 xin xin 4096 8月 23 09:19 桌面
通过 ls -l,我们可以看到建立了一个fifo1的通道。
接下来:
ls>fifo1将ls命令输出到fifio1中。
cat<fifo1通过cat命令从fifo1中读取数据。
xin@xin-Lenovo-V3000:~$ ls
abc abc.tar code fifo1 公共的 模板 视频 图片 文档 下载 音乐 桌面
xin@xin-Lenovo-V3000:~$ ls>fifo1
xin@xin-Lenovo-V3000:~$ cat<fifo1
abc
abc.tar
code
fifio1
fifo1
公共的
模板
视频
图片
文档
下载
音乐
桌面
rm fifo1可以关闭管道
xin@xin-Lenovo-V3000:~$ rm fifo1
xin@xin-Lenovo-V3000:~$ ls
abc abc.tar code 公共的 模板 视频 图片 文档 下载 音乐 桌面
有名管道深入
写fifo
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
int len=0;
char buf[100];
memset(buf,0,sizeof(buf));
int fd=open("fifo1",O_RDONLY);
while((len=read(fd,buf,sizeof(buf)))>0){
printf("%s",buf);
memset(buf,0,sizeof(buf));
}
close(fd);
return 0;
}
读fifo:
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(){
char buf[100];
memset(buf,0,sizeof(buf));
int fd=open("fifo1",O_WRONLY);
while(1){
read(STDIN_FILENO,buf,sizeof(buf));//向标准输入输入数据,读取所有的键,包括回车,,保存在buf中。
if(buf[0]=='0')
break;
write(fd,buf,strlen(buf));//向管道写入buf的值。
memset(buf,0,sizeof(buf));
}
close(fd);
return 0;
}
打开两个终端,一个终端运行写fifo文件:
xin@xin-Lenovo-V3000:~/code/guandao/guandao2$ ./guandao2
hello world!
best or nothing!
另一个终端运行读fifo:
xin@xin-Lenovo-V3000:~/code/guandao/guandao2$ ./guandao2
hello world!
best or nothing!
如果我们改写写fifo文件:
//read(STDIN_FILENO,buf,sizeof(buf));
scanf("%s",buf);
读出来的结果为:
xin@xin-Lenovo-V3000:~/code/guandao/guandao2$ ./guandao2
helloworldbestornothing
而输入无法通过回车结束。
原因:
在键盘中敲入字符后,字符会首先保存在键盘缓冲区中供scanf函数读取,对于scanf以%s格式输入时候,“空格、换行符、TAB”等是作为两个数据的分隔符存在的,即分隔符前后是两个数据,读入时候分隔符不读入。因为在while循环中,所以会一直要求输入。
(3)共享内存
共享内存函数由shmget、shmat、shmdt、shmctl四个函数组成,用于Linux进程通信共享内存。
shmget()函数
int shmget(key_t key,size_t size,int shm_flg);
key :
0(IPC_PRIVATE):会建立新共享内存对象
大于0的32位整数:视参数shmflg来确定操作。通常要求此值来源于ftok返回的IPC键值
size:
大于0的整数:新建的共享内存大小,以字节为单位
shm_flg:
权限
我们通过一段代码建立共享内存:
#include<stdio.h>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(){
int shmid=shmget(IPC_PRIVATE,1024,0666);
if(shmid<0)
printf("error\n");
else
printf("success\n");
return 0;
}
通过ipcs -m访问共享内存区
xin@xin-Lenovo-V3000:~/code/guandao/sharememory$ ipcs -m
------------ 共享内存段 --------------
键 shmid 拥有者 权限 字节 连接数 状态
0x00000000 655360 xin 600 524288 2 目标
0x00000000 753665 xin 600 16777216 2
0x00000000 557058 xin 600 524288 2 目标
0x00000000 33325078 xin 600 16384 2 目标
0x00000000 45580311 xin 600 7626240 2 目标
0x00000000 43089944 xin 600 28672 2 目标
0x00000000 45809689 xin 666 1024 0 //我们建的
0x00000000 45744154 xin 600 663264 2 目标
0x00000000 45711387 xin 600 33480 2 目标
shmat()函数
shmat()是用来允许本进程访问一块共享内存的函数。
shmat的原型是
void *shmat(int shmid, const void *shmaddr, int shmflg);
它需要3个参数,
第一个参数是shmget返回的标识符,
第二个参数 如果 shmaddr 是NULL,系统将自动选择一个合适的地址! 如果shmaddr不是NULL 并且没有指定SHM_RND 则此段连接到addr所指定的地址上 如果shmaddr非0 并且指定了SHM_RND 则此段连接到shmaddr -(shmaddr mod SHMLAB)所表示的地址上.SHM_RND命令的意思是取整,SHMLAB的意思是低边界地址的倍数,它总是2的乘方。该算式是将地址向下取最近一个 SHMLAB的倍数。除非只计划在一种硬件上运行应用程序(这在当今是不大可能的),否则不用指定共享段所连接到的地址。所以一般应指定shmaddr为0,以便由内核选择地址。
第三个参数如果在flag中指定了SHM_RDONLY位,则以只读方式连接此段,否则以读写的方式连接此段
shmat返回值是该段所连接的实际地址 如果出错返回-1
shmdt()函数
shmdt函数的作用是将共享内存从当前进程中分离。它的参数是shmat返回的地址指针。成功时它返回0,失败时返回-1.注意,将共享内存分离并未删除它,只是使得该内存对当前进程不再可用。
接下来我们运行另外一个程序:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int arg,char *args[]){
char *shmbuf;
int shmid=0;
if(arg>1){
shmid=atoi(args[1]);
shmbuf=shmat(shmid,0,0);
sleep(60);
shmdt(shmbuf);
}
return 0;
}
在一个终端:
xin@xin-Lenovo-V3000:~/code/guandao/sharememory$ ./make2 45809689
在另外一个终端:ipcs -m,看到我们建立的共享内存发生了变化,连接数从0变成了1。
0x00000000 45809689 xin 666 1024 1
综合举例:完成共享内存的读写
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include<sys/ipc.h>
#include<sys/shm.h>
int main(int arg,char *args[]){
char *shmbuf;
int shmid=0;
if(arg>2){
shmid=atoi(args[1]);
shmbuf=shmat(shmid,0,0);
if(atoi(args[2])==1){
scanf("%s",shmbuf);
}
if(atoi(args[2])==2){
printf("%s\n",shmbuf);
}
shmdt(shmbuf);
}
return 0;
}
结果为:
xin@xin-Lenovo-V3000:~/code/guandao/sharememory$ ./make3 45809689 1
hello
xin@xin-Lenovo-V3000:~/code/guandao/sharememory$ ./make3 45809689 2
hello
实现了进程间通信。