Linux消息队列实践(2)
来源:程序员人生 发布时间:2014-12-15 09:27:17 阅读次数:4115次
消息队列函数
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
int msgget(key_t key, int msgflg);
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
msgctl函数
功能:获得/设置消息队列的信息
原型:
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
参数:
msqid: 由msgget函数返回的消息队列标识码
cmd:是将要采取的动作(见下)
返回值:
成功返回0,失败返回⑴
cmd:将要采取的动作(有3个可取值),分别以下:
消息队列数据结构
struct msqid_ds
{
struct ipc_perm msg_perm; /* Ownership and permissions */
time_t msg_stime; /* Time of last msgsnd(2) */
time_t msg_rtime; /* Time of last msgrcv(2) */
time_t msg_ctime; /* Time of last change */
unsigned long __msg_cbytes; /* Current number of bytes in
queue (nonstandard) */
msgqnum_t msg_qnum; /* Current number of messages
in queue */
msglen_t msg_qbytes; /* Maximum number of bytes
allowed in queue */
pid_t msg_lspid; /* PID of last msgsnd(2) */
pid_t msg_lrpid; /* PID of last msgrcv(2) */
};
Ipc_perm数据结构
struct ipc_perm
{
key_t __key; /* Key supplied to msgget(2) */
uid_t uid; /* Effective UID of owner */
gid_t gid; /* Effective GID of owner */
uid_t cuid; /* Effective UID of creator */
gid_t cgid; /* Effective GID of creator */
unsigned short mode; /* Permissions */
unsigned short __seq; /* Sequence number */
};
//实践:IPC_STAT
int main()
{
int msgid = msgget(0x1234, 0666);
if (msgid == ⑴)
{
err_exit("msgget error");
}
struct msqid_ds buf;
if (msgctl(msgid,IPC_STAT,&buf) == ⑴)
{
err_exit("msgctl error");
}
printf("buf.msg_perm.mode = %o
",buf.msg_perm.mode); //%o以8进制打印
cout << "buf.__msg_cbytes = " << buf.__msg_cbytes << endl;
cout << "buf.msg_qbytes = " << buf.msg_qbytes << endl;
cout << "buf.msg_lspid = " << buf.msg_lspid << endl;
}
//实践:IPC_SET,1般需要先获得,然后再设置
int main()
{
int msgid = msgget(0x1234, 0666);
if (msgid == ⑴)
{
err_exit("msgget error");
}
//获得消息队列的属性
struct msqid_ds buf;
if (msgctl(msgid,IPC_STAT,&buf) == ⑴)
{
err_exit("msgctl get error");
}
//设置消息队列的属性
buf.msg_perm.mode = 0644;
if (msgctl(msgid,IPC_SET,&buf) == ⑴)
{
err_exit("msgctl set error");
}
//获得并打印
if (msgctl(msgid,IPC_STAT,&buf) == ⑴)
{
err_exit("msgctl get error");
}
printf("buf.msg_perm.mode = %o
",buf.msg_perm.mode); //%o以8进制打印
}
//实践:IPC_RMID,删除消息队列
/**说明:可以通过在多个窗口上运行几个该程序,测试出:
消息队列并没有应用”援用计数”的功能!
*/
int main()
{
int msgid = msgget(0x1234, 0666);
if (msgid == ⑴)
{
err_exit("msgget error");
}
int choice = 0;
cout << "Please input Your choice: 0-delete, other-continue: ";
cin >> choice;
if (!choice)
{
//delete msg
if (msgctl(msgid,IPC_RMID,NULL) == ⑴)
{
err_exit("msgctl IPC_RMID error");
}
else
{
cout << "msgid = " << msgid << ", IPC_RMID OK!" << endl;
}
}
}
消息的发送和接收
msgsnd函数
功能:把1条消息添加到消息队列中
原型
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
参数
msgid: 由msgget函数返回的消息队列标识码
msgp:是1个指针,指针指向准备发送的消息,
msgsz:是msgp指向的消息长度,这个长度不含保存消息类型的那个long int长整型
msgflg:控制着当前消息队列满或到达系统上限时将要产生的事情
返回值:
成功返回0;失败返回⑴
msgflg=IPC_NOWAIT表示队列满不等待,返回EAGAIN毛病。
消息结构在两方面遭到制约。首先,它必须小于系统规定的上限值;其次,它必须以1个long int长整数开始,接收者函数将利用这个长整数肯定消息的类型
消息结构参考情势以下:
struct msgbuf
{
long mtype; /* message type, must be > 0 */
char mtext[1]; /* message data */
};
//实践
/**发送结构*/
struct msgBuf
{
long mtype; /* message type, must be > 0 */
char mtext[1024]; /* message data */
};
int main()
{
int msgid = msgget(0x1234,0666|IPC_CREAT);
if (msgid == ⑴)
{
err_exit("msgget error");
}
//初始化消息结构
struct msgBuf myBuffer;
myBuffer.mtype = 1;
strcpy(myBuffer.mtext,"Hello XiaoFang!");
//向消息队列发送消息
if (msgsnd(msgid,&myBuffer,strlen(myBuffer.mtext),IPC_NOWAIT) == ⑴)
{
err_exit("msgsnd error");
}
return 0;
}
msgrcv函数
功能:是从1个消息队列接收消息
原型
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
参数
msgid: 由msgget函数返回的消息队列标识码
msgp:是1个指针,指针指向准备接收的消息,
msgsz:是msgp指向的消息长度,这个长度不含保存消息类型的那个long int长整型
msgtype:它可以实现接收优先级的简单情势
msgflg:控制着队列中没有相应类型的消息可供接收时将要产生的事
返回值:
成功->返回实际放到接收缓冲区里去的字节数;失败->返回⑴
msgtyp |
msgtyp=0 | 返回队列第1条信息 |
msgtyp>0 | 返回队列第1条类型等于msgtype的消息 |
msgtyp<0 | 返回队列第1条类型小于等于msgtype绝对值的消息,并且是满足条件的消息类型最小的消息 |
msgflg |
msgflg=IPC_NOWAIT | 队列没有可读消息不等待,返回ENOMSG毛病。 |
msgflg=MSG_NOERROR | 消息大小超过msgsz时被截断 |
msgtyp>0且msgflg=MSG_EXCEPT | 接收类型不等于msgtype的第1条消息 |
//实践:消息发送
int main()
{
int msgid = msgget(0x1234,0666|IPC_CREAT);
if (msgid == ⑴)
{
err_exit("msgget error");
}
struct msgBuf myBuffer;
for (int i = 0; i < 128; ++i)
{
myBuffer.mtype = i+1;
sprintf(myBuffer.mtext,"Hello, My number is %d",i+1);
if (msgsnd(msgid,&myBuffer,strlen(myBuffer.mtext),IPC_NOWAIT) == ⑴)
{
err_exit("msgsnd error");
}
}
return 0;
}
//实践:消息接收:从队首不断的取数据
int main()
{
int msgid = msgget(0x1234,0666);
if (msgid == ⑴)
{
err_exit("msgget error");
}
//从队首不断的取数据,连续取10个
struct msgBuf myBuffer;
for (int i = 0; i < 10; ++i)
{
int recvBytes = 0;
if ((recvBytes = msgrcv(msgid,&myBuffer,sizeof(myBuffer.mtext),0,IPC_NOWAIT)) == ⑴)
{
err_exit("msgrcv error");
}
else
{
cout << "receive recvBytes = " << recvBytes << endl;
cout << "myBuffer.mtype = " << myBuffer.mtype << endl;
cout << " " << myBuffer.mtext << endl;
}
}
cout << "strlen(myBuffer.mtext) = " << strlen(myBuffer.mtext) << endl;
return 0;
}
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