Linux 进程间通讯详解七
- 上图的一台主机服务器架构的重大缺陷是容易死锁
- 因为客户端,服务器都往同一消息队列中发送接收消息,假设消息队列已经满了,此时客户端无法向队列中发送消息,阻塞了,
而服务器接收完一条消息后,想向消息队列发送消息,由于消息队列已经满了,也阻塞了,此时就会死锁。
- 改进型的一台主机服务器架构
- 建立两个消息队列,一个用于客户端写入服务器接收,一个用于服务器发送客户端接收,这样则永远不会出现死锁
- //本机客户端
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- #include <pthread.h>
- struct msgbuf
- {
- long mtype; /* message type, must be > 0 */
- char mtext[]; /* message data */
- };
- int get_msqid(const char *pathname)
- {
- if (pathname == NULL)
- {
- printf("get_msqid() params not correct !\n");
- return -;
- }
- char *pvalue1 = getenv(pathname);
- if (pvalue1 == NULL)
- {
- printf("getenv() failed !\n");
- return -;
- }
- key_t sendkey = ftok(pvalue1, );
- if (sendkey == -)
- {
- perror("ftok() err");
- return -;
- }
- int msqid = msgget(sendkey, | IPC_CREAT | IPC_EXCL);
- if (msqid == -)
- {
- if (errno == EEXIST)
- {
- printf("该消息队列已经存在!\n");
- msqid = msgget(sendkey, );
- } else
- {
- perror("msgget() err");
- return -;
- }
- }
- return msqid;
- }
- void * start_routine(void * arg)
- {
- //客户端发送消息队列
- int msqid = get_msqid("SENDFILE");
- struct msgbuf buf;
- memset(&buf, , sizeof(buf));
- buf.mtype = ;
- //数据的前4个字节存放当前进程的pid
- *((int *) buf.mtext) = getpid();
- while (fgets(buf.mtext + , , stdin) != NULL)
- {
- //发送消息队列
- if (msgsnd(msqid, &buf, sizeof(int) + strlen(buf.mtext + ), ) == -)
- {
- perror("msgsnd() err");
- return NULL;
- }
- memset(buf.mtext + , , );
- }
- return NULL;
- }
- int main(int arg, char * args[])
- {
- //开启多线程
- pthread_t thr1;
- //设置分离线程
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
- if (pthread_create(&thr1, &attr, start_routine, NULL) != )
- {
- printf("pthread_create() failed !\n");
- return -;
- }
- //客户端接收消息队列
- int msqid = get_msqid("RECVFILE");
- //recv
- int rc = ;
- struct msgbuf buf;
- while ()
- {
- memset(&buf, , sizeof(buf));
- rc = msgrcv(msqid, &buf, , getpid(), );
- if (rc == -)
- {
- perror("msgrcv() err");
- break;
- }
- printf("服务器有消息到来,消息长度是%d\n",rc);
- fputs(buf.mtext, stdout);
- }
- return ;
- }
- //本机服务器
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- struct msgbuf
- {
- long mtype; /* message type, must be > 0 */
- char mtext[]; /* message data */
- };
- int get_msqid(const char *pathname)
- {
- if (pathname == NULL)
- {
- printf("get_msqid() params not correct !\n");
- return -;
- }
- char *pvalue1 = getenv(pathname);
- if (pvalue1 == NULL)
- {
- printf("getenv() failed !\n");
- return -;
- }
- key_t sendkey = ftok(pvalue1, );
- if (sendkey == -)
- {
- perror("ftok() err");
- return -;
- }
- int msqid = msgget(sendkey, | IPC_CREAT | IPC_EXCL);
- if (msqid == -)
- {
- if (errno == EEXIST)
- {
- printf("该消息队列已经存在!\n");
- msqid = msgget(sendkey, );
- } else
- {
- perror("msgget() err");
- return -;
- }
- }
- return msqid;
- }
- int main(int arg, char * args[])
- {
- /*客户端的发送消息队列,是服务器的接收消息队列*/
- int send_msqid = get_msqid("RECVFILE");
- if (send_msqid == -)
- return -;
- int recv_msqid = get_msqid("SENDFILE");
- if (recv_msqid == -)
- return -;
- //接收消息再发送
- int rc = ;
- struct msgbuf recvbuf;
- struct msgbuf sendbuf;
- int pid=;
- while ()
- {
- memset(&recvbuf,,sizeof(struct msgbuf));
- memset(&sendbuf,,sizeof(struct msgbuf));
- rc = msgrcv(recv_msqid, &recvbuf, , , );
- if(rc==-)
- {
- perror("msgrcv() err");
- return -;
- }
- printf("客户端有数据到来!数据的长度是%d\n",rc);
- //解析数据
- pid=*((int *)recvbuf.mtext);
- sendbuf.mtype=pid;
- strcpy(sendbuf.mtext,recvbuf.mtext+);
- fputs(sendbuf.mtext,stdout);
- //发送
- if(msgsnd(send_msqid,&sendbuf,rc-,)==-)
- {
- perror("msgsnd() err");
- return -;
- }
- }
- return ;
- }
- .SUFFIXES:.c .o
- CC=gcc
- SRCS1=test01.c
- OBJS1=$(SRCS1:.c=.o)
- EXEC1=clt
- SRCS2=tec02.c
- OBJS2=$(SRCS2:.c=.o)
- EXEC2=ser
- start:$(OBJS1) $(OBJS2)
- $(CC) -o $(EXEC1) $(OBJS1) -lpthread
- $(CC) -o $(EXEC2) $(OBJS2)
- @echo "-------OK---------"
- .c.o:
- $(CC) -Wall -g -o $@ -c $<
- clean:
- rm -f $(OBJS1)
- rm -f $(OBJS2)
- rm -f $(EXEC1)
- rm -f $(EXEC2)
Linux 进程间通讯详解七的更多相关文章
- Linux 进程间通讯详解二
消息队列 --消息队列提供了本机上从一个进程向另外一个进程发送一块数据的方法 --每个数据块都被认为有一个类型,接收者进程接收的数据块可以有不同的类型值 --消息队列也有管道一样的不足,就是每个消息的 ...
- Linux 进程间通讯详解一
进程间的通讯 两台主机间的进程通讯 --socket 一台主机间的进程通讯 --管道(匿名管道,有名管道) --System V进程间通信(IPC)包括System V消息队列,System V信号量 ...
- Linux 进程间通讯详解六
ftok()函数 key_t ftok(const char *pathname, int proj_id); --功能:创建系统建立IPC通讯 (消息队列.信号量和共享内存) 时key值 --参数 ...
- Linux 进程间通讯详解三
msgctl()函数 int msgctl(int msqid, int cmd, struct msqid_ds *buf); --参数 msqid:有msgget函数返回的消息队列标识码 cmd: ...
- Linux 进程间通讯详解五
msgrcv函数 ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg); --功能:是从一个消息队列接 ...
- Linux 进程间通讯详解四
msgsnd函数 int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); --功能:把一条消息添加到消息队列中 --参数 ...
- Linux 进程间通讯方式 pipe()函数 (转载)
转自:http://blog.csdn.net/ta893115871/article/details/7478779 Linux 进程间通讯方式有以下几种: 1->管道(pipe)和有名管道( ...
- Linux 进程间通讯
一.Linux 下进程间通讯方式 1)管道(Pipe)及有名管道(named pipe): 管道可用于具有亲缘关系进程间的通信,有名管道克服了管道没有名字的限制,因此,除具有管道所具有的功能外,它还允 ...
- [转] linux系统文件流、文件描述符与进程间关系详解
http://blog.sina.com.cn/s/blog_67b74aea01018ycx.html linux(unix)进程与文件的关系错综复杂,本教程试图详细的阐述这个问题. 包括: ...
随机推荐
- 解决WIN7与虚拟机CentOS的文件夹共享问题
一.系统与软件 WIN7 64bit.VirtualBox 5.0.14.CentOS 6.5.SecureCRT 7.2.3 二.使用文件夹共享需要安装增强功能,但是安装时无法读取光盘iso文件 三 ...
- 关于Lind.DDD.Api客户端的使用与知识分享
回到目录 关于Lind.DDD.Api的使用与客户端的调用 作者:张占岭 花名:仓储大叔 框架:Lind.DDD,Lind.DDD.Api 目录 Api里注册全局校验特性 1 Api中设置全局的Cor ...
- Oracle常用SQL查询(2)
三.查看数据库的SQL 1 .查看表空间的名称及大小 select t.tablespace_name, round ( sum (bytes / ( 1024 * 1024 )), 0 ) ts ...
- jQuery动画特效实例教程
本文以实例形式详细讲述了jQuery动画特效的实现方法. 1.自制折叠内容块 内容块如下: <div class="module"> <div cla ...
- O365(世纪互联)SharePoint 之文档库使用小记
前言 当O365越来越流行的时候,大家往往更多使用的是传统的Office功能,有太少订阅用户能触及到O365的一个非常棒的功能,叫做SharePoint online. 下面,我们就以图文并茂的方式, ...
- 项目实战工具类(一):PhoneUtil(手机信息相关)
可以使用的功能: 1.获取手机系统版本号 2.获取手机型号 3.获取手机宽度 4.获取手机高度 5.获取手机imei串号 ,GSM手机的 IMEI 和 CDMA手机的 MEID. 6.获取手机sim卡 ...
- android VelocityTracker 速度追踪器的使用及创建
VelocityTracker 速度追踪 第一,创建方式: VelocityTracker mVelocityTracker = new VelocityTracker .obtain() 第二, ...
- IOS开发基础知识--碎片14
1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...
- 原生js实现Ajax
一般来说,大家可能都会习惯用JQuery提供的Ajax方法,但是用原生的js怎么去实现Ajax方法呢? JQuery提供的Ajax方法: $.ajax({ url: , type: '', dataT ...
- Scrum vs. PMP vs. PRINCE2的发展趋势图
这时2013年来自Google Trends的两幅图,数据来自对“Jobs and Education”的统计,体现了这三种认证,或者视为三种项目实施方式的趋势. 下图是全球的趋势: 下图是美国的趋势 ...