隊(duì)列最大容量字節(jié)數(shù) 16384
隊(duì)列最大隊(duì)列容量數(shù) 16
key_t ftok(char* path,int id)使用說明:
ftok創(chuàng)建一個(gè)鍵,是內(nèi)核中的隊(duì)列在外部的ID號(hào),由于消息隊(duì)列處于內(nèi)核中,只有創(chuàng)建者和內(nèi)核知道隊(duì)列在內(nèi)核里面的ID號(hào),這樣其它的進(jìn)程就無法知道內(nèi)核里面隊(duì)列ID號(hào),所以要關(guān)聯(lián)一個(gè)外部鍵進(jìn)行關(guān)聯(lián)
id (1-255)
返回內(nèi)核消息隊(duì)列的ID號(hào)
其它的注意就查看一下unix高級(jí)環(huán)境編程吧,或者有些問題需要討論就回我吧!!
server.c
#include "msg.h" #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char** argv) { int queid = open_msg("/root",100); while(1) { fputs("請(qǐng)輸入要發(fā)送的類型:1 or 2\\\\n", stdout); int type; scanf("%d",&type); switch(type) { case MYTYPE_ONE: { msg_send(queid,"MYTYPE_ONE", MYTYPE_ONE); break; } case MYTYPE_TWO: { msg_send(queid,"MYTYPE_TWO", MYTYPE_TWO); break; } default: { fputs("輸入類型錯(cuò)誤,請(qǐng)重新輸入\\\\n",stdout); break; } } fputs("輸入:q 為退出,其它表示繼續(xù)\\\\n",stdout); if(getchar() == \\\'q\\\') { fputs("退出成功!\\\\n",stdout); break; } else { fputs("繼續(xù)發(fā)送消息\\\\n",stdout); } } //不發(fā)送退出需要獎(jiǎng)隊(duì)列移除 del_que(queid); return 0; }
client.c
#include "msg.h" #include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char** argv) { int queid = open_msg("/root",100); while(1) { fputs("請(qǐng)接收要發(fā)送的類型:1 or 2\\\\n", stdout); int type; scanf("%d",&type); switch(type) { case MYTYPE_ONE: { msg_rec(queid,MYTYPE_ONE); break; } case MYTYPE_TWO: { msg_rec(queid,MYTYPE_TWO); break; } default: { fputs("輸入類型錯(cuò)誤,請(qǐng)重新輸入\\\\n",stdout); break; } } fputs("輸入:q 為退出,其它表示繼續(xù)\\\\n",stdout); if(getchar() == \\\'q\\\') { fputs("退出成功!\\\\n",stdout); break; } else { fputs("繼續(xù)發(fā)送消息\\\\n",stdout); } } //隊(duì)列移除 del_que(queid); return 0; }
msg.c
#include <sys/types.h> #include <sys/ipc.h> #include <stdio.h> #include <stdlib.h> #include <sys/ipc.h> #include <sys/msg.h> #include<string.h> #include"msg.h" //如果存在隊(duì)列則打開,沒有則創(chuàng)建 int open_msg(char* path, int id) { //獲取IPC對(duì)象的一個(gè)鍵 key_t key = ftok(path, id); if(-1 == key) { perror("ftok\\\\n"); exit(1); } //創(chuàng)建一個(gè)隊(duì)列 int queid = msgget(key, IPC_CREAT|0666); if(-1 == queid) { perror("msgget\\\\n"); exit(1); } return queid; } //發(fā)送消息到隊(duì)列 void msg_send(key_t key,char* text, long msgtype) { //初始化內(nèi)容 struct MSG tmp; memset(&tmp,sizeof(struct MSG),0); tmp.mytype = msgtype; strcpy(tmp.mytext,text); //發(fā)送消息 if(msgsnd(key, &tmp, TEXTSIZE, 0)) { perror("msgsnd\\\\n"); exit(1); } } //從消息隊(duì)列獲取消息并顯示 void msg_rec(key_t key,long msgtype) { struct MSG tmp; if(-1 == msgrcv(key, &tmp, TEXTSIZE, msgtype, MSG_NOERROR)) { perror("msgrcv\\\\n"); exit(1); } printf("receive content: %s\\\\n",tmp.mytext); } //刪除隊(duì)列,即使隊(duì)列里面還有消息也一起刪除 void del_que(key_t key) { if(msgctl(key,IPC_RMID,NULL)) { perror("msgsnd\\\\n"); exit(1); } }
msg.h
#ifndef MSG_H #define MSG_H #include <sys/types.h> #define TEXTSIZE 100 #define ARRYSIZE 2 #define MYTYPE_ONE 1 #define MYTYPE_TWO 2 struct MSG { long mytype; char mytext[TEXTSIZE]; }; int open_msg(char*,int); void msg_send(key_t,char*,long); #endif // end MSG_H
附件:http://down.51cto.com/data/2362206
更多關(guān)于云服務(wù)器,域名注冊(cè),虛擬主機(jī)的問題,請(qǐng)?jiān)L問三五互聯(lián)官網(wǎng):m.shinetop.cn