linux訊息序列(程序間通訊)

2021-12-29 16:24:31 字數 1755 閱讀 8127

linux訊息序列(程序間通訊)

[cpp] 

#include  

#include  

#include  

#include  

#include  

#include  

#include  

#include  

www.2cto.com  

//需要自己定義的訊息佇列結構  

struct msgstuct  

long int msgtype;  

char strmsg[1024];  

int  

main ()  

struct msgstuct msg_data;  

int msgid;  

char buffer[1024];  

//建立乙個訊息佇列  

if ((msgid= msgget ((key_t) 2234, 0666 | ipc_creat)) == -1)  

www.2cto.com  

[cpp] 

#include  

#include  

#include  

#include  

#include  

#include  

#include  

#include  

//需要自己定義的訊息佇列結構  

struct msgstuct  

long int msgtype;  

char strmsg[1024];  

int  

main ()  

{    www.2cto.com  

int msgid;  

struct msgstuct msg_data;  

//接收訊息優先順序  

long int msgpriority = 0;//從佇列中取第乙個  

//建立乙個訊息佇列  

if ((msgid= msgget ((key_t) 2234, 0666 | ipc_creat)) == -1)//類似open()建立乙個檔案返回它的檔案描述符,這裡是訊息序列  

perror ("msgget failed with error");  

exit (exit_failure);  

while (1)  

//接收訊息  

if (msgrcv (msgid, (void *) &msg_data, 1024,  

msgpriority, 0) == -1)  

perror ("msgrcv failed with error");  

exit (exit_failure);  

printf ("received message: %s", msg_data.strmsg);  

if (strncmp (msg_data.strmsg, "end", 3) == 0)  

{    www.2cto.com  

break;  

//刪除訊息佇列  

if (msgctl (msgid, ipc_rmid, 0) == -1)  

fprintf (stderr, "delete messagequeue error\n");  

exit (exit_failure);  

exit (exit_success);  

第乙個是send.c,第二個是recieve.c

linux 訊息序列(程序間通訊)

include include include include include include include include 需要自己定義的訊息佇列結構 struct msgstuct int main while 1 if strncmp buffer,end 3 0 exit exit suc...

linux 訊息序列(程序間通訊)

分類 文章 2012 09 13 00 00 108人閱讀收藏 舉報 cpp view plain copy print?include include include include include include include include 需要自己定義的訊息佇列結構 struct msgs...

Linux程序間通訊 訊息佇列

linux和類linux系統下程序間通訊 inter process communication,ipc 有很多種方式,包括套接字 socket 共享記憶體 shared memory 管道 pipe 訊息佇列 message queue 等,各自有各自的一些應用場景和用途,這次就來聊一聊訊息佇列這...