第二十七章 system v消息队列(三)
消息队列实现回射客户/服务器

msg_srv.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while (0)
#define MSGMAX 8192
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[MSGMAX]; /* message data */
};
void echo_srv(int msgid)
{
int nRec = 0;
struct msgbuf msg;
while(1)
{
if( (nRec = msgrcv(msgid, &msg, MSGMAX, 1, 0)) < 0)
ERR_EXIT("msgsnd");
int pid;
pid = *((int*)msg.mtext);
fputs(msg.mtext+4,stdout);
msg.mtype = pid;
msgsnd(msgid, &msg, nRec, 0);
}
}
int main(int argc, char* argv[])
{
int msgid;
msgid = msgget(1234,IPC_CREAT|0666);
if(msgid == -1)
ERR_EXIT("msgget");
printf("msget success, msgid=%d\n",msgid);
echo_srv(msgid);
return 0;
}
msg_cli.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while (0)
#define MSGMAX 8192
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[MSGMAX]; /* message data */
};
void echo_cli(int msgid)
{
int pid,nRec;
struct msgbuf msg;
memset(&msg, 9, sizeof(msg));
pid = getpid();
*((int*)msg.mtext) = pid;
while(fgets(msg.mtext+4, MSGMAX, stdin) != NULL)
{
msg.mtype = 1;
if(msgsnd(msgid, &msg, 4+strlen(msg.mtext+4), 0) < 0)
ERR_EXIT("msgsnd");
memset(msg.mtext+4, 0, MSGMAX-4);
if( (nRec = msgrcv(msgid, &msg, MSGMAX, pid, 0)) < 0)
ERR_EXIT("msgsnd");
fputs(msg.mtext+4, stdout);
memset(msg.mtext+4, 0, MSGMAX-4);
}
}
int main(int argc, char const *argv[])
{
int msgid;
msgid = msgget(1234,0);
if(msgid == -1)
ERR_EXIT("msgget");
echo_cli(msgid);
return 0;
}
当服务器端收到客户端的请求之后,需要向客户端回射数据, 此时服务器端处于往消息队列发送消息的状态;
这时很多客户端发起很多的请求,将队列读满了,此时服务器端发送将阻塞;而客户端也阻塞等待读取数据,这时就产生了死锁
如果使用非阻塞的模式发送,因为队列满了,没有办法往消息队列中填充数据,会返回EAGAIN错误,又迫使服务器再次往消息队列中填充数据,此时客户端依旧无法获取到数据

