Solved

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

Posted on 2003-11-10
17
604 Views
Last Modified: 2008-02-01

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
0
Comment
Question by:mBf
  • 5
  • 4
  • 3
  • +2
17 Comments
 
LVL 5

Accepted Solution

by:
g0rath earned 200 total points
ID: 9718455
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
 

Author Comment

by:mBf
ID: 9719347

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
 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 200 total points
ID: 9720439
>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
 
LVL 5

Expert Comment

by:g0rath
ID: 9722017
By definition, the only way to do what you want is via shared memory or some shared proccess.
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9725440
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
 
LVL 5

Expert Comment

by:g0rath
ID: 9725711
in other not portable and may change with revisions to the compiler....use at your own risk
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 9728744
I would still insist that shared memory is a better solution
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 24

Expert Comment

by:shivsa
ID: 9735534
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
 
LVL 5

Expert Comment

by:g0rath
ID: 9735932
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10738449
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
 

Author Comment

by:mBf
ID: 10761802

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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10763207
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
 

Author Comment

by:mBf
ID: 10766427

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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 10771996
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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now