[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 651
  • Last Modified:

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
0
mBf
Asked:
mBf
  • 5
  • 4
  • 3
  • +2
2 Solutions
 
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
 
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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
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

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

  • 5
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now