C語言訊息佇列

來源:互聯網
上載者:User

標籤:struct   技術   .com   ipcs   har   pcs   color   png   sgid   

msgsnd.c檔案

 1 #include<sys/types.h> 2 #include<sys/ipc.h> 3 #include<sys/msg.h> 4 #include<stdio.h> 5  6 struct msgbuf 7 { 8     long type;//類型           9     char buf[1024];10 };11 12 int main()13 {14     int msgid;15     msgid=msgget(0x1000,IPC_CREAT | 0777);16     struct msgbuf mb={1,"hello world"};17     int ret;18     ret=msgsnd(msgid,&mb,sizeof(struct msgbuf)-sizeof(long),0);19     //這裡的長度不包括類型的大小20 }

msgrcv.c檔案

 1 #include<sys/types.h> 2 #include<sys/ipc.h> 3 #include<sys/msg.h> 4 #include<stdio.h> 5 struct msgbuf 6 { 7     long type; 8     char buf[1024]; 9 };10 int main()11 {12     int msgid;13     msgid=msgget(0x1000,IPC_CREAT | 0777);14     struct msgbuf mb;15     msgrcv(msgid,&mb,sizeof(struct msgbuf)-sizeof(long),1,0);16     puts(mb.buf);17 }

gcc msgsnd.c -o msgsnd      gcc msgrcv.c -o msgrcv 

ipcs查看訊息佇列

./msgsnd   然後 ipcs

已經傳入,然後./msgrcv

ipcs查看一下

取出資料

C語言訊息佇列

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.