消息队列
1.获取key:ftok();
2.获取或创建消息队列:
int msgget(key_t key, int msgflg);
@msgflg的值:
IPC_CREAT,IPC_EXEC,mode;
返回值:成功返回消息队列标识:msgid
3.发送消息:
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
@msgp:要发送的消息:
struct msgbuf {
long mtype; /*消息类型:消息优先级 message type, must be > 0 */必须为long类型
char mtext[1]; /*数据:char mess[128]; message data */
};
@msgsz消息总字节数:
@msgflg:标志位,IPC_NOWAIT
4.接收消息
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,int msgflg);
@msgp:要存储数据的首地址
@msgsz:消息的字节数
@msgtyp:接收哪种类型的消息,接收消息时是同种类型消息的先后顺序依次接收
@msgflg:IPC_NOWAIT; 非阻塞
5.删除消息队列
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
IPC_RMID
@buf:只有在IPC_SET,IPC_STAT,IPC_INFO有效
#endif
删除消息队列:ipcrm -q 消息队列号
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
struct msgbuf{
long mtype;
char mess[128];
};
int main()
{
//获取key
key_t key = ftok(".",100);
system("ipcs -q");
//获取或创建消息队列
int msgid = msgget(key,IPC_CREAT | 0600);
if(msgid < 0)
{
perror("msgget error");
return -1;
}
//发送消息
system("ipcs -q");
struct msgbuf buf = {1,"zhangsan"};
if(msgsnd(msgid,&buf,sizeof(buf),0) < 0)
{
perror("msgnd error");
return -1;
}
system("ipcs -q");
//接收消息
struct msgbuf recbuf = {0};
msgrcv(msgid,&recbuf,sizeof(recbuf),1,0);
system("ipcs -q");
printf("%lu %s",recbuf.mtype,recbuf.mess);
msgctl(msgid,IPC_RMID,NULL);
system("ipcs -q");
exit(0);
}
}
描述消息队列的结构体
系统描述消息队列的结构体
struct msqid_ds {
struct ipc_perm msg_perm;
struct msg *msg_first; /*第1条消息 first message on queue,unused */
struct msg msg_last; / last message in queue,unused /
__kernel_time_t msg_stime; / last msgsnd time /
__kernel_time_t msg_rtime; / last msgrcv time /
__kernel_time_t msg_ctime; / last change time /
unsigned long msg_lcbytes; / Reuse junk fields for 32 bit /
unsigned long msg_lqbytes; / ditto /
unsigned short msg_cbytes; / current number of bytes on queue /
unsigned short msg_qnum; / number of messages in queue /
unsigned short msg_qbytes; / max number of bytes on queue /
__kernel_ipc_pid_t msg_lspid; / pid of last msgsnd /
__kernel_ipc_pid_t msg_lrpid; / last receive pid */
};
1.消息队列可以按优先级(消息类型)接收消息
2.接收时:msgrcv()的消息类型;
msgtyp == 0,按队列的先后顺序接收消息
msgtyp > 0,按消息的类型接收消息
msgtyp < 0,按先后顺序接收绝对值最小的消息类型的
3.消息队列可以异步通信,但是不能重启系统或手动删除消息队列
4.消息具原子性
#endif
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
struct msgbuf{
long mtype;
char mess[128];
};
int main()
{
key_t key = ftok(".",100);
system("ipcs -q");
int msgid = msgget(key,IPC_CREAT | 0600);
if(msgid < 0)
{
perror("msgget error");
return -1;
}
int len = -1;
#if 0
system("ipcs -q");
struct msgbuf buf = {1,"zhangsan"};
len = msgsnd(msgid,&buf,sizeof(buf),0);
printf("msgsnd buf1 len = %d\n",len);
struct msgbuf buf2 = {1,"lisi"};
len = msgsnd(msgid,&buf2,sizeof(buf2),0);
printf("msgsnd buf2 len = %d\n",len);
struct msgbuf buf3 = {2,"wanguw"};
len = msgsnd(msgid,&buf3,sizeof(buf3),0);
printf("msgsnd buf3 len = %d\n",len);
#endif
system("ipcs -q");
#if 1
struct msgbuf rcvbuf = {0};
len = msgrcv(msgid,&rcvbuf,sizeof(rcvbuf),-2,0);
printf("msgrcv len = %d,rcvbuf.mess = %s\n",len,rcvbuf.mess);
#endif
#if 0
system("ipcs -q");
msgctl(msgid,IPC_RMID,NULL);
#endif
system("ipcs -q");
exit(0);
}
使用消息队列为父子进程进行进程间全双工通信
#if 0
使用消息队列为父子进程进行进程间全双工通信
ftok();
msgget();
pid = fork();
if(0 == pid)
{
//子发1收2
msg_t;
while(“quit”)
{
fgets(); //从控制台输入
msgsnd();
printf();
msgrcv();
printf()
}
}
else if(pid > 0)
{
//父发2收1
msg_t;
while(quit)
{
msgrcv();
printf();
msgsnd();
printf();
}
wait();
msgctl();
}
作业:使用消息队列在非父子进程间进行全双工通信
#endif
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
typedef struct msgbuf{
long mtype;
char mess[128];
}msg_t;
int main()
{
key_t key = ftok(".",100);
int msgid = msgget(key,IPC_CREAT | 0600);
pid_t pid = fork();
if(pid < 0)
{
perror("fork error");
msgctl(msgid,IPC_RMID,NULL);
return -1;
}
else if(0 == pid)
{
msg_t cmess = {0};
int len = -1;
while(strcmp("quit\n",cmess.mess))
{
memset(cmess.mess,0,sizeof(cmess.mess));
cmess.mtype = 1;
fgets(cmess.mess,sizeof(cmess.mess) - 1,stdin);
len = msgsnd(msgid,&cmess,sizeof(cmess),0);
printf("child %d send len = %d\n",getpid(),len);
memset(cmess.mess,0,sizeof(cmess.mess));
len = msgrcv(msgid,&cmess,sizeof(cmess),2,0);
printf("child %d recv len = %d,mess = %s\n",getpid(),len,cmess.mess);
}
exit(0);
}
else if(pid > 0)
{
msg_t cmess = {0};
int len = -1;
while(strcmp("quit\n",cmess.mess))
{
memset(cmess.mess,0,sizeof(cmess.mess));
len = msgrcv(msgid,&cmess,sizeof(cmess),1,0);
printf("parent %d recv len = %d,mess = %s\n",getpid(),len,cmess.mess);
memset(cmess.mess,0,sizeof(cmess.mess));
cmess.mtype = 2;
fgets(cmess.mess,sizeof(cmess.mess) - 1,stdin);
len = msgsnd(msgid,&cmess,sizeof(cmess),0);
printf("parent %d send len = %d\n",getpid(),len);
}
wait(NULL);
msgctl(msgid,IPC_RMID,NULL);
}
}