linux下IPC之消息队列
来源:互联网 发布:个体户域名备案 编辑:程序博客网 时间:2024/05/21 09:23
创建消息对列:
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <errno.h>#include <sys/msg.h>//msg data structtypedef struct Msg{ int mtype;//get msg positive int start; int end;}Msg;int main(int argc, char *argv[]){ if (argc < 2) { fprintf(stderr, "usage:%s\n", argv[1]); exit(1); } //1st,path; 2nd,int(alt) key_t key = ftok(argv[1], 1); //msg id num long int msq_id; //create a msg deque,return a msgid msq_id = msgget(key, IPC_CREAT | IPC_EXCL | 0777); if (msq_id < 0) { fprintf(stderr, "msgget:%s\n", strerror(errno)); exit(1); } //send msg Msg m1 = {1, 10, 100}; Msg m2 = {2, 20, 200}; Msg m3 = {3, 30, 300}; Msg m4 = {4, 40, 400}; if (msgsnd(msq_id, &m1, sizeof(Msg), IPC_NOWAIT) < 0 || msgsnd(msq_id, &m2, sizeof(Msg), IPC_NOWAIT) < 0 || msgsnd(msq_id, &m3, sizeof(Msg), IPC_NOWAIT) < 0 || msgsnd(msq_id, &m4, sizeof(Msg), IPC_NOWAIT) < 0) { fprintf(stderr, "msgsnd:%s\n", strerror(errno)); exit(1); } //msg ctl struct msqid_ds msq_attr; if (msgctl(msq_id, IPC_STAT, &msq_attr) < 0) { fprintf(stderr, "msgctl:%s\n", strerror(errno)); exit(1); } else { printf("msg num:%ld\n", msq_attr.msg_qnum); printf("msg bytes:%ld\n", msq_attr.msg_qbytes); } return 0;}
接收消息对列的消息:
ude <stdlib.h>#include <string.h>#include <errno.h>//eque to send msgtypedef struct Msg{ int mtype; int start; int end;}Msg;int main(int argc, char *argv[]){ if(argc < 3) { fprintf(stderr, "usage:%s\n", argv[0]); exit(1); } key_t key; key = ftok(argv[1], 1); int msq_id; if(msq_id = msgget(key, 0777) < 0) { fprintf(stderr, "msgget:%s\n", strerror(errno)); exit(1); } Msg m; int type = atoi(argv[2]); if(msgrcv(msq_id, &m, sizeof(m), type, IPC_NOWAIT) < 0) { fprintf(stderr, "msgrcv:%s\n", strerror(errno)); exit(1); } else { fprintf("type: %d, start: %d, end: %d\n", m.mtype, m.start, m.end); exit(1); } return 0;}
0 0
- linux下IPC之消息队列
- Linux IPC 之消息队列
- linux IPC之消息队列
- linux IPC之消息队列
- Linux IPC 之消息队列
- Linux XSI IPC 之消息队列
- linux IPC之POSIX消息队列
- 【Linux】IPC通信之消息队列
- linux IPC-消息队列
- linux IPC-消息队列
- Linux IPC- 消息队列
- linux IPC--消息队列
- linux IPC---消息队列
- IPC之消息队列
- IPC之消息队列
- IPC之消息队列
- IPC之消息队列
- linux ipc—消息队列
- 【php基础】iconv 与 mb_convert_string 字符串转换
- 2015-1028 Ubuntu 翻译软件
- 云计算
- Apple Store 加速审核通道
- JDBC-规范化代码
- linux下IPC之消息队列
- mapreduce wordcount案例
- hdu1711Number Sequence(KMP)
- 数据结构-图
- VisualSVN Server的配置和使用方法
- UIautomator中的UiDevice相关的方法知识点
- coderforce 584B Kolya and Tanya(找规律的题)
- SQL Injection Prevention Cheat Sheet
- 如何编写更佳的bug report