1828015 Members
1621 Online
109973 Solutions
New Discussion

Re: Main Memory Sharing

 
SOLVED
Go to solution
Nagapandi
New Member

Main Memory Sharing

Hi,

1) Execute all functions in a common/shared area. How it can be manipulate? (Which system need to use that will give read/write permission to other function/exe's).

2) I am using HP-UX11

3) If possible, pls give example program (.c/.cpp) that saying how it can be implemented. So that I can easily understand.

Thanks in Advance,
Nagapandi :)
11 REPLIES 11
Dennis Handly
Acclaimed Contributor

Re: Main Memory Sharing

You can create shared memory segments or shared mapped files. You can use normal loads/stores to access the data.

If you want to share functions/code, you create shared libs.

To create them see shmget(2), shmat(2) or mmap(2).

This thread many give you some ideas:
http://forums11.itrc.hp.com/service/forums/questionanswer.do?threadId=1139789
Nagapandi
New Member

Re: Main Memory Sharing

Thanks Dennis, that thread is good one, still that one is sharing the variable memory area, but I am asking how we can execute few functions in the shared area. How we can load? How we can execute in our desired location from the main memory?
Give me some more visions about shared libs as you said.

Thanks, Naga :)
Dennis Handly
Acclaimed Contributor

Re: Main Memory Sharing

>but I am asking how we can execute few functions in the shared area.

You share readonly functions as part of the executable or shared lib.

>How we can load? How we can execute in our desired location from the main memory?

You can either link them to your application or you can dynamically load shared libs with dlopen or shl_load.

You have no choice in address for functions.
Nagapandi
New Member

Re: Main Memory Sharing

>>You have no choice in address for functions.

Means, Can't we get the main memory address of currently executing function?

As well, Can't we place our function in desired main memory area?

Thanks,
Naga :)
A. Clay Stephenson
Acclaimed Contributor

Re: Main Memory Sharing

>>Means, Can't we get the main memory address of currently executing function?

>>As well, Can't we place our function in desired main memory area?

No. Think about the implications of what you are asking. If I could place a function anywhere I liked in memory, I could usurp the system. Each process has its own address space and the OS makes sures that processes are not allowed to access memory outside of that space. The exception is shared memory and shared library code (and it really isn't an exception) because the same areas are mapped to multiple processes.

As Dennis has already told you, the correct approach to using the same function is many processes is to build a shared library.
If it ain't broke, I can fix that.
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Main Memory Sharing

I can tell you about a feature of UNIX from the old days that does do some of what you want. Originally, there was only the fork() system call which copied an entire process when it created a child process. Because these were separate copies, nothing in the child could affect the parent process and the processes were safely isolated.

It was recognized that in almost every case the child process did an exec() and became a completely different process. All of the extra work required to copy the process was not needed. The vfork() system call was invented to copy on a small fraction of the process and allow the two processes to share the same memory space. As long as the child did an immediate exec() that was a good optimization. However, a few people discovered that you could keep right on running without exec()'ing and have two processes (or more with additional vfork()'s) writing to the same memory) and you had shared memory without using traditional shared memory system calls. (It was a poor man's implementation of shared memory BUT IT WAS EXTREMELY DANGEROUS.)
If it ain't broke, I can fix that.
Srimalik
Valued Contributor

Re: Main Memory Sharing

Naga,

In modern systems the processor generates virtual address and these are translated to physical addresses in main memory by the OS.
So there is no easy way to know the physical address of any variable/function in a program.

You can use google to search/read about these terms

"virtual memory", "paging and page tables" , "segmentation" "MMU + OS"

Thanks
Sri
abandon all hope, ye who enter here..
Dennis Handly
Acclaimed Contributor

Re: Main Memory Sharing

>Can't we get the main memory address of currently executing function?

You don't want to be in that business. HP-UX already has two ways to share data that I mentioned. And separate ways to share code. And you shouldn't try to mix the two, without a very good reason.

>Can't we place our function in desired main memory area?

Why work so hard? As part of a function, it has to have all this massive infrastructure to enable references to other functions and data. That's besides compiling it PIC.

>Clay: If I could place a function anywhere I liked in memory, I could usurp the system.

I assume anywhere in shared memory that Nagapandi owns.

>The vfork() system call was invented to copy on a small fraction of the process

Other OSes use copy on write to handle this.

>Sri: So there is no easy way to know the physical address of any variable/function in a program.

I assume Nagapandi was asking about a shared virtual address.
James R. Ferguson
Acclaimed Contributor

Re: Main Memory Sharing

HI:

> Can't we get the main memory address of currently executing function?

> Can't we place our function in desired main memory area?

Why do you care about the absolute address and why do you want to control where your code is placed in memory?

You're questions seem better suited to a time of assembly languages and early operating systems, not compiled languages and virtual memory management.

It sounds like you want to eke out every last processor cycle from your machine when your code executes. Is this some real-time application or is this a purely academic set of questions?

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Main Memory Sharing

>JRF: It sounds like you want to ...

Or want to dynamically generate code like Java?
Nagapandi
New Member

Re: Main Memory Sharing

Thanks for all of your effort. From this thread I came to know lots of things.

thanks, Naga :)