Linux System V IPC 消息隊列
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
//獲取key值, key值是System V IPC的標識符,成功返回key,失敗返回-1設errno
//同pathname+同 proj_id==>同key_t;
key_t ftok(const char *pathname, int proj_id);
pathname :文件名
proj_id: 1~255的一個數,表示project_id
key_t key=ftok(".",100); //“.”就是一個存在且可訪問的路徑, 100是假設的proj_id
if(-1==key)
perror("ftok"),exit(-1);
//創建/獲取消息隊列,成功返回shmid,失敗返回-1
int msgget(key_t key, int msgflg); //ATTENTION:用int msqid=msgget()比較好看
msgflg:具體的操作標志
消息隊列的容量由msg_qbytes控制,在消息隊列被創建的過程中,這個大小被初始化為MSGMNB,這個限制可以通過msgctl()修改
int msqid=msgget(key,IPC_CREAT|IPC_EXCL|0664);
if(-1==msqid)
perror("msgget"),exit(-1);
//向指定的消息隊列發送指定的消息,如果消息隊列已經滿了,默認的行為是堵塞,直到隊列有空間容納新的消息,成功返回0,失敗返回-1設errno
int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);
msqid msgget()返回的消息隊列的ID
msgp消息的的首地址, 消息的參考數據類型如下
struct msgbuf {
long mtype; /* message type, must be > 0 */ //消息的類型
char mtext[1]; /* message data */ //消息的內容
};
ATTENTION:The mtext field is an array (or other structure) whose size is specified by msgsz, a nonnegative integer value.
msgsz消息的大小, 該參數用於指定消息內容的大小, 不包括消息的類型。只能sizeof(Msgbuf.mtext),不能sizeof(Msgbuf)
msgflg發送的標志, 默認給0即可
Msg msg1={1,"hello"};//消息的類型是1,內容是hello
int res=msgsnd(msqid,&msg2,sizeof(msg2.buf),0);
if(-1==res)
perror("msgsnd"),exit(-1);
//向指定的消息隊列取出指定的消息,成功返回實際接受到的byte數,失敗返回-1設errno
ssize_t msgrcv(int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg);
msqid: 消息隊列的ID(returned by msgget)
msgp: 存放接收到消息的緩沖區首地址
msgsz: 消息的最大大小, 不包括消息的類型==>只能sizeof(Msgbuf.mtext),不能sizeof(Msgbuf)
msgtyp: 消息的類型
msgflg: 發送的標志, 默認給0即可
Msg msg1;
int res=msgrcv(msqid,&msg1,sizeof(msg1.buf),1,0);
if(-1==res)
perror("msgrcv"),exit(-1);
// 消息操作,成功返回0,失敗返回-1設errno
int msgctl(int msqid, int cmd, struct msqid_ds *buf);
msqid :消息隊列的ID,由msgget()
buf 結構體指針
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) */
};
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 */
};
cmd
IPC_INFO (Linux-specific)返回整個系統對與消息隊列的限制信息到buf指向的結構體中
//_GNU_SOURCE
//<sys/msg.h>
struct msginfo {
int msgpool;/*Size in kibibytes of buffer pool used to hold message data; unused within kernel*/
int msgmap; /*Maximum number of entries in message map; unused within kernel*/
int msgmax; /*Maximum number of bytes that can be written in a single message*/
int msgmnb; /*Maximum number of bytes that can be written to queue; used to initialize msg_qbytes during queue creation*/
int msgmni; /*Maximum number of message queues*/
int msgssz; /*Message segment size; unused within kernel*/
int msgtql; /*Maximum number of messages on all queues in system; unused within kernel*/
unsigned short int msgseg; /*Maximum number of segments; unused within kernel*/
};
int res=msgctl(msqid,IPC_RMID,NULL);
if(-1==res)
perror("msgctl"),exit(-1);
//Sys V IPC msg
#include<sys/types.h>
#include<sys/ipc.h>
#include<signal.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct{
long mtype; //消息的類型
char buf[20]; //消息的內容
}Msg;
int msqid; //使用全局變量,這樣就可以在fa中使用msqid了
void fa(int signo){
printf("deleting..\n");
sleep(3);
int res=msgctl(msqid,IPC_RMID,NULL);
if(-1==res)
perror("msgctl"),exit(-1);
exit(0);
}
int main(){
//ftok()
key_t key=ftok(".",150);
if(-1==key)
perror("ftok"),exit(-1);
printf("key%#x\n",key);
//msgget()
msqid=msgget(key,IPC_CREAT|IPC_EXCL|0664);
if(-1==msqid)
perror("msgget"),exit(-1);
printf("msqid%d\n",msqid);
//msgsnd()
Msg msg1={1,"hello"};//消息的類型是1,內容是hello
Msg msg2={2,"world"};
int res=msgsnd(msqid,&msg2,sizeof(msg2.buf),0);
if(-1==res)
perror("msgsnd"),exit(-1);
res=msgsnd(msqid,&msg1,sizeof(msg1.buf),0);
if(-1==res)
perror("msgsnd"),exit(-1);
//msgctl()
//Ctrl+C delete msq
printf("Press CTRL+C to delete msq\n");
if(SIG_ERR==signal(SIGINT,fa))
perror("signal"),exit(-1);
while(1);
return 0;
}