消息队列函数由msgget、msgctl、msgsnd、msgrcv四个函数解析

消息队列函数由msgget、msgctl、msgsnd、msgrcv四个函数组成。下面的表格列出了这四个函数的函数原型及其具体说明。

1. msgget函数原型

msgget(得到消息队列标识符或创建一个消息队列对象)

所需头文件

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

函数说明

得到消息队列标识符或创建一个消息队列对象并返回消息队列标识符

函数原型

int msgget(key_t key, int msgflg)

函数传入值

key

0(IPC_PRIVATE):会建立新的消息队列

大于0的32位整数:视参数msgflg来确定操作。通常要求此值来源于ftok返回的IPC键值

msgflg

0:取消息队列标识符,若不存在则函数会报错

IPC_CREAT:当msgflg&IPC_CREAT为真时,如果内核中不存在键值与key相等的消息队列,则新建一个消息队列;如果存在这样的消息队列,返回此消息队列的标识符

IPC_CREAT|IPC_EXCL:如果内核中不存在键值与key相等的消息队列,则新建一个消息队列;如果存在这样的消息队列则报错

函数返回值

成功:返回消息队列的标识符

出错:-1,错误原因存于error中

附加说明

上述msgflg参数为模式标志参数,使用时需要与IPC对象存取权限(如0600)进行|运算来确定消息队列的存取权限

错误代码

EACCES:指定的消息队列已存在,但调用进程没有权限访问它

EEXIST:key指定的消息队列已存在,而msgflg中同时指定IPC_CREAT和IPC_EXCL标志

ENOENT:key指定的消息队列不存在同时msgflg中没有指定IPC_CREAT标志

ENOMEM:需要建立消息队列,但内存不足

ENOSPC:需要建立消息队列,但已达到系统的限制

如果用msgget创建了一个新的消息队列对象时,则msqid_ds结构成员变量的值设置如下:
Ÿ msg_qnum、msg_lspid、msg_lrpid、 msg_stime、msg_rtime设置为0。
Ÿ msg_ctime设置为当前时间。
Ÿ msg_qbytes设成系统的限制值。
Ÿ msgflg的读写权限写入msg_perm.mode中。
Ÿ msg_perm结构的uid和cuid成员被设置成当前进程的有效用户ID,gid和cuid成员被设置成当前进程的有效组ID。

2. msgctl函数原型

msgctl (获取和设置消息队列的属性)

所需头文件

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

函数说明

获取和设置消息队列的属性

函数原型

int msgctl(int msqid, int cmd, struct msqid_ds *buf)

函数传入值

msqid

消息队列标识符

cmd

IPC_STAT:获得msgid的消息队列头数据到buf中

IPC_SET:设置消息队列的属性,要设置的属性需先存储在buf中,可设置的属性包括:msg_perm.uid、msg_perm.gid、msg_perm.mode以及msg_qbytes

buf:消息队列管理结构体,请参见消息队列内核结构说明部分

函数返回值

成功:0

出错:-1,错误原因存于error中

错误代码

EACCESS:参数cmd为IPC_STAT,确无权限读取该消息队列

EFAULT:参数buf指向无效的内存地址

EIDRM:标识符为msqid的消息队列已被删除

EINVAL:无效的参数cmd或msqid

EPERM:参数cmd为IPC_SET或IPC_RMID,却无足够的权限执行

3. msgsnd函数原型

msgsnd (将消息写入到消息队列)

所需头文件

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

函数说明

将msgp消息写入到标识符为msqid的消息队列

函数原型

int msgsnd(int msqid, const void msgp, size_t msgsz, int msgflg)

函数传入值

msqid

消息队列标识符

msgp

发送给队列的消息。msgp可以是任何类型的结构体,但第一个字段必须为long类型,即表明此发送消息的类型,msgrcv根据此接收消息。msgp定义的参照格式如下:

    struct s_msg{ /msgp定义的参照格式/
     long type; /
 必须大于0,消息类型 /
           char mtext[256]; /
消息正文,可以是其他任何类型*/
    } msgp;

msgsz

要发送消息的大小,不含消息类型占用的4个字节,即mtext的长度

msgflg

0:当消息队列满时,msgsnd将会阻塞,直到消息能写进消息队列

IPC_NOWAIT:当消息队列已满的时候,msgsnd函数不等待立即返回

