当前位置: 首页 > news >正文

1.23 消息队列

使用消息队列,实现两个终端相互聊天

程序代码:

w1.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;	key_t key;  
int id;
typedef struct msg
{long channel;      char buf[64];     
}msg_t;void msg_snd()
{msg_t arr;while(1){arr.channel = 1; printf("请输入:");scanf("%s",arr.buf);msgsnd(id,&arr,strlen(arr.buf),0);}
}void msg_rcv()
{int flag;msg_t arr;while(1){flag = msgrcv(id,&arr,sizeof(arr.buf),2,0);if(flag != -1)printf("\b\b\b\b\b\b\b\b收到的消息为:%s\n",arr.buf);memset(arr.buf,0,sizeof(arr.buf));}
}void handler(int signum)
{if(SIGINT == signum) {msgctl(id,IPC_RMID,NULL);exit(0);}
}
void* pthread_main(void* arg)
{msg_snd();
}int main(int argc, const char *argv[])
{pthread_t pthread_id;signal(SIGINT,handler);key = ftok("1.txt",1);if(key == -1){perror("ftok");}id = msgget(key,IPC_CREAT|0664);pthread_create(&pthread_id,0,pthread_main,NULL);pthread_detach(pthread_id);msg_rcv();return 0;
}

w2.c

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>typedef struct sockaddr_in addr_in_t;
typedef struct sockaddr addr_t;
typedef struct sockaddr_un addr_un_t;	key_t key;
int id;
typedef struct msg_data
{long channel;char buf[64];
}msg_t;
void msg_snd()
{msg_t arr;while(1){arr.channel = 2;printf("请输入:");scanf("%s",arr.buf);msgsnd(id,&arr,strlen(arr.buf),0);}
}void msg_rcv()
{int flag;msg_t arr;while(1){flag = msgrcv(id,&arr,sizeof(arr.buf),1,0);if(flag != -1)printf("\b\b\b\b\b\b\b\b收到的消息为:%s\n",arr.buf);memset(arr.buf,0,sizeof(arr.buf));}
}
void handler(int signum)
{if(SIGINT == signum){msgctl(id,IPC_RMID,NULL);exit(0);}
}
void* pthread_main(void* arg)
{msg_snd();
}
int main(int argc, const char *argv[])
{pthread_t pthread_id;signal(SIGINT,handler);key = ftok("1.txt",1);if(key == -1){perror("ftok");}id = msgget(key,IPC_CREAT|0664);pthread_create(&pthread_id,0,pthread_main,NULL);pthread_detach(pthread_id);msg_rcv();return 0;
}

http://www.lryc.cn/news/525608.html

相关文章:

  • 【华为路由的arp配置】
  • 绘制决策树的尝试1
  • 概率论里的特征函数,如何用卷积定理去理解
  • Spring 是如何解决循环依赖问题
  • Linux 目录操作详解
  • Elasticsearch的经典面试题及详细解答
  • Linux-arm(1)ATF启动流程
  • C#编程:List.ForEach与foreach循环的深度对比
  • C语言文件操作:标准库与系统调用实践
  • 代码随想录 栈与队列 test 7
  • C语言练习(21)
  • 智能手机“混战”2025:谁将倒下而谁又将突围?
  • 计算机图形学:实验一 OpenGL基本绘制
  • 二分查找题目:快照数组
  • 深度学习|表示学习|卷积神经网络|参数共享是什么?|07
  • 基于相机内参推导的透视投影矩阵
  • 浅析Dubbo 原理:架构、通信与调用流程
  • 03垃圾回收篇(D3_垃圾收集器的选择及相关参数)
  • 一、引论,《组合数学(第4版)》卢开澄 卢华明
  • Vue3+TS 实现批量拖拽文件夹上传图片组件封装
  • 二叉树的所有路径(力扣257)
  • Python OrderedDict 实现 Least Recently used(LRU)缓存
  • LabVIEW项目中的工控机与普通电脑选择
  • Ansys Speos | Speos Meshing 网格最佳实践
  • elasticsearch segment数量对读写性能的影响
  • 全同态加密理论、生态现状与未来展望(中2)
  • 鸿蒙UI(ArkUI-方舟UI框架)-开发布局
  • RPC是什么?和HTTP区别?
  • Linux C\C++编程-建立文件和内存映射
  • 行政纠错——pycorrector学习