Reputation: 381
Requirement: kernel and userspace share huge memory through mmap. userspace is producer and kernel is consumer.
I have written a kernel module which creates a device and implements the mmap syscall and allocates kernel buffer. Userspace opens the device and calls mmap and writes something. Kernel reads it through timer thread every 5 sec and it works fine.
Issue is how do I synchronize this mmap'ed area.
Solutions I thought of:
1) After writing to mmap area , userspace sends netlink notification which marks global flag saying data is ready and kernel is ready to read now.
2) After writing to mmap area , userspace issues ioctl which marks global flag saying data is ready and kernel is ready to read now.
Are there any better approaches?
Upvotes: 1
Views: 699
Reputation: 420
You can implement a kernel space lock. Before kernel read the shared memory it should get the lock, otherwise it failed to read; before user write the shared memory it should get the lock too, otherwise it failed to write. You can implement ioctl in your kernel driver which get the lock, and user should call the ioctl before it write the memory.
Upvotes: 0