msg_cli.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <string.h>
#include <signal.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while (0)
#define MSGMAX 8192
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[MSGMAX]; /* message data */
};
void echo_cli(int snd_msgid, int rcv_msgid)
{
int pid,nRec;
struct msgbuf msg;
memset(&msg, 9, sizeof(msg));
*((int*)msg.mtext) = rcv_msgid;
while(fgets(msg.mtext+4, MSGMAX, stdin) != NULL)
{
msg.mtype = 1;
if(msgsnd(snd_msgid, &msg, 4+strlen(msg.mtext+4), 0) < 0)
ERR_EXIT("msgsnd");
sleep(1);
memset(msg.mtext+4, 0, MSGMAX-4);
if( (nRec = msgrcv(rcv_msgid, &msg, MSGMAX, 2, 0)) < 0)
ERR_EXIT("msgrcv");
fputs(msg.mtext+4, stdout);
memset(msg.mtext+4, 0, MSGMAX-4);
}
}
int main(int argc, char const *argv[])
{
int snd_msgid, rcv_msgid;
snd_msgid = msgget(1234,0);
rcv_msgid = msgget(IPC_PRIVATE,0666);
if(snd_msgid == -1 || rcv_msgid == -1)
ERR_EXIT("msgget");
echo_cli(snd_msgid,rcv_msgid);
return 0;
}
msg_srv.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while (0)
#define MSGMAX 8192
struct msgbuf {
long mtype; /* message type, must be > 0 */
char mtext[MSGMAX]; /* message data */
};
void echo_srv(int msgid)
{
int nRec = 0;
pid_t pid;
struct msgbuf msg;
while(1)
{
if( (nRec = msgrcv(msgid, &msg, MSGMAX, 1, 0)) < 0)
ERR_EXIT("msgsnd");
fputs(msg.mtext+4,stdout);
pid = fork();
if(pid == 0)
{
int nSnd;
int rcv_msgid;
rcv_msgid = *((int*)msg.mtext);
msg.mtype = 2;
nSnd = msgsnd(rcv_msgid, &msg, nRec, 0);
if(nSnd == -1)
{
perror("msgsnd");
}
}
}
}
int main(int argc, char* argv[])
{
int msgid;
msgid = msgget(1234,IPC_CREAT|0666);
if(msgid == -1)
ERR_EXIT("msgget");
echo_srv(msgid);
return 0;
}
第二十七章 system v消息队列(三)的更多相关文章
- 第二十五章 system v消息队列(一)
IPC对象的持续性 随进程持续 :一直存在直到打开的最后一个进程结束.(如pipe和FIFO) 随内核持续 :一直存在直到内核自举(内核自举就是把主引导记录加载到内存,并跳转执行这段内存)或显示删除( ...
- 第6章 System V消息队列
6.1 概述 System V消息队列在内核中是list存放的,头结点中有2个指针msg_first 和msg_last.其中每个节点包含:下个节点地址的指针.类型.长度.数据等. 6.2 函数 6. ...
- 第二十六章 system v消息队列(二)
msgsnd int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); 作用: 把一条消息添加到消息队列中 参数: msqi ...
- 进程间通信 System V 消息队列
1.msgget (key_t ket,int flag) ; //创建一个新的消息队列或者访问一个已存在的消息队列 2.msgsnd(int msid, const void *ptr ,size_ ...
- Linux进程通信之System V消息队列
System V消息队列是Open Group定义的XSI,不属于POSIX标准.System V IPC的历史相对很早,在上个世70年代后期有贝尔实验室的分支机构开发,80年代加入System V的 ...
- 利用System V消息队列实现回射客户/服务器
一.介绍 在学习UNIX网络编程 卷1时,我们当时可以利用Socket套接字来实现回射客户/服务器程序,但是Socket编程是存在一些不足的,例如: 1. 服务器必须启动之时,客户端才能连上服务端,并 ...
- UNIX环境高级编程——system V消息队列
unix早期通信机制中的信号能够传送的信息量有限,管道则只能传送无格式字节流,这远远是不够的. 消息队列(也叫报文队列)客服了这些缺点: 消息队列就是一个消息的链表. 可以把消 ...
- linux c编程:System V消息队列一
消息队列可以认为是一个消息链表,System V 消息队列使用消息队列标识符标识.具有足 够特权的任何进程都可以往一个队列放置一个消息,具有足够特权的任何进程都可以从一个给定队列读出一个消息.在某个进 ...
- linux网络编程之system v消息队列(二)
今天继续学习system v消息队列,主要是学习两个函数的使用,开始进入正题: 下面则开始用代码来使用一下该发送函数: 在运行之前,先查看一下1234消息队列是否已经创建: 用上次编写的查看消息队列状 ...
随机推荐
- Docker 为非root用户授权
Docker 为非root用户授权: 当运行docker pull busybox时候,会提示sky用户无法调用docker. 那么应该把sky用户加入docker用户组,不过在添加的时候,又提示了如 ...
- raspbian修改swap分区为硬盘上的分区
一直以为raspbian的swap分区和平常装系统时候的swap分区是一样的,最近用u盘插在树莓派上运行后发现这个交换分区和我想象中不一样. 百度后发现它是一个文件挂上去的.直接搞把! pi@rasp ...
- ionic3 浏览器端返回
首屏component.ts文件中使用setupBrowserBackButtonBehavior() { // Register browser back button action(s) wind ...
- 前端css杂记
1em等于16px,页面默认是16px,一个字宽高都等于16px; 当行内元素display:inline的元素设置float:left浮动后,display值也被设置成block;总结:无论行内元素 ...
- Flutter 的setState与FutureBuilder及EasyRefresh示例
用setState改变状态 class CpwsList extends StatefulWidget { _CpwsListState createState() => _CpwsListSt ...
- cogs2550. 冰桥,升起来了!
[问题背景] 11月16日: 今天要来到南极洲的一角来考察啦!南极的空气真的很好呢,只不过有点冷,雪什么的真是太可爱了!这次我要在一个冰谷(应该说是山谷的地方)考察,考察点在这山谷的两边(希望不要掉下 ...
- CentOS 7 的 systemctl 命令
Centos 7.* 使用 Systemd 进行系统初始化,因此,Centos 7.* 中我们可以使用 systemctl 管理系统中的服务. systemctl 管理的服务均包含了一个以 .serv ...
- Java并发编程总结(一)Syncronized解析
Syncronized解析 作用: )确保线程互斥的访问同步代码 )保证共享变量的修改能够及时可见 )有效解决重排序问题. 用法: )修饰普通方法(锁是当前实例对象) )修饰静态方法(锁是当前对象的C ...
- Linxu下Yii2的POST请求被拒经历
Linxu下Yii2的POST提交被拒经历 介于对Yii2的使用,浅谈一下自己的经验,在以往的项目中我使用的框架是Yii1,由于Yii2的出现,所以极力的想使用一下它的新特性. 我的使用环境Linux ...
- Jenkinsfile与Json的转换
前段时间调研了下青云的kubesphere,意外的发现了一个插件,pipeline-model-definition-plugin,用了将jenkins的pipeline.json互相转换的,以前可能 ...