Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - General
User Name
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.


  Search this Thread
Old 10-23-2012, 11:43 AM   #1
LQ Newbie
Registered: Aug 2010
Posts: 10

Rep: Reputation: 0
Memory mapped file descriptor opened after execl is empty but should have content

Hi guys,

I am working on sharing memory to a execl'd process using mmap MAP_SHARED memory. I open a file before execl, mmap the returned fd, write to the region, leave the fd open and pass the opened fd to the execl'd process. I believe I have a lapse in my understanding how MMAP works under-the-hood and as a result I'm not seeing the content in my execl'd process.

Here is the basic logic/order of my code:

open("/dev/null", fd);
addr = mmap(NULL, sb.size, PROTO_READ|PROTO_WRITE, MAP_SHARED, fd, 0);
memcpy(addr, stuff, size(stuff));
//addr now has content
execl("./test", "test", fd_str, NULL);

In test:
new_addr=mmap(NULL, sb.size, PROTO_READ|PROTO_WRITE, MAP_SHARED, atoi(fd_str), 0);
//new_addr is all empty.

Let me know if this is the right forum for this question. Thanks for your help.

just an FYI, from the man pages for execve, file descriptors are left open on execve unless FD_CLOEXEC is set.
Old 11-03-2012, 10:47 AM   #2
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Stretch (Fluxbox WM)
Posts: 1,389
Blog Entries: 52

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
If you are wanting to share via a file backed memory map, then you cannot use "/dev/null" as the file backing, it will need to be a real file. Otherwise any data written will end up in... /dev/null!

If you don't want a real file being mapped, you can create an anonymous memory map, but this will not be available to the target of the exec.

Depending on your application, perhaps a more suitable method would be a shared memory segment. By using shmget/shmat, you will be able to pass the shared memory segment on to the new process (and to any child processes). Even nicer, the key token to access it can be made a constant, rather than having to pass it via arguments.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Get number of dirty pages of memory-mapped file via syscall dezo Linux - Kernel 1 01-16-2012 11:14 AM
How to get File Descriptor of an already opened file?? pulkit_m Programming 2 07-20-2011 05:59 AM
Network device Memory mapped or Port mapped ?? Bignon Linux - Hardware 0 10-20-2009 08:36 AM
'\n' eats line when stdout opened from file descriptor rsood Programming 4 08-31-2008 12:14 AM
Memory Mapped file IO problems legogt Programming 0 08-01-2003 01:52 PM > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:29 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration