程序間通訊 訊息佇列(msg 實現自由通訊

2021-08-01 09:59:51 字數 1616 閱讀 5640

//msg(訊息佇列)--是將訊息按佇列的方式組織成的鍊錶

#include"utili.h"

#include

#include

#include

#include

#include

#include

#include

#include

//............ser.c

#include"utili.h"

#define send_type 100

#define recv_type 200

typedef struct msgtype

msgtype;

int main()

int msg_id = msgget(msg_key, ipc_creat|0755);

if(msg_id == -1)

msgtype msg;

pid_t pid1,pid2;

pid1=fork();

int status;

if(pid1 == -1)

else

if(pid1 == 0)

}else

if(pid1 > 0)

if(pid2 == 0)

msg.msg_type = send_type;

msgsnd(msg_id, &msg, strlen(msg.msg_buf)+1, 0);}}

else

if(pid2>0)

}/* ---只能實現你一句我一句

msgtype msg;

while(1)

*/return0;}

#..............cli.c

#include"utili.h"

#define send_type 200

#define recv_type 100

/*int

msgsnd(int msqid,const void *ptr,size_t length,int flag);

id號 傳送訊息的指標 長度 訪問標記

其中ptr是乙個結構指標,模板如下:

*/typedef struct msgtype

msgtype;

int main()

int msg_id = msgget(msg_key, 0);

if(msg_id == -1)

msgtype msg;

pid_t pid1,pid2;

pid1=fork();

int status;

if(pid1 == -1)

else

if(pid1 == 0)

}else

if(pid1 >0)

else

if(pid2 == 0)

msg.msg_type = send_type;

msgsnd(msg_id, &msg, strlen(msg.msg_buf)+1, 0);}}

else

if(pid2>0)

}/* while(1)

*/return

0; }

Linux 程序間通訊(1)訊息佇列 msg

2.訊息佇列介面 msgget 和 ftok 3.訊息佇列介面 msgctl 4.訊息佇列介面 msgsnd 5.訊息佇列介面 msgrcv 6.訊息佇列兩個命令 ipcs 參考 其實,就是程序間通訊。linux中的訊息佇列是程序間通訊的一種方式 通過建立乙個訊息佇列可以完成乙個或者多個程序的資訊交...

練習 LINUX程序間通訊之訊息佇列MSG

繼續堅持,或許不能深刻理解,但至少要保證有印象。訊息佇列 也叫做報文佇列 能夠克服早期unix通訊機制的一些缺點。作為早期unix通訊機制之一的訊號能夠傳送的資訊量有限,後來雖然posix 1003.1b在訊號的實時性方面作了拓廣,使得訊號在傳遞資訊量方面有了相當程度的改進,但是訊號這種通訊方式更像...

程序間訊息佇列通訊

要保證server能夠接收client的訊息,就必須保證server的生成的msg的識別符號是一樣的,也就是兩個用的key是必須一樣的。msglucy.c include include include include include include include include include ...