消息队列实现回射客户/服务器

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消息队列(三)的更多相关文章

  1. 第二十五章 system v消息队列(一)

    IPC对象的持续性 随进程持续 :一直存在直到打开的最后一个进程结束.(如pipe和FIFO) 随内核持续 :一直存在直到内核自举(内核自举就是把主引导记录加载到内存,并跳转执行这段内存)或显示删除( ...

  2. 第6章 System V消息队列

    6.1 概述 System V消息队列在内核中是list存放的,头结点中有2个指针msg_first 和msg_last.其中每个节点包含:下个节点地址的指针.类型.长度.数据等. 6.2 函数 6. ...

  3. 第二十六章 system v消息队列(二)

    msgsnd int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg); 作用: 把一条消息添加到消息队列中 参数: msqi ...

  4. 进程间通信 System V 消息队列

    1.msgget (key_t ket,int flag) ; //创建一个新的消息队列或者访问一个已存在的消息队列 2.msgsnd(int msid, const void *ptr ,size_ ...

  5. Linux进程通信之System V消息队列

    System V消息队列是Open Group定义的XSI,不属于POSIX标准.System V IPC的历史相对很早,在上个世70年代后期有贝尔实验室的分支机构开发,80年代加入System V的 ...

  6. 利用System V消息队列实现回射客户/服务器

    一.介绍 在学习UNIX网络编程 卷1时,我们当时可以利用Socket套接字来实现回射客户/服务器程序,但是Socket编程是存在一些不足的,例如: 1. 服务器必须启动之时,客户端才能连上服务端,并 ...

  7. UNIX环境高级编程——system V消息队列

    unix早期通信机制中的信号能够传送的信息量有限,管道则只能传送无格式字节流,这远远是不够的.     消息队列(也叫报文队列)客服了这些缺点:     消息队列就是一个消息的链表.     可以把消 ...

  8. linux c编程:System V消息队列一

    消息队列可以认为是一个消息链表,System V 消息队列使用消息队列标识符标识.具有足 够特权的任何进程都可以往一个队列放置一个消息,具有足够特权的任何进程都可以从一个给定队列读出一个消息.在某个进 ...

  9. linux网络编程之system v消息队列(二)

    今天继续学习system v消息队列,主要是学习两个函数的使用,开始进入正题: 下面则开始用代码来使用一下该发送函数: 在运行之前,先查看一下1234消息队列是否已经创建: 用上次编写的查看消息队列状 ...

随机推荐

  1. linux脚本入门之终端显示输出

    主要基本命令为 echo 与 printf. 关于echo: 其语法结构为:echo -选项参数 字符串: 例如:echo hello,world   echo 'hello,world'  echo ...

  2. IDEA 学习笔记之 多线程调试

    多线程调试: 在多线程调试的时候,发现一些断点会被跳过,让人很郁闷,然后上网查了下资料,发现是自己IDEA设置不对. 使用IDEA调试多线程的时候,IDEA的断点有不同的模式,只有当Thread模式下 ...

  3. [go设计模式]简单工厂模式

    优点 工厂类是整个模式的关键.包含了必要的逻辑判断,根据外界给定的信息,决定究竟应该创建哪个具体类的对象.通过使用工厂类,外界可以从直接创建具体产品对象的尴尬局面摆脱出来,仅仅需要负责“消费”对象就可 ...

  4. mobaxterm和CRT的文件上传

    版权声明:本文为博主原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/submarineas/article/de ...

  5. Hyper-V虚拟机win7网络红叉,无法上网解决方法

    之前一直都是玩Vmware虚拟机,后来win8之后的系统有Hyper-V虚拟机就开始接触了. Windows 中内置的Hyper-V管理器可以说是给很多人带来了惊喜!至少运行的流畅程度要比Vmware ...

  6. 【NOIP2009】道路游戏

    Description 小新正在玩一个简单的电脑游戏. 游戏中有一条环形马路,马路上有 nn 个机器人工厂,两个相邻机器人工厂之间由一小段马路连接.小新以某个机器人工厂为起点,按顺时针顺序依次将这 n ...

  7. Python实现电子邮件的发送

    利用Python smtplib.SMTP类方法来实现电子邮件的发送. 列举SMTP对象常见的方法: sendmail(from, to ,msg[,mopts,ropts]) :将msg从from发 ...

  8. 数据结构(java)

    数据结构1.什么是数据结构?数据结构有哪些? 数据结构是指数据在内存中存放的机制. 不同的数据结构在数据的查询,增删该的情况下性能是不一样的. 数据结构是可以模拟业务场景. 常见的数据结构有:栈,队列 ...

  9. C、C++的Makefile模板

    目录 Makefile模板 用法 编译C程序 编译C++程序 其他 Tips Makefile模板 CC = gcc LD = $(CC) TARGET = $(notdir $(CURDIR)) S ...

  10. Windows 7 SP1官方原版ISO系统镜像所有版本下载集合

    ========================================================================== Windows 7 With SP1 32位简体中 ...