#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <unistd.h>
#define BUFF_SIZE 1024
#define MSG_KEY "/etc/passwd"
typedef struct msgbuf
{
long mtype;
char mtext[BUFF_SIZE];
}msgbuf_def;
int main()
{
int res = 0;
int read_nm = 0;
int msg_len = 0;
int msgid = 0;
char buff[BUFF_SIZE];
key_t key;
msgbuf_def msg_buff;
key = ftok(MSG_KEY, 'z');
if (key < 0)
{
printf("ftok exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), key);
return -1;
}
msgid = msgget(key, IPC_CREAT|0777);
if (msgid == -1)
{
printf("msgget exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), res);
return -1;
}
while(1)
{
memset(buff, 0x00, sizeof(buff));
read_nm=read(0, buff, sizeof(buff)-1);
if (read_nm == -1)
{
printf("msgget exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), read_nm);
return -1;
}
if (strncmp(buff, "exit", 4) == 0)
{
printf("process exit");
return 0;
}
memset(&msg_buff, 0x00, sizeof(msg_buff));
msg_buff.mtype = 2;
memcpy(msg_buff.mtext, buff, sizeof(msg_buff.mtext)-1);
msg_len = sizeof(msg_buff)-sizeof(msg_buff.mtype);
printf("msg_buff.mtext=%s, msg_buff_len=[%d]\n", msg_buff.mtext, msg_len);
res = msgsnd(msgid, &msg_buff, msg_len, 0);
if (res != 0)
{
printf("msgsnd exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), res);
return -1;
}
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#define BUFF_SIZE 1024
#define MSG_KEY "/etc/passwd"
typedef struct msgbuf
{
long mtype;
char mtext[BUFF_SIZE];
}msgbuf_def;
int main()
{
int res = 0;
int read_nm = 0;
int msg_len = 0;
int msgid = 0;
key_t key;
msgbuf_def msg_buff;
key = ftok(MSG_KEY, 'z');
if (key == -1)
{
printf("ftok exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), key);
return -1;
}
msgid = msgget(key, IPC_CREAT|0777);
if (msgid == -1)
{
printf("msgget exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), res);
return -1;
}
while(1)
{
memset(&msg_buff, 0x00, sizeof(msg_buff));
res = msgrcv(msgid, &msg_buff, BUFF_SIZE, 0, MSG_NOERROR);
if (res == -1)
{
printf("msgrcv exec failed, errno=%d, errmsg=%s, res=%d\n", errno, strerror(errno), res);
return -1;
}
printf("rcv=%s\n", msg_buff.mtext);
}
return 0;
}