ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
An application I'm writing involves two threads in the same process negotiating using shmat, a memory location where a shared memory can be attached.
This is roughly how it works:
Code:
Thread 1:
addr = shmat( shm_id, NULL, SHM_RND );
while ( !result ) {
result = proposeAttachLoacation( addr )
if ( !result ) {
shmdt( addr );
addr = shmat( shm_id, addr+0x4000, SHM_RND );
}
}
Thread 2:
//shmget was successful
//addr received through a named pipe initially.
addr = shmat( shm_id, addr, 0 );
The shmat in the 2nd thread results in EINVAL. Is it even valid to use this technique between 2 threads in the same process? Please help me debug this.
EINVAL is what you get if you specify an address, but it can't be used (among other things). Why do you feel it is important to specify an address in thread 2? Try just specifying NULL and using what you're given - also, why are you trying different addresses after NULL in thread 1? I can't quite see what you're trying to achieve here...
Unless you have specific requirements, you should probably just use NULL as the second argument to shmat().
Last edited by JohnGraham; 12-15-2011 at 06:02 AM.
Sorry, I just realised why you *might* be trying to specify an addr in thread 2 - however, this is not necessary. Because of virtual memory, the system is able to map pages in your virtual address space anywhere in the physical address space - the upshot is you could be accessing address 0x1000, and another process 0x2000, and the OS can have arranged matters so that you're actually both accessing the same physical address 0x3000.
In other words - you rarely (if ever) need to use the addr parameter of shmat().
The reason I'm using this mechanism is that the structure instance to which addr points, contains as members, absolute pointers. At least in the case of the two threads existing in different processes, I found the use of this mechanism necessary. In this case, where the two threads are part of the same process, I'm not really sure ( more accurately, honestly confused ) on how to approach the problem.
I guess I should have mentioned the existence of the absolute pointers, but that is what made me specify an address as the second argument to shmat. Please help me clear out the confusion.
the structure instance to which addr points, contains as members, absolute pointers.
You can't do that with shared memory - if the pointers point to an address in any particular process' address space, they will be invalid to other processes. Other threads will be okay, but that's only because they share the same address space, and so the use of shared memory is pointless.
Your basic problem is that you're trying to mash two incompatible things together - sharing an address space (as two threads do) and sharing memory across processes (as shared memory does). This will not work, because of virtual memory. Pick one and run with it - my guess is that you want to drop the absolute pointers and use offsets into the shared memory segment, but that also seems like a kludge. In addition, make sure you don't try to reference your own process' memory from the shared memory segment - that will not work across different processes.
Last edited by JohnGraham; 12-15-2011 at 12:40 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.