Basic Linux MSG queue operations

Source: Internet
Author: User
# Include <sys/types. h> # include <sys/MSG. h> # include <unistd. h> # include <string. h> # include <stdio. h> void msg_stat (INT, struct msqid_ds); int main () {int gflags, sflags, rflags; key_t key; int msgid; int reval; struct msgsbuf {int mtype; char mtext [1];} msg_sbuf; struct msgmbuf {int mtype; char mtext [10];} msg_rbuf; struct msqid_ds msg_ginfo, msg_sinfo; char * msgpath = "/Unix/msgqueue"; Key = ftok (msgpath, 'a '); Gflags = ipc_creat | ipc_excl; msgid = msgget (Key, gflags | 00666); If (msgid =-1) {printf ("MSG create error \ n "); return;}/* after creating a message queue, the default attribute of the Output Message Queue */msg_stat (msgid, msg_ginfo); sflags = ipc_nowait; msg_sbuf.mtype = 10; msg_sbuf.mtext [0] = 'a'; reval = msgsnd (msgid, & msg_sbuf, sizeof (msg_sbuf.mtext), sflags); If (reval =-1) {printf ("Message send error \ n");}/* Output Message Queue attributes */msg_stat (msgid, msg_ginfo) after a message is sent; rflags = IP C_nowait | msg_noerror; reval = msgrcv (msgid, & msg_rbuf, 4,10, rflags); If (reval =-1) printf ("read MSG error \ n "); else printf ("read from MSG queue % d bytes \ n", Reval);/* output the Message Queue attribute */msg_stat (msgid, msg_ginfo); msg_sinfo.msg_perm.uid = 8;/* Just a try msg_sinfo.msg_perm.gid = 8;/* msg_sinfo.msg_qbytes = 16388; /* Verify that the superuser can change the default message queue msg_qbytes * // * Note that the value set here is greater than the default value */reval = msgctl (msgid, ipc_set, & msg_sinfo); If (Reval =-1) {printf ("MSG set info error \ n"); return;} msg_stat (msgid, msg_ginfo ); /* Verify the setting of Message Queue attributes */reval = msgctl (msgid, ipc_rmid, null);/* Delete message queue */If (reval =-1) {printf ("unlink MSG queue error \ n"); Return ;}} void msg_stat (INT msgid, struct msqid_ds msg_info) {int reval; sleep (1 ); /* only for the convenience of subsequent output time */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 % d \ n ", msg_info.msg_cbytes); printf (" number of messages in queue is % d \ n ", msg_info.msg_qnum); printf ("Max number of bytes on queue is % d \ n", msg_info.msg_qbytes);/* the capacity (in bytes) of each message queue is limited to msgmnb, the value size varies with the system. When creating a new message queue, the default value of msg_qbytes is msgmnb */printf ("PID of last msgsnd is % d \ n", msg_info.msg_lspid ); printf ("PID of last msgrcv is % d \ n", msg_info.msg_lrpid); printf ("Last msgsnd time is % s", ctime (& (msg_info.msg_stime ))); printf ("Last msgrcv time is % s", ctime (& (msg_info.msg_rtime); printf ("last change time is % s", ctime (& (msg_info.msg_ctime ))); printf ("MSG uid is % d \ n", msg_info.msg_perm.uid); printf ("msg gid is % d \ n", msg_info.msg_perm.gid );}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.