LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to lock/unlock the shared memory created by mmap? (https://www.linuxquestions.org/questions/programming-9/how-to-lock-unlock-the-shared-memory-created-by-mmap-684356/)

john.daker 11-18-2008 10:48 AM

How to lock/unlock the shared memory created by mmap?
 
Hi,all
I create a shared memory with mmap, and share the memory within parent and child process, now I want to lock/unlock the shared memory, how to do this?

Thanks.

dangerboy 11-18-2008 12:23 PM

Quote:

Originally Posted by john.daker (Post 3346409)
Hi,all
I create a shared memory with mmap, and share the memory within parent and child process, now I want to lock/unlock the shared memory, how to do this?

Thanks.

You didn't state what langauge... many programming languages have an mmap function. A quick Google brought this up in C++

http://publib.boulder.ibm.com/infoce.../apis/mmap.htm

When in doubt, Google it. I taught myself how to program with Google and looking at other people's source code. Good luck!

john.daker 11-18-2008 10:14 PM

It's C.
Maybe I didnt use the right keyword, cannot found anything valuable.

ta0kira 11-19-2008 12:24 AM

That depends on what you mean by "lock." Do you mean protect/unprotect, or lock/unlock in physical RAM? The first can be done with mprotect (subject to limitations by the underlying file/device) and the second can be done with mlock. I don't think either will propagate between parents and children, however.
ta0kira

PS Or maybe you mean lock for exclusive write access? Look at semget if that's the case.

john.daker 11-19-2008 01:53 AM

process synchronization.
I googled that thread synchronization can use pthread_mutex_lock/unlock, but how process do the lock/unlock?
I created a shared memory, and want lock something , and child can read/modify the shared memory until get the lock

perfect_circle 11-19-2008 04:40 PM

I can't find a reason why to use processes (fork()) and not pthreads when you want to have shared memory....

ta0kira 11-19-2008 09:47 PM

There are quite a few valid reasons to use fork vs. threads (and vice-versa) even with shared memory, but that's an entirely different discussion.
ta0kira


All times are GMT -5. The time now is 12:16 AM.