| 123456789101112131415161718192021222324252627282930 |
- 消息队列的函数定义如下:
- #include <sys/msg.h>
- int msgctl(int msqid, int cmd, struct msqid_ds *buf);
- int msgget(key_t key, int msgflg);
- int msgrcv(int msqid, void *msg_ptr, size_t msg_sz, long int msgtype, int msgflg);
- int msgsnd(int msqid, const void *msg_ptr, size_t msg_sz, int msgflg);
- Linux系统中有两个宏定义:
- MSGMAX, 以字节为单位,定义了一条消息的最大长度。
- MSGMNB, 以字节为单位,定义了一个队列的最大长度。
- struct my_msg_st {
- long int my_msg_type;
- char some_text[BUFSIZ];
- }some_data;
- 创建
- if (msgid = msgget((key_t)1234, 0666 | IPC_CREAT)) == -1)
- 接收
- if (msgrcv(msgid, (void *)&some_data, BUFSIZ, msg_to_receive, 0) == -1)
- 发送
- if (msgsnd(msgid, (void *)&some_data, MAX_TEXT, 0) == -1)
- 删除
- if (msgctl(msgid, IPC_RMID, 0) == -1)
|