Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3441564
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:34:06+00:00 2026-05-18T08:34:06+00:00

When using mmap() for shared memory (from Linux, or other UNIX-like systems) is it

  • 0

When using mmap() for shared memory (from Linux, or other UNIX-like systems) is it possible (and portable) to use fcntl() (or flock() or lockf() functions) to co-ordinate access to the mapping?

Responses to this SO question seems to suggest that it should work.

The idea I have in mind would be to have the shared memory structured with a process/page map to minimize the locking contention. Processes could each work with their pages concurrently, and a lock would only need to be acquired when updating the process/page mappings. (Read access from unowned pages would involve checking a serial number, copying the desired data, then validating that the serial number of that block hadn’t changed).

Conceptually each process sharing this file mapping would perform the mmap(), find a free block therein, acquire a lock to the process/page area, update that with its own assignment, release the lock and then go on merrily with its work. Any process could search for stale mappings (using kill() with zero as the signal) and clean up the process/page table mapping.

(In rough, generic terms, I’m toying with a producer/consumer processing engine using shared memory from Python over Linux; I’d like the solution to be portable to BSD and to other programming languages — so long as the support mmap() and the necessary interfaces to fcntl(), flock() or lockf(). I’d also be interested in psuedo-code showing how one would measure lock contention and detect any synchronization failures. I am aware that the threading and multiprocessing with their respective Queue() objects are the most straightforward way to implement a Python producer/consumer processing model).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:34:07+00:00Added an answer on May 18, 2026 at 8:34 am

    I’m sure the locks will provide mutual exclusion, but I don’t know if they will give you a memory barrier. It seems like jumping into the kernel (which fcntl, flock, and lockf will do) is likely to do something that forces out of order memory reads and writes to commit, but I doubt you’ll get a hard guarantee. I think this is one of those things where it probably works, and testing will show that it does work, but you won’t know that it always works unless you find a reference saying as much.

    I’ve done something similar to this from C, but I used atomic spinlocks in the shared memory itself. It used to be that you had to do a little bit of inline assembly, but gcc now has some intrinsic operations that you can use:

    http://gcc.gnu.org/onlinedocs/gcc/Atomic-Builtins.html

    If you’re willing to write a very simple Python extension, you could wrap __sync_lock_test_and_set(…) and __sync_lock_release(…) to do what you need. Those should be pretty portable.

    I believe there is a way to put pthread mutexes into shared memory too, but I don’t have any experience with that. Again, you’d have to write a simple C extension to get access to that from Python.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the driver I posted at Direct Memory Access in Linux to mmap
I would like to map a file into memory using mmap function and would
So I have the following code, which allocates memory using the mmap linux system
I am using the combination of shm_open() and mmap() to create a shared memory
I have mapped a file into memory using mmap . Now I would like
I am using mmap for allocation memory and mark some pages as PROT_READ and
I am using mmap call to read from a very big file using simple
I heard about using a mmap system call for merging two blocks of memory
I need to make some stream in and out classes using mmap() in Linux.
I am trying to setup a 'shared' memory location using caddr_t mmap_ptr; But am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.