From 1243ecf308a35e81f98d1e519602b5ddf3cab494 Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Sat, 08 Dec 2018 01:01:08 +0800
Subject: [PATCH] add IPC Message Queue sample code
---
ch6_ipc/semaphore.c | 3
ch6_ipc/msgqueue_sender.c | 63 +++++++++++++++++++++
ch6_ipc/msgqueue_recver.c | 61 ++++++++++++++++++++
3 files changed, 125 insertions(+), 2 deletions(-)
diff --git a/ch6_ipc/msgqueue_recver.c b/ch6_ipc/msgqueue_recver.c
new file mode 100644
index 0000000..04ebf6e
--- /dev/null
+++ b/ch6_ipc/msgqueue_recver.c
@@ -0,0 +1,61 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+
+#define FTOK_PATH "/dev/zero"
+#define FTOK_PROJID 0x22
+
+typedef struct s_msgbuf
+{
+ long mtype;
+ char mtext[512];
+} t_msgbuf;
+
+int main(int argc, char **argv)
+{
+ key_t key;
+ int msgid;
+ t_msgbuf msgbuf;
+ int msgtype;
+ int i;
+
+ if( (key=ftok(FTOK_PATH, FTOK_PROJID)) < 0 )
+ {
+ printf("ftok() get IPC token failure: %s\n", strerror(errno));
+ return -1;
+ }
+
+ msgid = msgget(key, IPC_CREAT|0666);
+ if( msgid < 0)
+ {
+ printf("shmget() create shared memroy failure: %s\n", strerror(errno));
+ return -2;
+ }
+
+ msgtype = msgtype = (int)key;
+ printf("key[%d] msgid[%d] msgypte[%d]\n", (int)key, msgid, msgtype);
+
+ for(i=0; i<4; i++)
+ {
+ memset(&msgbuf,0,sizeof(msgbuf));
+ if( msgrcv(msgid, &msgbuf, sizeof(msgbuf.mtext), msgtype, IPC_NOWAIT) < 0 )
+ {
+ printf("msgsnd() receive message failure: %s\n", strerror(errno));
+ break;
+ }
+
+ printf("Receive Message: %s\n", msgbuf.mtext);
+ sleep(1);
+ }
+
+ msgctl(msgid, IPC_RMID, NULL);
+
+ return 0;
+}
+
diff --git a/ch6_ipc/msgqueue_sender.c b/ch6_ipc/msgqueue_sender.c
new file mode 100644
index 0000000..9e962b8
--- /dev/null
+++ b/ch6_ipc/msgqueue_sender.c
@@ -0,0 +1,63 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <sys/types.h>
+#include <sys/ipc.h>
+#include <sys/msg.h>
+
+#define FTOK_PATH "/dev/zero"
+#define FTOK_PROJID 0x22
+
+typedef struct s_msgbuf
+{
+ long mtype;
+ char mtext[512];
+} t_msgbuf;
+
+int main(int argc, char **argv)
+{
+ key_t key;
+ int msgid;
+ t_msgbuf msgbuf;
+ int msgtype;
+ int i;
+
+ if( (key=ftok(FTOK_PATH, FTOK_PROJID)) < 0 )
+ {
+ printf("ftok() get IPC token failure: %s\n", strerror(errno));
+ return -1;
+ }
+
+ msgid = msgget(key, IPC_CREAT|0666);
+ if( msgid < 0)
+ {
+ printf("shmget() create shared memroy failure: %s\n", strerror(errno));
+ return -2;
+ }
+
+ msgtype = (int)key;
+ printf("key[%d] msgid[%d] msgypte[%d]\n", (int)key, msgid, msgtype);
+
+ for(i=0; i<4; i++)
+ {
+ msgbuf.mtype = msgtype;
+ strcpy(msgbuf.mtext,"Ping");
+
+ if( msgsnd(msgid, &msgbuf, sizeof(msgbuf.mtext), IPC_NOWAIT) < 0)
+ {
+ printf("msgsnd() send message failure: %s\n", strerror(errno));
+ break;
+ }
+ printf("Send message: %s\n", msgbuf.mtext);
+
+ sleep(1);
+ }
+
+ msgctl(msgid, IPC_RMID, NULL);
+
+ return 0;
+}
+
diff --git a/ch6_ipc/semaphore.c b/ch6_ipc/semaphore.c
index 6f5e6df..b64987f 100644
--- a/ch6_ipc/semaphore.c
+++ b/ch6_ipc/semaphore.c
@@ -57,8 +57,7 @@
semaphore_p(semid);
printf("Parent start do something now...\n");
sleep(2);
- printf("Child process exit and ");
-
+ printf("Perent process destroy semaphore and exit\n");
semaphore_term(semid);
return 0;
--
Gitblit v1.9.1