From 6632fe9c973b5281a359e533a7820961176057fd Mon Sep 17 00:00:00 2001
From: Guo Wenxue <guowenxue@gmail.com>
Date: Fri, 07 Dec 2018 23:42:35 +0800
Subject: [PATCH] Add IPC shared memory sample code

---
 ch6_ipc/shared_mem_write.c |   63 +++++++++++++++++++++++++++++++
 ch6_ipc/shared_mem_read.c  |   57 ++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+), 0 deletions(-)

diff --git a/ch6_ipc/shared_mem_read.c b/ch6_ipc/shared_mem_read.c
new file mode 100644
index 0000000..74a4ce5
--- /dev/null
+++ b/ch6_ipc/shared_mem_read.c
@@ -0,0 +1,57 @@
+#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/shm.h>
+
+#define FTOK_PATH            "/dev/zero"
+#define FTOK_PROJID          0x22
+
+typedef struct st_student
+{
+    char            name[64];
+    int             age;
+} t_student;
+
+int main(int argc, char **argv)
+{
+    key_t          key;
+    int            shmid;
+    int            i;
+    t_student     *student;
+
+    if( (key=ftok(FTOK_PATH, FTOK_PROJID)) < 0 ) 
+    {   
+	printf("ftok() get IPC token failure: %s\n", strerror(errno)); 
+	return -1;
+    }
+
+    shmid = shmget(key, sizeof(t_student), IPC_CREAT|0666);
+    if( shmid < 0)
+    {
+	printf("shmget() create shared memroy failure: %s\n", strerror(errno));
+	return -2;
+    }
+
+    student = shmat(shmid, NULL, 0);
+    if( (void *)-1  == student )
+    {
+	printf("shmat() alloc shared memroy failure: %s\n", strerror(errno));
+	return -2;
+    }
+
+    for(i=0; i<4; i++)
+    {
+	printf("Student '%s' age [%d]\n", student->name, student->age);
+	sleep(1);
+    }
+
+    shmctl(shmid, IPC_RMID, NULL);
+
+    return 0;
+}
+
diff --git a/ch6_ipc/shared_mem_write.c b/ch6_ipc/shared_mem_write.c
new file mode 100644
index 0000000..f4692c4
--- /dev/null
+++ b/ch6_ipc/shared_mem_write.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/shm.h>
+
+#define FTOK_PATH            "/dev/zero"
+#define FTOK_PROJID          0x22
+
+typedef struct st_student
+{
+    char            name[64];
+    int             age;
+} t_student;
+
+int main(int argc, char **argv)
+{
+    key_t          key;
+    int            shmid;
+    int            i;
+    t_student     *student;
+
+    if( (key=ftok(FTOK_PATH, FTOK_PROJID)) < 0 ) 
+    {   
+	printf("ftok() get IPC token failure: %s\n", strerror(errno)); 
+	return -1;
+    }
+
+    shmid = shmget(key, sizeof(t_student), IPC_CREAT|0666);
+    if( shmid < 0)
+    {
+	printf("shmget() create shared memroy failure: %s\n", strerror(errno));
+	return -2;
+    }
+
+    student = shmat(shmid, NULL, 0);
+    if( (void *)-1  == student )
+    {
+	printf("shmat() alloc shared memroy failure: %s\n", strerror(errno));
+	return -2;
+    }
+
+    strncpy(student->name, "zhangsan", sizeof(student->name));
+    student->age = 18;
+
+    for(i=0; i<4; i++)
+    {
+	student->age ++;
+        printf("Student '%s' age [%d]\n", student->name, student->age);
+	sleep(1);
+    }
+
+    shmdt(student);
+
+    shmctl(shmid, IPC_RMID, NULL);
+
+    return 0;
+}
+

--
Gitblit v1.9.1