Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice10 / ex2-1.c
diff options
context:
space:
mode:
1 files changed, 21 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practice10/ex2-1.c21
diff --git a/Year 3/System programming/Practice10/ex2-1.c b/Year 3/System programming/Practice10/ex2-1.c
new file mode 100644
index 0000000..db839c1
--- /dev/null
+++ b/Year 3/System programming/Practice10/ex2-1.c
@@ -0,0 +1,21 @@
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int main() {
+ const char msg[] = "hello world";
+ size_t msglen = strlen(msg);
+
+ int shmd = shm_open("/shm", O_RDWR | O_CREAT | O_TRUNC, 0755);
+ ftruncate(shmd, msglen);
+ char* memory = mmap(NULL, msglen, PROT_WRITE, MAP_SHARED, shmd, 0);
+
+ strncpy(memory, msg, msglen);
+
+ munmap(memory, msglen);
+ close(shmd);
+ return 0;
+}