st_nlink with shared memory
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 > st_nlink with shared memory




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

    st_nlink with shared memory  
jombocito


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


 
11-05-07 06:28 PM


Hello can someone give me some reference to know how a st_nlink works
when I open a shared memory with POSIX?

I try to do some shm_open(...) but when I extract stat, st_nlink is
always one.

Thank you.






[ Post a follow-up to this message ]



    Re: st_nlink with shared memory  
David Schwartz


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


 
11-07-07 12:38 PM

On Nov 5, 9:36 am, jombocito <jomboc...@gmail.com> wrote:
> Hello can someone give me some reference to know how a st_nlink works
> when I open a shared memory with POSIX?

I don't think it's defined.

> I try to do some shm_open(...) but when I extract stat, st_nlink is
> always one.

This may be for one of three reasons:

1) The system just puts in 1 for st_nlink when it's dealing with files
that have no defined number of links.

2) The way shared memory is implemented on this system results in
shared memory segments always having exactly one link.

3) The code doesn't do anything special and this is just what winds up
in there.

DS






[ Post a follow-up to this message ]



    Re: st_nlink with shared memory  
Geoff Clare


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


 
11-08-07 06:27 PM

David Schwartz wrote:

> On Nov 5, 9:36 am, jombocito <jomboc...@gmail.com> wrote: 
>
> I don't think it's defined.

Right.  In POSIX.1 the description of fstat() says:

If fildes references a shared memory object, the implementation
shall update in the stat structure pointed to by the buf argument
only the st_uid, st_gid, st_size, and st_mode fields, and only the
S_IRUSR, S_IWUSR, S_IRGRP, S_IWGRP, S_IROTH, and S_IWOTH file
permission bits need be valid. The implementation may update other
fields and flags.
 
>
> This may be for one of three reasons:
>
> 1) The system just puts in 1 for st_nlink when it's dealing with files
> that have no defined number of links.

That's not allowed by POSIX for most file types.  Only shared memory
objects, typed memory objects and symbolic links need not have a
meaningful st_nlink.  For all other file types it must be the number
of links (directory entries) that the file has.  If it has no links,
st_nlink must be zero.  (I seem to recall that Linux gets this wrong
for pipes.)

> 2) The way shared memory is implemented on this system results in
> shared memory segments always having exactly one link.

This would be typical for systems where shared memory objects are
visible in the file hierarchy.  (If you could find the file and
execute "ln thefile anotherfile" you may find st_nlink is then 2.)

> 3) The code doesn't do anything special and this is just what winds up
> in there.

Or a slight variant of that: the value of st_nlink happened to be 1
before the fstat() call and the call did not set it.

--
Geoff Clare <netnews@gclare.org.uk>





[ Post a follow-up to this message ]



    Re: st_nlink with shared memory  
jombocito


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


 
11-19-07 12:33 PM


I find the next text in http://linux.die.net/man/3/shm_open :

"After a successful shm_unlink(), attempts to shm_open() an object
with the same name will fail (unless O_CREAT was specified, in which
case a new, distinct object is created)."

So I want call shm_unlink() only when the last process connected to
the shared memory finished it=B4s connection, because there can be new
processes that want to connect to this shared memory. But How can I
know which is the last process connected to this shared memory if I can
=B4t use st_nlink?

Thak you for your answers.





[ Post a follow-up to this message ]



    Re: st_nlink with shared memory  
Barry Margolin


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


 
11-20-07 06:37 AM

In article
<bd784668-db2b-422b-85b6-2e8618a7e0aa@d61g2000hsa.googlegroups.com>,
jombocito <jombocito@gmail.com> wrote:

> I find the next text in http://linux.die.net/man/3/shm_open :
>
> "After a successful shm_unlink(), attempts to shm_open() an object
> with the same name will fail (unless O_CREAT was specified, in which
> case a new, distinct object is created)."
>
> So I want call shm_unlink() only when the last process connected to
> the shared memory finished itīs connection, because there can be new
> processes that want to connect to this shared memory. But How can I
> know which is the last process connected to this shared memory if I can
> īt use st_nlink?
>
> Thak you for your answers.

There's no built-in way to do this.  You need to track it separately in
your application or library.

About 6 months ago we had a thread on a similar topic -- someone wanted
to be able to tell when the last process had closed a named pipe, and
remove it when this happened.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 09:00 AM.      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