From 0bc4c76bd86246396c2b1e40df78e349f7296306 Mon Sep 17 00:00:00 2001 From: Jet Hughes Date: Wed, 12 Oct 2022 09:44:42 +1300 Subject: [PATCH] vault backup: 2022-10-12 09:44:42 --- .../notes/18-processes-sharing-and-threads.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/content/notes/18-processes-sharing-and-threads.md b/content/notes/18-processes-sharing-and-threads.md index 0f78899a7..160529eea 100644 --- a/content/notes/18-processes-sharing-and-threads.md +++ b/content/notes/18-processes-sharing-and-threads.md @@ -10,6 +10,30 @@ tags: ![linux memory space layout](https://i.imgur.com/vLlD03U.png) Can't assume all variables are initally zero. +OS decide which process maps who which physical address + +memory mapping `mmap` is a family of system calls: +- allow two processes to share some region of their memory space. +- achieved by creating a memory region that can be shared by other processes + +![map diagram](https://i.imgur.com/IDk8AcN.png) + +![mmap examples](https://i.imgur.com/zKhFpsm.png) + +shared memory by two processes mapping to the same file or using fork() after mmap() +``` c +random_fd = open("/home/hzy/test/zero", O_RDWR); +ptr = mmap(NULL, 10240, PROT_READ|PROT_WRITE, + MAP_SHARED | MAP_FILE, random_fd, 0); +``` + +private mapping is often ysed to set up new memory sections. +``` c +fd = open("mmap_test_file", O_RDWR); p +tr = mmap(NULL, 10240, PROT_READ|PROT_WRITE, + MAP_PRIVATE|MAP_FILE, fd, 0); +``` + # Threads Lightweight process - shares everything except the stack