IPC_NOERROR:若发送的消息大于size字节,则把该消息截断,截断部分将被丢弃,且不通知发送进程。

函数返回值

成功:0

出错:-1,错误原因存于error中

错误代码

EAGAIN:参数msgflg设为IPC_NOWAIT,而消息队列已满

EIDRM:标识符为msqid的消息队列已被删除

EACCESS:无权限写入消息队列

EFAULT:参数msgp指向无效的内存地址

EINTR:队列已满而处于等待情况下被信号中断

EINVAL:无效的参数msqid、msgsz或参数消息类型type小于0

msgsnd()为阻塞函数,当消息队列容量满或消息个数满会阻塞。消息队列已被删除,则返回EIDRM错误;被信号中断返回E_INTR错误。
如果设置IPC_NOWAIT消息队列满或个数满时会返回-1,并且置EAGAIN错误。
msgsnd()解除阻塞的条件有以下三个条件:
① 不满足消息队列满或个数满两个条件,即消息队列中有容纳该消息的空间。
② msqid代表的消息队列被删除。
③ 调用msgsnd函数的进程被信号中断。

4. msgrcv函数原型

msgrcv (从消息队列读取消息)

所需头文件

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/msg.h>

函数说明

从标识符为msqid的消息队列读取消息并存于msgp中,读取后把此消息从消息队列中删除

函数原型

ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp,

                      int msgflg);

函数传入值

msqid

消息队列标识符

msgp

存放消息的结构体,结构体类型要与msgsnd函数发送的类型相同

msgsz

要接收消息的大小,不含消息类型占用的4个字节

msgtyp

0:接收第一个消息

>0:接收类型等于msgtyp的第一个消息

<0:接收类型等于或者小于msgtyp绝对值的第一个消息

msgflg

0: 阻塞式接收消息,没有该类型的消息msgrcv函数一直阻塞等待

IPC_NOWAIT:如果没有返回条件的消息调用立即返回,此时错误码为ENOMSG

IPC_EXCEPT:与msgtype配合使用返回队列中第一个类型不为msgtype的消息

IPC_NOERROR:如果队列中满足条件的消息内容大于所请求的size字节,则把该消息截断,截断部分将被丢弃

函数返回值

成功:实际读取到的消息数据长度

出错:-1,错误原因存于error中

错误代码

E2BIG:消息数据长度大于msgsz而msgflag没有设置IPC_NOERROR

EIDRM:标识符为msqid的消息队列已被删除

EACCESS:无权限读取该消息队列

EFAULT:参数msgp指向无效的内存地址

ENOMSG:参数msgflg设为IPC_NOWAIT,而消息队列中无消息可读

EINTR:等待读取队列内的消息情况下被信号中断

msgrcv()解除阻塞的条件有以下三个:
① 消息队列中有了满足条件的消息。
② msqid代表的消息队列被删除。
③ 调用msgrcv()的进程被信号中断。

消息队列使用程序范例

5. 消息队列控制范例

