4

I need to pass void handler to another application, To replicate the scenario I have created one small program using shared memory and try to pass the void pointer to another application and print the value, I can get the void pointer address in another application, but when I try to dereference the pointer second application crash.

Here are the sample application wire.c .

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>

int main() {

key_t key=1235;
int shm_id;
void *shm;
void *vPtr;

shm_id = shmget(key,10,IPC_CREAT | 0666);
shm = shmat(shm_id,NULL,NULL);
sprintf(shm,"%d",&vPtr);
printf("Address is %p, Value is %d \n", (void *)&vPtr, * (int *)vPtr);
return;
}

Here is read.c

#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <stdlib.h>

int main() {

key_t key=1235;
int shm_id;
void *shm;

void *p = (void *)malloc(sizeof(void));

shm_id = shmget(key,10,NULL);
shm = shmat(shm_id,NULL,NULL);
if(shm == NULL)
{
printf("error");
}
sscanf(shm,"%d",&p);
printf("Address is %p %d\n",(void *)p);
return 0;
}

When I try to dereference p it crash. I need to pass the void pointer address and value in second application.

I don't want to share value between application, It works using shared memory,I know.

By default void *ptr will have some garbadge value (for ex. add= 0xbfff7f, value=23456), Can you please tell me, how can i pass void pointer address to another application and from the second application using that address i can print the value which was found in first application (i.e. 23456).

Apart from the shared memory is there any other alternate available?

Thanks.

3limin4t0r
  • 19,353
  • 2
  • 31
  • 52
skanzariya
  • 397
  • 1
  • 4
  • 10
  • I also tried the file method, but it is also not useful – skanzariya Jan 22 '14 at 10:45
  • I tried to print the shm address and value, however in both the application address are different (i.e. virtual memory), but the value is same. I thought the shared memory, address will be same in both the process, but I was wrong, it is different, Now there is one more question comes to my mind, hows the shared memory works? what each process see(i.e. memory) in terms of access the value? – skanzariya Jan 22 '14 at 12:02

3 Answers3

1

This is probably because the pointer is still virtual; it's pointing at memory that is shared but there is no guarantee that two processes sharing the same memory maps it to the same virtual address. The physical addresses are of course the same (it's the same actual memory, after all) but processes never deal with physical addresses.

You can try to request a specific address by using mmap() (in Linux) to do the sharing. You can also verify the virtual address theory by simply printing the addresses of the shared memory block in both processes.

Also, don't cast the return value of malloc() in C and don't allocate memory when you're going to be using the shared memory. That part just makes no sense.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • yes that might be the case since both the application has its own virtual sendbox. I am trying to store the address in shared memory rather than a value. Let me know if any other method are available. I tried the file concept (storing void pointer address in file) and try to read the file in second program and store address in pointer, but it also not working. – skanzariya Jan 22 '14 at 10:44
1

If you want to call a function within a process from another process, it is NOT IPC.
IPC is sharing of data between multiple threads/processes.

Consider adding the shared function into a DLL/shared-object to share the code across processes. If not, then you could add RPC support to your executables as shown here.


Why passing function pointers between 2 process does NOT work?

A function pointer is a virtual address referring to the physical memory location where the function code is currently loaded into physical memory. Whenever a function pointer(virtual address) is referred to in the process, the kernel is responsible for performing the mapping to physical address. This is successful as the mapping is present in the page-tables for the current process.

However when a context-switch occurs and another process is running, the page-tables containing the mappings of that particular process are loaded and currently active. these will NOT contain the mapping of the function pointer form the previous process. Hence attempting to use the function pointer from another process will fail.

Why the page-tables do NOT contain the mapping of function in another process?

If this was done then there would be no advantages with having multiple processes. All the code that could ever be run would have to be loaded into physical memory simultaneously. Also the entire system would then effectively be a single process.

Practically speaking whenever a context-switch happens and a different process is executing, the code/data segments of the earlier process can even be swapped out of physical memory. Hence even maintaining a function pointer and passing it to the new process is useless as it cannot guarantee that the function code will be held in memory even after newer process is loaded in memory and starts executing.

Community
  • 1
  • 1
TheCodeArtist
  • 21,479
  • 4
  • 69
  • 130
  • Ok, Got it, So how can i shared handler between processes. I have one USB Hardware (with library), using their API I can handle the hardware, Now to use the hardware I need to create one handler in my application (That goes to API ), now I want to share the handler between the process to operate the hardware from other process. That handler is void pointer, Now I need to pass the void pointer to another process, that's what I am trying to achieve using shared memory. – skanzariya Jan 24 '14 at 08:08
  • Since you want to achieve this using shared memory, you could maintain a data struct in the shared memory region. Whenever an external process wants to call a function within this process, it updates the struct accordingly. A thread in the local process can periodically check this struct in shared memory and make the appropriate call to the function directly within the same process. Also an alternative to regularly polling the shared memory region for modification is to [**register a notification mechanism**](http://stackoverflow.com/a/17337839/319204). – TheCodeArtist Jan 24 '14 at 08:24
0

This is illegal:

void *p = (void *) malloc(sizeof(void));

void is an incomplete type, sizeof(void) is invalid. You can't do this for the same reason that you can't declare a void variable:

void i; /* WRONG */

What happens when you dereference p is undefined. If you just want the pointer value, you don't need to call malloc on read.c - that's the whole concept of shared memory - if it's shared, why would you allocate space on the reader program?

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
  • I removed the malloc part from the p, but still no result, when I try to dereference the p, it gives segfault, I did gdb, at that point i got address in p now when i try to print value it display 0x0 in gdb. – skanzariya Jan 22 '14 at 10:35