structmsg_queue { structipc_permq_perm; time_t q_stime; // last msgsnd time time_t q_rtime; // last msgrcv time time_t q_ctime; // last change time unsignedlong q_cbytes; // current number of bytes on queue unsignedlong q_qnum; // number of message in queue unsignedlong q_qbytes; // max number of bytes on queue pid_t q_lspid; // pid of last msgsnd pid_t q_lrpid; // last receive pid structlist_headq_messages; structlist_headq_receives; structlist_headq_senders; };
// 摘自所用ubuntu18.04电脑中的/usr/include/i386-linux-gnu/bits/msq.h structmsqid_ds { structipc_permmsg_perm;/* structure describing operation permission */ __time_t msg_stime; /* time of last msgsnd command */ #ifndef __x86_64__ unsignedlongint __glibc_reserved1; #endif __time_t msg_rtime; /* time of last msgrcv command */ #ifndef __x86_64__ unsignedlongint __glibc_reserved2; #endif __time_t msg_ctime; /* time of last change */ #ifndef __x86_64__ unsignedlongint __glibc_reserved3; #endif __syscall_ulong_t __msg_cbytes; /* current number of bytes on queue */ msgqnum_t msg_qnum; /* number of messages currently on queue */ msglen_t msg_qbytes; /* max number of bytes allowed on queue */ __pid_t msg_lspid; /* pid of last msgsnd() */ __pid_t msg_lrpid; /* pid of last msgrcv() */ __syscall_ulong_t __glibc_reserved4; __syscall_ulong_t __glibc_reserved5; };
voidmsg_stat(int msgid, struct msqid_ds msg_info) { int reval; sleep(1); // get message property reval = msgctl(msgid, IPC_STAT, &msg_info); if(reval == -1) { printf("get msg info error\n"); return; }
printf("\n"); printf("current number of bytes on queue is %ld\n", msg_info.msg_cbytes); printf("number of message in queue is %ld\n", msg_info.msg_qnum); printf("max number of bytes on queue is %ld\n", msg_info.msg_qbytes); // printf("pid of last msgsnd is %d\n", msg_info.msg_lspid); // last send opera te process's ID printf("pid of last msgrcv is %d\n", msg_info.msg_lrpid); // last receive op erate process's ID printf("last msgsnd time is %s", ctime(&(msg_info.msg_stime))); // last send time printf("last msgrcv time is %s", ctime(&(msg_info.msg_rtime))); // last rece ive time printf("last change time is %s", ctime(&(msg_info.msg_ctime))); // last chan ge time printf("msg uid is %d\n", msg_info.msg_perm.uid); // message queue user ID printf("msg gid is %d\n", msg_info.msg_perm.gid); // message queue group ID }
$ sudo ./msg_app [sudo] password for deeplearning: key = 0x1e11060e msg create ok
msg_stat1: current number of bytes on queue is 0 number of message in queue is 0 max number of bytes on queue is 16384 pid of last msgsnd is 0 pid of last msgrcv is 0 last msgsnd time is Thu Jan 1 08:00:00 1970 last msgrcv time is Thu Jan 1 08:00:00 1970 last change time is Wed Nov 27 10:40:41 2019 msg uid is 0 msg gid is 0
message send ok
msg_stat2: current number of bytes on queue is 10 number of message in queue is 1 max number of bytes on queue is 16384 pid of last msgsnd is 3912 pid of last msgrcv is 0 last msgsnd time is Wed Nov 27 10:40:42 2019 last msgrcv time is Thu Jan 1 08:00:00 1970 last change time is Wed Nov 27 10:40:41 2019 msg uid is 0 msg gid is 0
read from msg queue 3 bytes type:8, message:abc
msg_stat3: current number of bytes on queue is 0 number of message in queue is 0 max number of bytes on queue is 16384 pid of last msgsnd is 3912 pid of last msgrcv is 3912 last msgsnd time is Wed Nov 27 10:40:42 2019 last msgrcv time is Wed Nov 27 10:40:43 2019 last change time is Wed Nov 27 10:40:41 2019 msg uid is 0 msg gid is 0
msg set ok
msg_stat4: current number of bytes on queue is 0 number of message in queue is 0 max number of bytes on queue is 16388 pid of last msgsnd is 3912 pid of last msgrcv is 3912 last msgsnd time is Wed Nov 27 10:40:42 2019 last msgrcv time is Wed Nov 27 10:40:43 2019 last change time is Wed Nov 27 10:40:44 2019 msg uid is 8 msg gid is 8