Unix/Linux进程间通信——(System V)消息队列
来源:互联网 发布:软件动态分析工具 编辑:程序博客网 时间:2024/06/05 01:56
System V消息队列实例:
/* http://beej.us/guide/bgipc/examples/kirk.c */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <string.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>struct my_msgbuf {long mtype;char mtext[200];};int main(void){struct my_msgbuf buf;int msqid;key_t key;if ((key = ftok("kirk.c", 'B')) == -1) {perror("ftok");exit(1);}if ((msqid = msgget(key, 0644 | IPC_CREAT)) == -1) {perror("msgget");exit(1);}printf("Enter lines of text, ^D to quit:\n");buf.mtype = 1; /* we don't really care in this case */while(fgets(buf.mtext, sizeof(buf.mtext), stdin) != NULL) {int len = strlen(buf.mtext);/* ditch newline at end, if it exists */if (buf.mtext[len-1] == '\n') buf.mtext[len-1] = '\0';if (msgsnd(msqid, &buf, len+1, 0) == -1) /* +1 for '\0' */perror("msgsnd");}if (msgctl(msqid, IPC_RMID, NULL) == -1) {perror("msgctl");exit(1);}return 0;}
/* http://beej.us/guide/bgipc/examples/spock.c */#include <stdio.h>#include <stdlib.h>#include <errno.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>struct my_msgbuf {long mtype;char mtext[200];};int main(void){struct my_msgbuf buf;int msqid;key_t key;if ((key = ftok("kirk.c", 'B')) == -1) { /* same key as kirk.c */perror("ftok");exit(1);}if ((msqid = msgget(key, 0644)) == -1) { /* connect to the queue */perror("msgget");exit(1);}printf("spock: ready to receive messages, captain.\n");for (;;) { /* Spock never quits! */if (msgrcv(msqid, &buf, sizeof(buf.mtext), 0, 0) == -1) {perror("msgrcv");exit(1);}printf("spock: \"%s\"\n", buf.mtext);}return 0;}
0 0
- Unix/Linux进程间通信——(System V)消息队列
- linux—进程通信IPC--system v-消息队列
- UNIX网络编程卷2进程间通信读书笔记—System V消息队列
- System V进程间通信—— 消息队列
- Linux 进程通信(System V)消息队列
- linux进程间通信-----System V消息队列总结实例
- System V进程间通信---消息队列
- UNP(卷2:进程间通信)—— 第6章:System V消息队列
- UNPv2:进程间通信(三) System V 消息队列
- linux进程间通信(system v消息队列,阻塞式)实例
- Linux C编程--进程间通信(IPC)5--System V IPC 机制1--消息队列
- Linux C编程--进程间通信(IPC)5--System V IPC 机制1--消息队列
- Linux进程间通信(IPC)编程实践(三) 详解System V消息队列(1)
- Linux进程通信之System V消息队列
- 【十】 进程间通信——[System V IPC对象]消息队列(message queue)
- 进程间通信(7) - 消息队列(System V)
- 进程学习:进程间通信(system v IPC)1.消息队列
- Linux进程间通信(IPC)编程实践(四) 详解System V消息队列(2)(msgsnd & msgrcv)
- RHEL 4 性能优化详述(/proc/sys/kernel/优化)
- 对于一个非常大的数,如何快速高效返回比它小的最大质数【腾讯笔试题】
- 函数式编程
- 各数据库的分页SQL语句
- 分析用户的网页浏览行为与习惯
- Unix/Linux进程间通信——(System V)消息队列
- 小米2及其他Android手机无法连接mac解决方案
- relocation R_X86_64_32 against `.rodata' can not be used when making a shared object;
- matlab 图像处理命令
- oracle中使用cube子句
- iOS---各种控件的创建方法和部分调用方法
- [回溯&&剪枝]Firetruck UVA208
- 【Guava的用法】2. collection
- 仅限于IE的将table中的数据导入导出excel