Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice10 / ex1.c
diff options
context:
space:
mode:
Commit609b7864d38fddb96617473fbc6aea16c64e8285 (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-01-20 15:54:05 +0200
Committer
 Syndamia <kamen@syndamia.com>
2024-01-20 15:54:05 +0200
Files8f203b247508a774b4cbde63da224a084ada8538 /Year 3/System programming/Practice10/ex1.c
uni-projects-609b7864d38fddb96617473fbc6aea16c64e8285.tar
uni-projects-609b7864d38fddb96617473fbc6aea16c64e8285.tar.gz
uni-projects-609b7864d38fddb96617473fbc6aea16c64e8285.zip
Parent92478827872214a442c96891a607d0781cfa6dd8 (diff)
[SP/P10] Solved exercises
1 files changed, 25 insertions, 0 deletions
-rw-r--r--Year 3/System programming/Practice10/ex1.c25
diff --git a/Year 3/System programming/Practice10/ex1.c b/Year 3/System programming/Practice10/ex1.c
new file mode 100644
index 0000000..c04f7dd
--- /dev/null
+++ b/Year 3/System programming/Practice10/ex1.c
@@ -0,0 +1,25 @@
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int main() {
+ char msg[] = "hello world";
+
+ int shmd = shm_open("/shmemory", O_RDWR | O_CREAT | O_TRUNC, 0755);
+ ftruncate(shmd, strlen(msg));
+ char* mem = mmap(NULL, strlen(msg) + 1, PROT_READ | PROT_WRITE, MAP_SHARED, shmd, 0);
+
+ strncpy(mem, msg, strlen(msg) + 1);
+
+ char buf[1024];
+ strncpy(buf, mem, strlen(msg) + 1);
+ write(1, buf, strlen(buf) + 1);
+
+ close(shmd);
+ shm_unlink("/shmemory");
+
+ return 0;
+}