linux訊息佇列相關操作

來源:互聯網
上載者:User

標籤:io   ar   sp   on   c   amp   linux   ef   size   

/* 發送訊息佇列 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct mymsg
{
long mtype; /* message type, must be > 0 */
char mtext[32];
};


#define KEY (key_t)0x1fff
int
main ( int argc, char *argv[] )
{
key_t key;
int msgid;
int res;
key = KEY;
struct mymsg msg;

printf ("發送給解析進程......\n");
strcpy (msg.mtext, "haha");

/* 建立訊息佇列 */
msgid = msgget (key, 0666 | IPC_CREAT);
if (msgid == -1)
{
perror ("msgget");
}
msg.mtype = 1;

res = msgsnd (msgid, &msg, sizeof(struct mymsg), 0);
printf ("fa ok msg.mtext = %s\n", msg.mtext);

return 0;
}

 

 ========================================================

 

/* 接收訊息佇列 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

struct mymsg
{
long mtype;/* message type, must be > 0 */
char mtext[32]; /* message data */
};

#define KEY (key_t)0x1fff
int main ( int argc, char *argv[] )
{
key_t key;
int msgid;
int res;
key = KEY;

struct mymsg msg;
/* 建立訊息佇列 */
msgid = msgget (key, 0666 | IPC_CREAT);
if (msgid == -1)
{
perror ("msgget");
}
msg.mtype = 1;

/* libnids抓包資訊 */
int i=1;
while(i--)
{
res = msgrcv (msgid, &msg, sizeof(struct mymsg), 0, 0);
printf ("shou ok, msg.mtext = %s\n", msg.mtext);
}

return 0;
}

 ==========================================================

/*刪除訊息佇列*/

#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>

/* 訊息佇列通訊的key */
#define KEY (key_t)0x1fff

int rmqueue(key_t key)
{
int msgid, res;

msgid = msgget (key, 0);
res = msgctl(msgid, IPC_RMID, 0);
return 0;
}

int main ( int argc, char *argv[] )
{
key_t key;

key = KEY;
rmqueue(key);

return 0;
}

 ==========================================================

/* 擷取訊息佇列的狀態資訊 */

#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/msg.h>

#define NIDS_PRO_KEY (key_t)0x1fff

int main()
{
key_t key;
key = NIDS_PRO_KEY;
int id = msgget(key,0);
if (id == -1) perror("shmget"),exit(-1);
struct msqid_ds ds;
msgctl(id,IPC_STAT,&ds);
printf("key=%x\n",ds.msg_perm.__key);
printf("mode=%o\n",ds.msg_perm.mode);
printf("current total size=%d\n",ds.__msg_cbytes);
printf("current msg count=%d\n",ds.msg_qnum);
printf("system max allow msg size=%d\n",ds.msg_qbytes);
}

 

linux訊息佇列相關操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.