<>消息队列

 

/*msgque.c*/
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define  BUFSZ  512

struct message
{
 long msg_type;
 char msg_text[BUFSZ];
};

int main()
{
 int qid;
 key_t key;
 int len;
 struct message msg;
 
 /*根据不同的路径和关键表示产生标准的key*/
 if ((key = ftok(".", 'a')) == -1)
 {
  perror("ftok");
  exit(1);
 }
 
 /*创建消息队列*/
 if ((qid = msgget(key,IPC_CREAT|0666)) == -1)
 {
  perror("msgget");
  exit(1);
 }
 
 printf("Opened queue %d\n",qid);
 puts("Please enter the message to queue:");
 
 if ((fgets((&msg)->msg_text, BUFSZ, stdin)) == NULL)
 {
  puts("no message");
  exit(1);
 }
 
 msg.msg_type = getpid();
 len = strlen(msg.msg_text);
 
 /*添加消息到消息队列*/
 if ((msgsnd(qid, &msg, len, 0)) < 0)
 {
  perror("message posted");
  exit(1);
 }
 
 /*读取消息队列*/
 if (msgrcv(qid, &msg, BUFSZ, getpid(), 0) < 0)
 {
  perror("msgrcv");
  exit(1);
 }
 
 printf("message is:%s\n",(&msg)->msg_text);
 
 /*从系统内核中移走消息队列 */
 if ((msgctl(qid, IPC_RMID, NULL)) < 0)
 {
  perror("msgctl");
  exit(1);
 }
 
 exit(0);
}

执行:


lsb@ubuntu:~/gx/wangluo$ ./msgque
Opened queue 0
Please enter the message to queue:
12
结果:

message is:12

 ********************************

********************************

消息队列通信-写端


/* msgsnd.c */

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define  BUFFER_SIZE  512

struct message
{
 long msg_type;
 char msg_text[BUFFER_SIZE];
};

int main()
{
 int qid;
 key_t key;
 struct message msg;
 
 /*根据不同的路径和关键表示产生标准的key*/
 if ((key = ftok(".", 'a')) == -1)
 {
  perror("ftok");
  exit(1);
 }
 
 /*创建消息队列*/
 if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
 {
  perror("msgget");
  exit(1);
 }
 printf("Open queue %d\n",qid);
 
 while(1)
 {
  printf("Enter some message to the queue(enter 'quit' to exit):");
  if ((fgets(msg.msg_text, BUFFER_SIZE, stdin)) == NULL)
  {
   puts("no message");
   exit(1);
  }
  
  msg.msg_type = getpid();
  
  /*添加消息到消息队列*/
  if ((msgsnd(qid, &msg, strlen(msg.msg_text), 0)) < 0)
  {
   perror("message posted");
   exit(1);
  }
  
  if (strncmp(msg.msg_text, "quit", 4) == 0)
  {
   break;
  }
 }
 
 exit(0);
}

执行:

lsb@ubuntu:~/gx/wangluo$ ./msgsnd
Open queue 32768
Enter some message to the queue(enter 'quit' to exit):ni hao
Enter some message to the queue(enter 'quit' to exit): 等待写的内容

Enter some message to the queue(enter 'quit' to exit):ni xian zai zai na li
Enter some message to the queue(enter 'quit' to exit):
----------------------------------

消息队列通信-读端,现实写过来的信息


/* msgrcv.c */

#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define  BUFFER_SIZE  512

struct message
{
 long msg_type;
 char msg_text[BUFFER_SIZE];
};

int main()
{
 int qid;
 key_t key;
 struct message msg;
 
 /*根据不同的路径和关键表示产生标准的key*/
 if ((key = ftok(".", 'a')) == -1)
 {
  perror("ftok");
  exit(1);
 }
 
 /*创建消息队列*/
 if ((qid = msgget(key, IPC_CREAT|0666)) == -1)
 {
  perror("msgget");
  exit(1);
 }
 printf("Open queue %d\n", qid);
 
 do
 {
  /*读取消息队列*/
  memset(msg.msg_text, 0, BUFFER_SIZE);
  if (msgrcv(qid, (void*)&msg, BUFFER_SIZE, 0, 0) < 0)
  {
   perror("msgrcv");
   exit(1);
  }
  printf("The message from process %ld : %s", msg.msg_type, msg.msg_text);  
  
 } while(strncmp(msg.msg_text, "quit", 4));
 
 
 /*从系统内核中移走消息队列 */
 if ((msgctl(qid, IPC_RMID, NULL)) < 0)
 {
  perror("msgctl");
  exit(1);
 }
 
 exit(0);
}

 执行:

lsb@ubuntu:~/gx/wangluo$ ./msgrcv
Open queue 32768
The message from process 4651 : ni hao
The message from process 4651 : ni xian zai zai na li


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值