linux应用开发基础知识(七)——管道和消息队列进程通信

管道通信

匿名管道

#include  <unistd.h>
int  pipe(int pfd[2]);

pfd[0]用于读管道,而pdf[1]用于写管道。
注意:匿名管道只能用于亲缘关系的进程之间通信。管道通道是单向的,一边读,另一边写。管道可以用于大于两个进程共享。
例如设计父进程读,两个子进程写的代码如下:

#include<stdio.h>
#include<pthread.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
int main(int argc, char* argv)
{
    int pipegd[2];
    int i;
    char buf[19]={0};
    pid_t pid;
    pid_t pid1;
    int pi = pipe(pipegd);
    if(pi<0)
{
    perror("pipe:");
    return -1;
}
pid=fork();
if(pid<0)
	{
	perror("pid create:");
	return 0;
	}
else if(pid == 0)
	{
	close(pipegd[0]);
	memset(buf, 0, 19);
	strcpy(buf, "this is b");
	write(pipegd[1], buf, strlen(buf));
	}
 else if(pid>0)
	{
	pid1=fork();
	if(pid1<0)
	{
	perror("pid create:");
	return 0;
	}
        else if(pid1 == 0)
	{
	close(pipegd[0]);
	memset(buf, 0, 19);
	strcpy(buf, "this is a");
	write(pipegd[1], buf, strlen(buf));
	}
        else if(pid1>0)
	{
        sleep(1);
	close(pipegd[1]);
	memset(buf,0,19);
	read(pipegd[0],buf,19);
	printf("pipe read is \"%s\"\n",buf);
	}
        }
return 0;
}

有名管道

有名管道也叫命名管道。
1、管道文件仅仅是文件系统中的标示,并不在磁盘上占据空间。在使用时,在内存上开辟空间,作为两个进程数据交互的通道。也就是提供一个路径名与之关联,这样,即使与创建有名管道的进程不存在亲缘关系的进程,只要可以访问该路径,就能够通过这个有名管道进行相互通信。
2、并且顾名思义,管道秉承着“先进先出”的原则。先写进去的数据,会被先读出来。
3、并且管道需要读进程先存在,否则写进程是没有什么意义的。
有名管道通信的API

#include  <unistd.h>
#include <fcntl.h>
//创建管道的头文件
#include <sys/types.h>
#include <sys/stat.h>
int  mkfifo(const char *path, mode_t mode);
open(const char *path, O_RDONLY);//1
open(const char *path, O_RDONLY | O_NONBLOCK);//无阻塞读
open(const char *path, O_WRONLY);//3
open(const char *path, O_WRONLY | O_NONBLOCK);//无阻塞写

并且需要注意
1)使用open函数打开管道文件
如果一个进程以只读(只写)打开,那么这个进程会被阻塞到open,直到另一个进程以只写(只读)或者读写。
2)使用read函数读取内容
read读取普通文件,read不会阻塞。而read读取管道文件,read会阻塞运行,直到管道中有数据或者所有的写端关闭。
利用上面的API实现父子进程通信:子进程写,父进程读

#include"stdio.h"
#include"unistd.h"
#include"string.h"
#include"fcntl.h"
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc,char* argv[])
{
    int pd,fd1,fd2;
    char buf[20]={0};
    char buf1[20]="hello,world!";
    char str[]="./my";
    pid_t pid;
    pd = mkfifo(str,S_IFIFO|0666);
    if(pd<0)
    {
        perror("mkfifo:");
        return -1;
    }
    pid = fork();
    if(pid<0)
    {
        perror("pthread creat");
    }
    if(pid==0)
    {
        fd2 = open(str,O_WRONLY);
        if(fd2<0)
        {
            perror("write:");
            return -1;
        }
        write(fd2,buf1,20);
    }
    else
    {
    fd1 = open(str,O_RDONLY); 
    if(fd1<0)
    {
        perror("read:");
        return -1;
    }
    read(fd1,buf,20);
    printf("read = %s\n",buf);
    }
return 0;
}

创建两个进程来实现非亲缘关系进程通信
read部分

