Surviving a shared memory after malloc() and execl()


Greetings,

In my little code, I try to malloc() a memory segment in such a way that it can survive after issuing an execl()

Of course, I get that allocated memory segment emptied after execl() and a Segmentation Fault. Is there a workaround knowing that IPC shared memory is not an option?

-- mBf
mBfAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

g0rathCommented:
So in the child proccess you malloc memory, and then attempt to call it in the parent after control returns, or during the execution?

So you cannot used shared memory to do this, and you want another method of sharing memory?

I've used Memory Mapped files, where it maps a segement of memory to a file, and only on changing the pointer does it write to the file, or whenever you want it to. You may need to use semephores if you want proper syncronization.

This may have additional overhead that you do not want, as in extra file I/O.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mBfAuthor Commented:

No, in the parent process I malloc memory, I then attemp to execl a shell command that's supposed to make use of that allocated memory. I can't do so because once I execl(), the allocated memory is emptied.

Obviously, I have no control on the source code of the process that I try to execl it, as it's a shell command again.

And gain, memory mapped files as for IPC shared memory isn't an option for my implementation.

--mBf
0
sunnycoderCommented:
>Is there a workaround knowing that IPC shared memory is not an option?
No. The child and the parent are two different process and memory allocated by malloc() belongs to the parent... you cannnot access the same memory from the child unless it happens to be shared memory. One process cannot access another's memory... that is the whole point!!!

You will have to use some kind of IPC mechanism, may it be pipes, sockets, message queues, shared memory or anything else to commmunicate. Best would be to use shared memory as from the language of your question, it seems as if everything else is in place.
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

g0rathCommented:
By definition, the only way to do what you want is via shared memory or some shared proccess.
0
mtmikeCommented:
You might be able to ptrace() a child that calls exec() and then copy the memory chunk into its address space.

Note that the ptrace() system call is intended for debugging and not for IPC.
0
g0rathCommented:
in other not portable and may change with revisions to the compiler....use at your own risk
0
sunnycoderCommented:
I would still insist that shared memory is a better solution
0
shivsaCommented:
Another possibility is to use a specific area of shared memory and do
not put any pointers in it. You can have all processes 'mmap' the same file
(or do it before you 'fork') or you can use SysV IPC shared memory.

also u can use multithreading instaed of forking and have shared memory. here is no essential difference between a thread and a process. Instead, in Linux, when a process creates another process it can choose what resources are shared (e.g., memory can be shared). The Linux kernel then performs optimizations to get thread-level speeds; see clone(2) for more information
0
g0rathCommented:
that would be easy except he specifically stated he didn't want to use shared memory...so threads may be the closest....or not :)
0
sunnycoderCommented:
Hi Lunchy,

I have objections to deletion. No is the answer in this case and has been reasonably explained and alternatives suggested.

If you still feel that comments do not deserve to be awarded the points, I would recommend PAQ/No refund but never a delete/refund

sunnycoder
0
mBfAuthor Commented:

Yes, surely I agree on what sunnucoder said.

I havent known that a refund would delete this as a PAQ but I wanted this question to be closed and in the mean while no expert answered me. I asked for a workaround and I stated that solution X is not an answer and received "No, there are no workarounds and only X is the solution".

If a refund/PAQ is possible, I would ask for that.
If not, I would leave this issue for the administrator to handle.

Cheers,

--mBf
www.bo2k.com



0
sunnycoderCommented:
mBf,

If you found a solution using "fork" and without using shared memory or some other IPC, post it here and I shall have no objections to deletions. But as of now, in my limited knowledge, "no possible solution" was *the* answer and it was provided with appropriate reasoning.

Also I am not sure if posting links as signatures is permitted at EE

sunnycoder
0
mBfAuthor Commented:

No, I have not and also do not want this question to be deleted.

If PAQ/no refund/no award is the only solution (I do not know much about EE system btw), I would agree on that because it simply tells whom who have the same problem that there is no solution but for *my* alternative.

But with my all respect to experts here who commented on the problem, no one deserves to be awarded the points.

BTW, I am interested to know whether posting links as signatures is allowed here or not.

Cheers,

--mBf


0
sunnycoderCommented:
Hi Anniemod,

Asker concedes that no is the correct answer but not what he wants. I would recommend normal closure by awarding in whatever way you deem fit.

sunnycoder
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.