IPC objects IPC communication ------: 1, shared memory, 2, 3 message queues, semaphores.
2, the message queue (queue chain)
the msgget () function to create analysis:
the msgctl () release of analytic functions:
Example 1: Using the msgget () function to create a message queue, then the msgctl () to delete the message queue; Create creat_delete_massegeque.c, code:
#include"unistd.h"
#include"sys/types.h"
#include"sys/msg.h"
#include"signal.h"
#include"stdio.h"
#include"stdlib.h"
int main()
{
int msgid;
//创建或打开消息队列
msgid=msgget(IPC_PRIVATE,0777);
if(msgid<0)
{
printf("creat message queue failure\n");
return -1;
}
printf("create message queue success\n");
system("ipcs -q");//查看消息队列属性
//删除消息队列
msgctl(msgid,IPC_RMID,NULL);
system("ipcs -q");
return 0;
}
Compile the source file, and run ./mque, as shown in the results:
Visible, after the success of the message queue is created, and successfully been deleted.
a msgsnd () function writes analysis:
Example 2:: Example 1, the write data in the message queue is created based on the
created msgsnd.c file, as follows:
#include"unistd.h"
#include"sys/types.h"
#include"sys/msg.h"
#include"signal.h"
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
//消息节点
typedef struct MsgNode{
long type;
char voltage[124];
char ID[4];
}MsgNode;
int main()
{
int msgid;
MsgNode snd;
//创建或打开消息队列
msgid=msgget(IPC_PRIVATE,0777);
if(msgid<0)
{
printf("creat message queue failure\n");
return -1;
}
printf("create message queue success\n");
system("ipcs -q");//查看消息队列属性
//向消息队列写入数据
fgets(snd.voltage,124,stdin);//从标准输入写入
msgsnd(msgid,(void*)&snd.voltage,strlen(snd.voltage),0);
system("ipcs -q");
while(1);
//删除消息队列
msgctl(msgid,IPC_RMID,NULL);
system("ipcs -q");
return 0;
}
We write: helloeveryone welcome to daily dictation total of 41 characters. FIG results are as follows:
msgrcv read function analysis:
Example 3: Based on Example 2, the read data queue messages:
Create msgrcv.c file, as follows:
#include"unistd.h"
#include"sys/types.h"
#include"sys/msg.h"
#include"signal.h"
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
//消息节点
typedef struct MsgNode{
long type;
char voltage[124];
char ID[4];
}MsgNode;
int main()
{
int msgid;
int readret;//读返回的字符个数
MsgNode snd;//写对象
MsgNode red;//读对象
//创建或打开消息队列
msgid=msgget(IPC_PRIVATE,0777);
if(msgid<0)
{
printf("creat message queue failure\n");
return -1;
}
printf("create message queue success\n");
system("ipcs -q");//查看消息队列属性
//init snd
snd.type=100;
//向消息队列写入数据
printf("please input message:\n");
fgets(snd.voltage,124,stdin);//从标准输入写入
msgsnd(msgid,(void*)&snd,strlen(snd.voltage),0);
//system("ipcs -q");
//clear voltage buf before read
memset(red.voltage,0,124);
//从消息队列里读数据
readret=msgrcv(msgid,(void*)&red,124,100,0);
printf("the result read from message queue\n");
printf("red.voltage=%sreadret=%d\n",red.voltage,readret);
//删除消息队列
msgctl(msgid,IPC_RMID,NULL);
system("ipcs -q");
return 0;
}
After writing hello linux !, read as follows:
reading out hello linux !.
[Note] to read the message queue is equivalent to delete the list, write the equivalent of the insertion of the message queue, the queue read the news not in the.
Comprehensive Example: A message queue bidirectional communication relationship between two unrelated processes A and B; (transceiver multi-process: the main process development, the child process received).
Is used to reflect the differences and advantages of the [message queues and shared memory communication conduit] communication with
the main server of the write process, the read sub-process; main process of the client to read, write the child process.
Establish server.c, client.c, ac file, as shown:
server.c codes are as follows:
#include"unistd.h"
#include"sys/types.h"
#include"sys/msg.h"
#include"signal.h"
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
//message node
typedef struct MsgNode{
long type;
char voltage[124];
char ID[4];
}MsgNode;
int main()
{
int msgid;
int readret;//return number of string
MsgNode sndbuf;//write object
MsgNode redbuf;//read object
int key;
key=ftok("./a.c",'a');//creat key for message queue
if(key<0)
{
printf("creat key failure\n");
return -1;
}
printf("creat key success, key=%d\n",key);
//creat or open message queue
msgid=msgget(key,IPC_CREAT | 0777);
if(msgid<0)
{
printf("creat message queue failure\n");
return -2;
}
printf("create message queue success\n");
system("ipcs -q");//check attributes of massege queues
pid_t pid;
pid=fork();//fork child process pid
if(pid>0)
{
//init sndbuf type
sndbuf.type =100;
while(1)
{
//clear sndbuf before write
memset(sndbuf.voltage,0,124);
//send message to client
printf("please send message to client:\n");
fgets(sndbuf.voltage,124,stdin);//write message from stdin to sndbuf
msgsnd(msgid,(void*)&sndbuf,strlen(sndbuf.voltage),0);//write message to msgque
}
}
//child process code
if(pid==0)
{
while(1)
{
//clear redbuf before read
memset(redbuf.voltage,0,124);
//read message from messageque
readret=msgrcv(msgid,(void*)&redbuf,124,200,0);
printf("\ncontent of message queue:\n");
printf("type = %ld\n",redbuf.type);
printf("red.voltage=%sreadret=%d\n",redbuf.voltage,readret);
}
}
//delete or free message queue
msgctl(msgid,IPC_RMID,NULL);
system("ipcs -q");
return 0;
}
client.c codes are as follows:
#include"unistd.h"
#include"sys/types.h"
#include"sys/msg.h"
#include"signal.h"
#include"stdio.h"
#include"stdlib.h"
#include"string.h"
//message node
typedef struct MsgNode{
long type;
char voltage[124];
char ID[4];
}MsgNode;
int main()
{
int msgid;
int readret;//return number of message
MsgNode sndbuf;//writed object
MsgNode redbuf;//read object
int key;
key=ftok("./a.c",'a');//creat key
if(key<0)
{
printf("creat key failure\n");
return -1;
}
printf("creat key success, key=%d\n",key);
//creat or open message queue
msgid=msgget(key,IPC_CREAT | 0777);
if(msgid<0)
{
printf("creat message queue failure\n");
return -2;
}
printf("create message queue success\n");
system("ipcs -q");//check message attributes
pid_t pid;
pid=fork();//creat child process pid
//parent process code
if(pid>0)
{
while(1)
{
//clear redbuf before read message
memset(redbuf.voltage,0,124);
//read message from msgque
readret=msgrcv(msgid,(void*)&redbuf,124,100,0);
printf("message queue are:\n");
printf("red.voltage=%sreadret=%d\n",redbuf.voltage,readret);
}
}
if(pid==0)
{
//init type
sndbuf.type=200;
while(1)
{
//clear sndbuf before write
memset(sndbuf.voltage,0,124);
printf("please send message to server\n");
fgets(sndbuf.voltage,124,stdin);//write message from stdin
msgsnd(msgid,(void*)&sndbuf,strlen(sndbuf.voltage),0);//write to message queue
}
}
//delete message queue
msgctl(msgid,IPC_RMID,NULL);
system("ipcs -q");
return 0;
}
Above is a summary of the message queue communication.