msgctl.c源代码如下:

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #include <error.h>
  7. #define TEXT_SIZE 512
  8. struct msgbuf
  9. {
  10. long mtype ;
  11. char mtext[TEXT_SIZE] ;
  12. } ;
  13. int main(int argc, char **argv)
  14. {
  15. int msqid ;
  16. struct msqid_ds info ;
  17. struct msgbuf buf ;
  18. struct msgbuf buf1 ;
  19. int flag ;
  20. int sendlength, recvlength ;
  21. msqid = msgget( IPC_PRIVATE, 0666 ) ;
  22. if ( msqid < 0 )
  23. {
  24. perror("get ipc_id error") ;
  25. return -1 ;
  26. }
  27. buf.mtype = 1 ;
  28. strcpy(buf.mtext, "happy new year!") ;
  29. sendlength = sizeof(struct msgbuf) - sizeof(long) ;
  30. flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
  31. if ( flag < 0 )
  32. {
  33. perror("send message error") ;
  34. return -1 ;
  35. }
  36. buf.mtype = 3 ;
  37. strcpy(buf.mtext, "good bye!") ;
  38. sendlength = sizeof(struct msgbuf) - sizeof(long) ;
  39. flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
  40. if ( flag < 0 )
  41. {
  42. perror("send message error") ;
  43. return -1 ;
  44. }
  45. flag = msgctl( msqid, IPC_STAT, &info ) ;
  46. if ( flag < 0 )
  47. {
  48. perror("get message status error") ;
  49. return -1 ;
  50. }
  51. printf("uid:%d, gid = %d, cuid = %d, cgid= %d\n" ,
  52. info.msg_perm.uid, info.msg_perm.gid, info.msg_perm.cuid, info.msg_perm.cgid ) ;
  53. printf("read-write:%03o, cbytes = %lu, qnum = %lu, qbytes= %lu\n" ,
  54. info.msg_perm.mode&0777, info.msg_cbytes, info.msg_qnum, info.msg_qbytes ) ;
  55. system("ipcs -q") ;
  56. recvlength = sizeof(struct msgbuf) - sizeof(long) ;
  57. memset(&buf1, 0x00, sizeof(struct msgbuf)) ;
  58. flag = msgrcv( msqid, &buf1, recvlength ,3,0 ) ;
  59. if ( flag < 0 )
  60. {
  61. perror("recv message error") ;
  62. return -1 ;
  63. }
  64. printf("type=%d, message=%s\n", buf1.mtype, buf1.mtext) ;
  65. flag = msgctl( msqid, IPC_RMID,NULL) ;
  66. if ( flag < 0 )
  67. {
  68. perror("rm message queue error") ;
  69. return -1 ;
  70. }
  71. system("ipcs -q") ;
  72. return 0 ;
  73. }

编译 gcc msgctl.c –o msgctl。
执行 ./msg,执行结果如下:

 
  1. uid:1008, gid = 1003, cuid = 1008, cgid= 1003
  2. read-write:666, cbytes = 1024, qnum = 2, qbytes= 163840
  3. ------ Message Queues --------
  4. key msqid owner perms used-bytes messages
  5. 0x00000000 65536 zjkf 666 1024 2
  6. type=3, message=good bye!
  7. ------ Message Queues --------
  8. key msqid owner perms used-bytes messages

6. 两进程通过消息队列收发消息

(1)发送消息队列程序

msgsnd.c源代码如下:

 
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #include <time.h>
  7. #define TEXT_SIZE 512
  8. struct msgbuf
  9. {
  10. long mtype ;
  11. int status ;
  12. char time[20] ;
  13. char mtext[TEXT_SIZE] ;
  14. } ;
  15. char *getxtsj()
  16. {
  17. time_t tv ;
  18. struct tm *tmp ;
  19. static char buf[20] ;
  20. tv = time( 0 ) ;
  21. tmp = localtime(&tv) ;
  22. sprintf(buf,"%02d:%02d:%02d",tmp->tm_hour , tmp->tm_min,tmp->tm_sec);
  23. return buf ;
  24. }
  25. int main(int argc, char **argv)
  26. {
  27. int msqid ;
  28. struct msqid_ds info ;
  29. struct msgbuf buf ;
  30. struct msgbuf buf1 ;
  31. int flag ;
  32. int sendlength, recvlength ;
  33. int key ;
  34. key = ftok("msg.tmp", 0x01 ) ;
  35. if ( key < 0 )
  36. {
  37. perror("ftok key error") ;
  38. return -1 ;
  39. }
  40. msqid = msgget( key, 0600|IPC_CREAT ) ;
  41. if ( msqid < 0 )
  42. {
  43. perror("create message queue error") ;
  44. return -1 ;
  45. }
  46. buf.mtype = 1 ;
  47. buf.status = 9 ;
  48. strcpy(buf.time, getxtsj()) ;
  49. strcpy(buf.mtext, "happy new year!") ;
  50. sendlength = sizeof(struct msgbuf) - sizeof(long) ;
  51. flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
  52. if ( flag < 0 )
  53. {
  54. perror("send message error") ;
  55. return -1 ;
  56. }
  57. buf.mtype = 3 ;
  58. buf.status = 9 ;
  59. strcpy(buf.time, getxtsj()) ;
  60. strcpy(buf.mtext, "good bye!") ;
  61. sendlength = sizeof(struct msgbuf) - sizeof(long) ;
  62. flag = msgsnd( msqid, &buf, sendlength , 0 ) ;
  63. if ( flag < 0 )
  64. {
  65. perror("send message error") ;
  66. return -1 ;
  67. }
  68. system("ipcs -q") ;
  69. return 0 ;
  70. }

