LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-15-2011, 02:52 AM   #1
abhishekbatra
LQ Newbie
 
Registered: May 2008
Location: Delhi, India
Distribution: Debian, Ubuntu, Kubuntu
Posts: 27
Blog Entries: 1

Rep: Reputation: 0
Using shared memory with threads


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.

Thanks, Abhishek
 
Old 12-15-2011, 07:01 AM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
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 07:02 AM.
 
Old 12-15-2011, 07:19 AM   #3
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
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().
 
Old 12-15-2011, 11:33 AM   #4
abhishekbatra
LQ Newbie
 
Registered: May 2008
Location: Delhi, India
Distribution: Debian, Ubuntu, Kubuntu
Posts: 27
Blog Entries: 1

Original Poster
Rep: Reputation: 0
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.

Thanks,
Abhishek
 
Old 12-15-2011, 01:38 PM   #5
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 138Reputation: 138
Quote:
Originally Posted by abhishekbatra View Post
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 01:40 PM.
 
Old 12-15-2011, 02:03 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,543

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
Quote:
Originally Posted by abhishekbatra View Post
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.

...

Is it even valid to use this technique between 2 threads in the same process?
No, this makes no sense: threads in the same process already share all memory.
 
1 members found this post helpful.
Old 12-17-2011, 11:41 AM   #7
abhishekbatra
LQ Newbie
 
Registered: May 2008
Location: Delhi, India
Distribution: Debian, Ubuntu, Kubuntu
Posts: 27
Blog Entries: 1

Original Poster
Rep: Reputation: 0
@ntubski: Thanks, thats what I needed. I'll just pass on the pointer to the allocated memory in case the pid's are same.
 
  


Reply

Tags
shared, threading


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to create a shared memory and some shared objects in linux? jeremy28 Programming 6 06-11-2010 06:21 AM
Linux shared memory segment access problem and x86 Virtual Memory layout. regmee Linux - Kernel 1 08-23-2008 01:11 AM
Difference between resident memory,shared memory and virtual memory in system monitor mathimca05 Linux - Newbie 1 11-11-2007 05:05 AM
Any test suite in linux for threads, file descriptors and shared memory segs. Basavaraj Linux - Newbie 2 08-31-2006 09:47 PM


All times are GMT -5. The time now is 04:34 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration