shared memory between processes
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > shared memory between processes




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    shared memory between processes  
Chris Friesen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-03-05 11:00 PM

When sharing memory between processes, is the use of locking primitives
(fcntl, flock, sysV semas, NPTL process-shared operations, etc.)
sufficient to ensure that gcc will not optimize away the load from the
shared memory area?

Or do I need to also make it volatile?

As an example, consider the following code, where the locking operations
could be any one of the ones listed above:


int *b;

int test()
{
b=<address of shared memory>;

while(1) {
<lock>
if (*b) {
break;
}
<unlock>
}
<unlock>

return *b;
}

Without the locks, the compiler is free to only load *b once (and in
fact gcc does so).  Is the addition of the locks sufficient to force *b
to be re-read each time, or do I need to declare it as

volatile int *b;


Thanks,

Chris





[ Post a follow-up to this message ]



    Re: shared memory between processes  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-03-05 11:00 PM


"Chris Friesen" <cfriesen@nortelnetworks.com> wrote in message
news:d58r0m$i3r$1@zcars129.ca.nortel.com...

> When sharing memory between processes, is the use of locking primitives
> (fcntl, flock, sysV semas, NPTL process-shared operations, etc.)
> sufficient to ensure that gcc will not optimize away the load from the
> shared memory area?

Yes. Any mechanism another thread could use to access the memory could
also be used by the locking primitives, as far as GCC knows, so it must
reload.

DS







[ Post a follow-up to this message ]



    Re: shared memory between processes  
Chris Friesen


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-03-05 11:00 PM

David Schwartz wrote:

>     Yes. Any mechanism another thread could use to access the memory could
> also be used by the locking primitives, as far as GCC knows, so it must
> reload.

Okay, that makes sense for the pthread ones.  I wasn't sure that the
same would hold true for the strictly process-based ones (fcntl/flock/etc.).

Chris





[ Post a follow-up to this message ]



    Re: shared memory between processes  
David Schwartz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
05-03-05 11:00 PM


"Chris Friesen" <cfriesen@nortelnetworks.com> wrote in message
news:d58s0a$ks8$1@zcars129.ca.nortel.com...

> David Schwartz wrote:
 
[vbcol=seagreen]
> Okay, that makes sense for the pthread ones.  I wasn't sure that the same
> would hold true for the strictly process-based ones (fcntl/flock/etc.).

Any mechanism another process could use could also be used by another
thread or by the lock/unlock functions. That's how it magically happens to
work. (Of course, if it didn't magically happen to work, it would be made to
work by some other mechanism. But that's not needed. The synchronization
functions work.)

DS







[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 08:56 PM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register