#include"stdio.h"
#include"string.h"
#include"unistd.h"
#include"sys/types.h"
#include"sys/stat.h"
#include <fcntl.h>
int main(int argc,char* argv[])
{
    int pd,fd;
    pid_t pid;
    char str[]="./mypipe";
    char buf[]="you!";
    // pd = mkfifo(str,0666);
    // if(pd<0)
    // {
    //     perror("mkfifo");
    //     return -1;
    // }
    fd = open(str,O_WRONLY);
    if(fd<0)
    {
        perror("open");
        return -1;
    }
    while(1)
    {
    int ret = write(fd,buf,sizeof(buf));
    if(ret<0)
    {
        perror("write");
        return -1;
    }
    printf("write successful!");
    printf("write buf is %s",buf);
    sleep(1);
    }
    return 0;
}

write部分

#include"stdio.h"
#include"string.h"
#include"unistd.h"
//创建管道头文件
#include"sys/types.h"
#include"sys/stat.h"
//下面的大写宏定义头文件
#include <fcntl.h>
int main(int argc,char* argv[])
{
    int pd,fd;
    pid_t pid;
    char str[]="./mypipe";
    char buf[30];
    fd = open(str,O_RDONLY);
    if(fd<0)
    {
        perror("open");
        return -1;
    }
    read(fd,buf,30);
    printf("read is:%s\n",buf);
    return 0;
}

需要两边都开着才行,不然会进行堵塞。

总结就是:
(1)两个进程运行时,写端彻底关闭,则读端read返回0,也会关闭。
(2)如果管道的读端关闭,继续写入数据会发生异常,写端收到未捕获的信号SIGPIPE会关闭写端。
     当然也可以修改默认的信号响应方式,比如增加信号处理函数。
(3)写端写入数据以后,读端不从里面读取内容:数据保持在管道中存在一段时间。管道文件的大小是0。
(4)管道通讯发送的数据若没有指定进程接收,任何一个进程只要打开的是同一个管道文件,都有可能读到数据。
(5)read读取管道中的数据,只要读过的数据就会被清空 。

消息队列

消息队列的本质就是存放在内存中的消息的链表,而消息本质上是用户自定义的数据结构。如果进程从消息队列中读取了某个消息,这个消息就会被从消息队列中删除。
对比一下管道机制:

1、消息队列允许一个或多个进程向它写入或读取消息。
2、消息队列可以实现消息的随机查询,不一定非要以先进先出的次序读取消息,也可以按消息的类型读取。比有名管道的先进先出原则更有优势。
3、对于消息队列来说,在某个进程往一个队列写入消息之前,并不需要另一个进程在该消息队列上等待消息的到达。而对于管道来说,除非读进程已存在,否则先有写进程进行写入操作是没有意义的。
4、消息队列的生命周期随内核,如果没有释放消息队列或者没有关闭操作系统,消息队列就会一直存在。而匿名管道随进程的创建而建立,随进程的结束而销毁。

需要注意的是,消息队列对于交换较少数量的数据很有用,因为无需避免冲突。但是,由于用户进程写入数据到内存中的消息队列时,会发生从用户态拷贝数据到内核态的过程;同样的,另一个用户进程读取内存中的消息数据时,会发生从内核态拷贝数据到用户态的过程。因此,如果数据量较大,使用消息队列就会造成频繁的系统调用,也就是需要消耗更多的时间以便内核介入。

消息队列的一些API

 #include <sys/ipc.h>
 #include <sys/msg.h>
 key_t ftok(const char *pathname, int proj_id);把一个已存在的路径名和一个整数标识符转换成IPC键值
 int msgget(key_t key, int msgflg);创建一个消息队列的函数
 int msgsnd(int msgid, const void *msgp, size_t size, int msgflg);向这个消息队列发送消息
 int msgrcv(int msgid, void *msgp, size_t size, long msgtype, int msgflg);向这个消息队列读取消息
 int msgctl(int msgid, int cmd, struct msqid_ds *buf);消息队列的控制

对这些API的解释:

key_t ftok(const char *pathname, int proj_id);
把一个已存在的路径名和一个整数标识符转换成IPC键值

1、成功时返回返回key_t值(即IPC 键值),失败时返回-1;
2、pathname:指定的文件,此文件必须存在且可存取; proj_id:计划代号(project ID)。
3、在使用ftok()函数时,里面有两个参数,即fname和id,fname为指定的文件名,而id为子序列号,这个函数的返回值就是key,它与指定的文件的索引节点号和子序列号id有关,但是如果文件被删除然后重新创建了一个一样的,这个key值也会变的。

 int msgget(key_t key, int msgflg);
 创建一个消息队列的函数

