2023.11.16 TIL
Code, Image source: The Linux Programming Interface, Michael Kerrisk
tlpi-dist/mmap/t_mmap.c
$ dd if=/def/zero of=s.txt bs=1 count=1024
→ file copy$ ./t_mmap s.txt hello
$ ./t_mmap s.txt goodbye
$ od -c -w8 s.txt
#include <sys/mman.h>
void *mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
flags
: Private / Sharedaddr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
if(addr == MAP_FAILED) errExit("mmap");
tlpi-dist/mmap/mmcat.c
#include <sys/mman.h>
int munmap(void *addr, size_t length);
#include <sys/mman.h>
int mprotect(void *addr, size_t length, int prot);
tlpi-dist/vmem/t_mprotect.c
#include <sys/mman.h>
int mlock(void *addr, size_t length);
int munlock(void *addr, size_t length);
tlpi-dist/vmem/memlock.c
#include <sys/mount.h>
int mount(const char *source, const char *target, const char *fstype, unsigned long mountflags, const void *data);
#include <sys/inotify.h>
int inotify_init(void); // return fd on success, -1 on error
int inotify_add_watch(int fd, const char *pathname, uint32_t mask); // return watch descriptor on success, -1 on error
tlpi-dist/inotify/demo_inotify.c