实验八 消息队列

实验目的
(1)加深对进程概念的理解,明确进程和程序的区别;进一步认识并发执行的实质;
汽车安全带卡扣
(2)了解并熟悉Linux系统中消息机制的基本概念及方法;
(3)熟悉Linux提供的有关消息机制的系统调用函数/库函数,并能使用这些函数。
实验准备及预习
阅读讲义《附件9-消息队列》,了解Linux系统中利用管道实现进程通信的基本概念及方法,熟悉Linux提供的有关系统调用函数/库函数:msgget()、msgsnd()、msgrcv()、msgctl()。
实验内容
1使用函数msgget()创建一个消息队列,并在终端使用shell命令“ipcs -q”来查看是否创建成功、用“ipcrm –q msgid”删除指定消息队列。
#include<stdio.h>
#include<stdlib.h>
#include<sys/msg.h>
#define MYMSG_KEY 75
int main()
{    int msgid;
msgid=msgget(MYMSG_KEY,IPC_CREAT | 0644);
printf("msgid=%d\n",msgid);
if(msgid==-1){
perror("msgget error :");
exit(EXIT_FAILURE);
}
return 0;
}
2、利用函数msgsnd()向指定消息队列发送消息。运行该程序两次,写入2个消息,通过命令“ipcs –q”,查看消息队列中消息的数量。
#include<stdio.h>
#include<stdlib.h>
#include<sys/msg.h>
#include<string.h>极早期烟雾探测器
#define MYMSG_KEY 75
struct mymesg {
long mtype;        /* 消息类型*/
char mtext[512];    /* 消息文本*/
};
int main()
{    int msgid,msgsend_ret;
char buf[25];
struct mymesg msg_send;
//msgid=msgget(ftok(".",100),IPC_CREAT | 0644);
msgid=msgget (MYMSG_KEY,IPC_CREAT | 0644);
printf("msgid=%d\n",msgid);
if(msgid==-1){
perror("msgget error :");
exit(EXIT_FAILURE);
}
// write messages to msg queue
pe=1;
printf("enter a message:\n");
scanf(“%s”,buf);    //输入字符串不能包含空格
strcpy(,buf);
msgsend_ret=msgsnd(msgid,&msg_send,strlen()+1,0);
if(msgsend_ret==-1){
perror("msgget error: ");
exit(EXIT_FAILURE);
}
return 0;
}
3、利用函数msgrcv()把程序2运行两次写入消息队列的2个消息读取出来。执行该程序两次,把前一个程序运行时写入消息队列的两个消息都读出来;通过命令“ipcs –q”,查看消息队列中消息的数量。
#include<stdio.h>
#include<stdlib.h>
#include<sys/msg.h>
#include<string.h>
#define MYMSG_KEY 75
di palomostruct mymesg {
降香黄檀树
long mtype; /* positive message type */
char mtext[512]; /* message data, of length nbytes */
};
int main()
{    int msgid, msgrcv_ret;
char buf[25];
struct mymesg mymsgrece;
msgid=msgget(MYMSG_KEY,IPC_CREAT | 0644);
printf("msgid=%d\n",msgid);
if(msgid==-1){
perror("msgget error :");
exit(EXIT_FAILURE);
}
//read mseeags from msg queue
太阳能手电筒
msgrcv_ret=msgrcv(msgid,&mymsgrece, sizeof(struct mymesg)-sizeof(long),1,0);
//读取消息的类型为1,长度为sizeof(struct mymesg)-sizeof(long)
//读到的消息放到mymsgrece中
if(msgrcv_ret==-1){
perror("msgrcv error:");
exit(EXIT_FAILURE);
}
printf("received msg from queue : %s\n",);
景区拍照return 0;
}
例4:函数msgctl()可对消息队列进行处理,比如删除消息队列、获取消息队列的详细信息、改变消息队列的信息等。下面的程序利用利用该函数删除前例中创建的消息队列。
#include<stdio.h>
#include<stdlib.h>
#include<sys/msg.h>
#include<string.h>
#define MYMSG_KEY 75
int main()
{    int msgid,msgctl_ret;
msgid=msgget(MYMSG_KEY,IPC_CREAT | 0644);
printf("msgid=%d\n",msgid);
if(msgid==-1){
perror("msgget error :");
exit(EXIT_FAILURE);
}
//delete queue
msgctl_ret = msgctl(msgid,IPC_RMID,0);
if(msgctl_ret==-1){
perror("msgctl error :");
exit(EXIT_FAILURE);
}
printf("deleted queue %d ok.\n ",msgid);
return 0;
}

本文发布于:2024-09-22 00:54:52,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/183334.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:消息   队列   实验   系统   利用   进程   程序   删除
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议