1、成功时返回消息队列的id,失败时返回EOF(-1)
2、key和消息队列关联的key IPC_PRIVATE 或 ftok
3、msgflg 标志位: IPC_CREAT|0666,IPC_CREAT:没有创建则创建,有则打开。

int msgsnd(int msgid, const void *msgp, size_t size, int msgflg);
向这个消息队列发送消息

1、成功时返回0,失败时返回-1
2、msgid:消息队列id;msgp: 消息缓冲区地址;size: 消息正文长度
3、msgflg:标志位 0 或 IPC_NOWAIT
0: 当消息队列满时,msgsnd将会阻塞,直到消息能写进消息队列
IPC_NOWAIT: 当消息队列已满的时候,msgsnd函数不等待立即返回

int msgrcv(int msgid, void *msgp, size_t size, long msgtype, int msgflg);
向这个消息队列读取消息

1、成功时返回收到的消息长度,失败时返回-1
2、msgid : 消息队列id;msgp : 消息缓冲区地址;size : 指定接收的消息长度;msgtype : 指定接收的消息类型;
msgtype=0: 收到的第一条消息,任意类型。
msgtype>0: 收到的第一条 msg_type类型的消息。
msgtype<0: 接收类型等于或者小于msgtype绝对值的第一个消息。
例子:如果msgtype=-4,只接受类型是1、2、3、4的消息
3、msgflg : 标志位
0: 阻塞式接收消息
IPC_NOWAIT: 如果没有返回条件的消息调用立即返回,此时错误码为ENOMSG
MSG_EXCEPT: 与msgtype配合使用返回队列中第一个类型不为msgtype的消息

 int msgctl(int msgid, int cmd, struct msqid_ds *buf);消息队列的控制

1、成功时返回0,失败时返回-1
2、msgid : 消息队列id;cmd : 要执行的操作 IPC_STAT/ IPC_SET / IPC_RMID(删除)
3、buf : 存放消息队列属性的地址

创建两个.c文件来实现消息队列的写入和读取。

写入消息队列

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#define LEN (sizeof(MSG) - sizeof(long))
typedef struct
{
	long mtype;
	char mtext[64];
}MSG;
int main(int argc,char *argv[])
{
	MSG buf;
	key_t key;
	int msgid,mssend,msrecv;
	//fgets(buf.mtext,64,stdin);
	if((key = ftok(".",'q')) == -1)
	{
		perror("ftok");
		return -1;
	}
	if(msgid = msgget(key,IPC_CREAT|0666)<0)
	{
		perror("magid");
		return -1;
	}
    buf.mtype = 1;
	strcpy(buf.mtext, "this is message 1");
	if(mssend = msgsnd(msgid,buf.mtext,LEN,0)<0)
	{
		perror("mssend");
		return -1;
	}
	buf.mtype = 2;
	strcpy(buf.mtext, "this is message 2");
	if(mssend = msgsnd(msgid,buf.mtext,LEN,0)<0)
	{
		perror("mssend");
		return -1;
	}
	buf.mtype = 3;
	strcpy(buf.mtext, "this is message 3");
	if(mssend = msgsnd(msgid,buf.mtext,LEN,0)<0)
	{
		perror("mssend");
		return -1;
	}
	return 0;
}

读取消息队列

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdlib.h>
#define LEN (sizeof(MSG) - sizeof(long))
typedef struct
{
	long mtype;
	char mtext[64];
}MSG;
int main(int argc,char *argv[])
{
	MSG buf;
	key_t key;
	int msgid,msrecv;
	buf.mtype = 1;
    int count = 0;
	//fgets(buf.mtext,64,stdin);
	if((key = ftok(".",'q')) == -1)
	{
		perror("ftok");
		return -1;
	}
	if(msgid = msgget(key,IPC_CREAT|0666)<0)
	{
		perror("magid");
		return -1;
	}
    while(1)
    {
	if(msrecv = msgrcv(msgid,&buf,64,0,0) < 0)
	{
		perror("msrecv");
		return -1;
	}
    printf("msrecv = %s\n",buf.mtext);
    count++;
    if(count==3)
    break;
    }
	return 0;
}

最后结果

在这里插入图片描述

  • 17
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值