日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

System V 消息队列

發布時間:2023/11/30 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 System V 消息队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、System V 消息隊列

有一個隊列,隊列存放各種消息。每個進程可以把數據封存在消息中,再放入隊列。每個進程都可以拿到消息隊列,再從中取出/放入消息。

消息隊列也有管道一樣的不足,就是每個消息的最大長度是有上限的(MSGMAX),每個消息隊列的總的字節數是有上限的(MSGMNB),系統上消息隊列的總數也有一個上限(MSGMNI)。

?

二、msgget函數

. msgget函數原型:用于創建一個新的消息隊列或訪問一個已存在的消息隊列

#include <sys/msg.h> int msgget(key_t key, int msgflg);

注意:

msgid = msgget(1234, 0); //按照原有權限打開該消息隊列

1. 測試代碼:

#include <unistd.h> #include <sys/msg.h> #include <stdlib.h> #include <stdio.h>int main() {int msgid;msgid = msgget(1234, 0666 | IPC_CREAT);if(msgid == -1){perror("msgget");exit(EXIT_FAILURE);}printf("msgget succ\n");printf("msgget = %d\n", msgid);return 0; }

輸出結果:


2. 測試代碼:

#include <unistd.h> #include <sys/msg.h> #include <stdlib.h> #include <stdio.h>int main() {int msgid;msgid = msgget(IPC_PRIVATE, 0666);if (msgid == -1){perror("msgget");exit(EXIT_FAILURE);}printf("msgget succ\n");printf("msgget = %d\n", msgid);return 0; }

?輸出結果:


3. 測試代碼:

#include <unistd.h> #include <sys/msg.h> #include <stdlib.h> #include <stdio.h>int main() {int msgid;msgid = msgget(1234, 0400 | IPC_CREAT);if (msgid == -1){perror("msgget");exit(EXIT_FAILURE);}printf("msgget succ\n");printf("msgget = %d\n", msgid);msgid = msgget(1234, 0600 | IPC_CREAT);if (msgid == -1){perror("msgget");exit(EXIT_FAILURE);} return 0; }

?輸出結果:

?

三、msgsnd函數?

#include <sys/msg.h> int msgctl(int msqid, int cmd, struct msqid_ds *buf); #include <unistd.h> #include <sys/msg.h> #include <stdlib.h> #include <stdio.h> #include <malloc.h>int main(int argc, const char *argv[]) {int msgid;msgid = msgget(1234, 0);if (msgid == -1){perror("msgget");exit(EXIT_FAILURE);}printf("msggget succ\n");printf("msgid = %d\n", msgget);struct msqid_ds buf;msgctl(msgid, IPC_STAT, &buf);printf("mode = %o\n", buf.msg_perm.mode);printf("bytes = %ld\n", buf.__msg_cbytes);printf("msg_qnum = %d\n", (int)buf.msg_qnum);printf("msg_qbytes = %d\n", (int)buf.msg_qbytes);return 0; }

?輸出結果:


#include <unistd.h> #include <sys/msg.h> #include <stdlib.h> #include <stdio.h> #include <malloc.h>int main(int argc, const char *argv[]) {int msgid;msgid = msgget(1234, 0);if (msgid == -1){perror("msgget");exit(EXIT_FAILURE);}printf("msggget succ\n");printf("msgid = %d\n", msgget);struct msqid_ds buf;sscanf("666", "%o", (unsigned int *)&buf.msg_perm.mode);msgctl(msgid, IPC_SET, &buf);return 0; }

?輸出結果:

四、msgsnd函數

msgsnd函數原型:把一條消息添加到消息隊列中

#include <sys/msg.h> int msgsnd(int msqid, const void *msgp, size_t msgsz, int msgflg);

參數:

  • msgid:有msgget函數返回的消息隊列標識碼
  • msgp:是一個指針,指針指向準備發送的消息
  • msgsz:是msgp指向的消息長度,這個長度不含保存消息類型的那個long int長整型
  • msgflg:控制著當前消息隊列滿或到達系統上限時將要發生的事情

1. 測試代碼:

程序1:

//msg_stat.c #include <unistd.h> #include <sys/msg.h> #include <stdlib.h> #include <stdio.h> #include <malloc.h>int main(int argc, const char *argv[]) {int msgid;msgid = msgget(1234, 0);if (msgid == -1){perror("msgget");exit(EXIT_FAILURE);}printf("msggget succ\n");printf("msgid = %d\n", msgget);struct msqid_ds buf;msgctl(msgid, IPC_STAT, &buf);printf("mode = %o\n", buf.msg_perm.mode);printf("bytes = %ld\n", buf.__msg_cbytes);printf("msg_qnum = %d\n", (int)buf.msg_qnum);printf("msg_qbytes = %d\n", (int)buf.msg_qbytes);return 0; }

程序2:

#include <unistd.h> #include <sys/msg.h> #include <stdlib.h> #include <stdio.h> #include <malloc.h>int main(int argc, char *argv[]) {if (argc != 3){fprintf(stderr, "Usage:%s<bytes> <type>\n", argv[0], argv[1]);exit(EXIT_FAILURE);}int len = atoi(argv[1]);int type = atoi(argv[2]);int msgid;msgid = msgget(1234, 0);if (msgid == -1){perrro("msgget");exit(EXIT_FAILURE);}struct msgbuf *ptr;ptr(struct msgbuf*) malloc(sizeof(long) + len);ptr->mtype = type;if (msgsnd(msgid, ptr, len, 0) < 0) //阻塞方式{perrro("msgsnd");exit(EXIT_FAILURE);}return 0; }

輸出結果:

?

總結

以上是生活随笔為你收集整理的System V 消息队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。