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

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;
}

最新文章

  1. MySQL学习笔记五:数据类型
  2. 【动态规划】bzoj1642 [Usaco2007 Nov]Milking Time 挤奶时间
  3. Git: untrack a file in local repo only and keep it in the remote repo
  4. Masonry使用注意篇
  5. WCF: 没有终结点在侦听可以接受消息的 这通常是由于不正确的地址或者 SOAP 操作导致的。
  6. 使用gulp、yeoman、bower建站
  7. 发布站点到远程FTP根目录
  8. 每天一道LeetCode--326. Power of Three
  9. poj 2081 Recaman&amp;#39;s Sequence
  10. delegate-使用笔记
  11. page-object使用(1)
  12. WPF中图形表示语法详解(Path之Data属性语法)
  13. Spark调研笔记第2篇 - 怎样通过Sparkclient向Spark提交任务
  14. 201521123003《Java程序设计》第4周学习总结
  15. Druid数据库连接池源码分析
  16. 神奇的layout_weight属性
  17. IDEA忽略某些文件
  18. Linux curl 一例
  19. UNITY3d在移动设备上的一些优化实战
  20. 1.虚拟机中安装ubuntu

热门文章

  1. jQuery常用方法(二)-事件
  2. 浅谈个人对客户端JavaScript同步、异步、执行顺序等概念的理解
  3. centos7 scrapy安装
  4. 滴滴热力图-php版(后面有js版本)
  5. HTML基础知识(块级标签,行内标签,行内块标签)
  6. spring5 源码深度解析----- 事务增强器(100%理解事务)
  7. STM32进阶之串口环形缓冲区实现
  8. COGS 2089. 平凡的测试数据
  9. MyEclipse注册代码
  10. SEER流量众筹模块开发测试网络及使用文档发布