struct{ uid_t uid; //owner's effective user id gid_t gif; //owner's effective group id uid_t cuid; //creator's effective user id git_t cgid; //creator's effective group id mode_t mode;//access mode //...... };
structmsgid_ds{ structipc_perm msg_perm; msgqnum_t msg_qnum;// num of messages on queue msglen_t msg_qbtes;//max num of bytes on queue pid_t msg_lspid;//pid of last msgsnd() pit_t msg_lrpid;//pid of last msgrcv() time_t msg_stime;//last-msgsnd() time time_t msg_rtime;//last-msgrcv() time time_t msg_ctime;// //...... }
以上结构定义了队列的当前状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include<sys/msg.h> intmsgget(key_t key, int flag); //用于创建消息队列,成功返回消息队列的ID,出错返回-1 intmsgctl(int msqid, int cmd, struct msqid_ds *buf); //成功返回0,出错返回-1 //cmd参数可以为IPC_STAT用于取数据,IPC_SET用于设置,IPC_RMID用于删除,只有root或者有效用户等于创建者可以成功删除 intmsgsnd(int msqid, constvoid *ptr, size_t nbytes, int flag); //成功返回0,出错返回-1 //flag可以设置IPC_NOWAIT进行不阻塞的操作 //自定义的*ptr结构如下 structmymesg{ long mtype; //message type, must be positive char mtxet[512];//content, of length nbytes } ssize_tmsgrcv(int msqid, void *ptr, size_t nbytes, long type, int flag); //成功返回消息数据那部分的长度,出错返回-1 //type可以指定想要那个消息,为0返回第一个,大于0返回队里消息类型为type的第一个消息,小于0返回绝对值小于type绝对值的消息,如果有若干个,先返回类型之最小的那个