(2)接收消息队列程序

msgrcv.c源代码如下:

 
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <unistd.h>
  4. #include <sys/ipc.h>
  5. #include <sys/msg.h>
  6. #define TEXT_SIZE 512
  7. struct msgbuf
  8. {
  9. long mtype ;
  10. int status ;
  11. char time[20] ;
  12. char mtext[TEXT_SIZE] ;
  13. } ;
  14. int main(int argc, char **argv)
  15. {
  16. int msqid ;
  17. struct msqid_ds info ;
  18. struct msgbuf buf1 ;
  19. int flag ;
  20. int recvlength ;
  21. int key ;
  22. int mtype ;
  23. key = ftok("msg.tmp", 0x01 ) ;
  24. if ( key < 0 )
  25. {
  26. perror("ftok key error") ;
  27. return -1 ;
  28. }
  29. msqid = msgget( key, 0 ) ;
  30. if ( msqid < 0 )
  31. {
  32. perror("get ipc_id error") ;
  33. return -1 ;
  34. }
  35. recvlength = sizeof(struct msgbuf) - sizeof(long) ;
  36. memset(&buf1, 0x00, sizeof(struct msgbuf)) ;
  37. mtype = 1 ;
  38. flag = msgrcv( msqid, &buf1, recvlength ,mtype,0 ) ;
  39. if ( flag < 0 )
  40. {
  41. perror("recv message error\n") ;
  42. return -1 ;
  43. }
  44. printf("type=%d,time=%s, message=%s\n", buf1.mtype, buf1.time, buf1.mtext) ;
  45. system("ipcs -q") ;
  46. return 0 ;
  47. }

(3)编译与执行程序

① 在当前目录下利用>msg.tmp建立空文件msg.tmp。
② 编译发送消息队列程序 gcc msgsnd.c -o msgsnd。
③ 执行./msgsnd,执行结果如下:
——- Message Queues ————
key msqid owner perms used-bytes messages
0x0101436d 294912 zjkf 600 1072 2
④ 编译接收消息程序 gcc msgrcv.c -o msgrcv
⑤ 执行./msgrcv,执行结果如下:
type=1,time=03:23:16, message=happy new year!

-——- Message Queues ————
key msqid owner perms used-bytes messages
0x0101436d 294912 zjkf 600 536 1
⑥ 利用ipcrm -q 294912删除该消息队列。因为消息队列是随内核持续存在的,在程序中若不利用msgctl函数或在命令行用ipcrm命令显式地删除,该消息队列就一直存在于系统中。另外信号量和共享内存也是随内核持续存在的。
在消息队列中,例如函数msgsnd(int msqid, const void* msgp, size_t msgsz, int msgflg);这个函数调用的时候,msgsz最大只能为8192,也就是2的16次方。可以看出这里的msgsz大小限制在一个short型。超过这个大小就会出错——invalid argument。并不是其他人所说的只要msgsz是mtext的大小就不会出错,如果sizeof(mtype)+sizeof(mtext)<=8192时,msgsz为sizeof(mtype)+sizeof(mtext)大小也没有关系。
当然msgsz这个大小也不是不可以改变,如果要变,就去内核代码里面关于实现消息队列的程序中把这个限制改变一下就好。(我没试过,应该可以),一般是在内核源码中的ipc文件夹中会有mqueue.c这个c语言程序文件,里面会定义DFLT_MSGSIZEMAX为8192,这应该就是为什么msgsz最大为8192的原因,如果要改,可以改掉,然后重新编译内核。

linux后台查看共享内存和消息队列的命令

ipcs
ipcs -q : 显示所有的消息队列
ipcs -qt : 显示消息队列的创建时间,发送和接收最后一条消息的时间
ipcs -qp: 显示往消息队列中放消息和从消息队列中取消息的进程ID
ipcs -q -i msgid: 显示该消息队列结构体中的消息信息:
ipcs -ql : 显示消息队列的限制信息:
取得ipc信息:
ipcs [-m|-q|-s]
-m 输出有关共享内存(shared memory)的信息
-q 输出有关信息队列(message queue)的信息
-s 输出有关“遮断器”(semaphore)的信息
删除ipc
ipcrm -m|-q|-s shm_id

摘录自《深入浅出Linux工具与编程》

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值