Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Unix Operating Systems > HP-UX Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-20-2008, 12:42 PM
Alex Vinokur
 
Posts: n/a
Default Detect if a segment existes in sharred memoty

> uname -smrsv
HP-UX B.11.23 U ia64


Here are two functions to detect if some segment exists in shared
memory.


bool shmSegmentExists(key_t key)
{
if ((shmget(key, 0, myFlags | IPC_CREAT | IPC_EXCL) == -1)
{
if (errno == EEXIST)
{
return true;
}
}

return false;
}

bool shmSegmentIsAbsent(key_t key)
{
assert (!(myFlags & IPC_CREAT));
if ((shmget(key, 0, myFlags) == -1)
{
if (errno == ENOENT)
{
return true;
}
}

return false;
}


--------------------------

1. Are those functions valid?

2. If the functions are indeed valid, is this always true:
assert (shmSegmentExists(key) == !shmSegmentIsAbsent (key));
?


Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-20-2008, 12:42 PM
Don Morris
 
Posts: n/a
Default Re: Detect if a segment existes in sharred memoty

Alex Vinokur wrote:
>> uname -smrsv

> HP-UX B.11.23 U ia64


Note on the below -- if you add PA into the mix, there an additional
problem of a binary using your functions where chatr +q4p enabled was
used [Q4 private process]. Such a process gets EFAULT on any
shmget() call [since there's no shared address space for it to use].
ia64 chatr does not allow this option according to the man page and
my knowledge, hence I won't discuss it further... but beware if you're
writing a library that will be compiled on PA as well.

>
>
> Here are two functions to detect if some segment exists in shared
> memory.
>
>
> bool shmSegmentExists(key_t key)
> {
> if ((shmget(key, 0, myFlags | IPC_CREAT | IPC_EXCL) == -1)


You don't define myFlags anywhere I can see. An incorrect flag
to shmget() can yield EINVAL, you'll falsely return that the
segment does not exist. Since in this case you're only checking
for segment existence and explicitly plan to fail on actual
creation, I'd lose myFlags entirely and just pass (IPC_CREAT|IPC_EXCL).

If your key is IPC_PRIVATE you'll fail with ENOSPC (no segments
left) or EINVAL [0 sized segment not allowed]. Technically, asking
if a private key segment already exists without knowing the shmid
is a weird op anyway -- each call to shmget() with IPC_PRIVATE is
supposed to create a new segment in the first place... so failing
to consider a brand new segment which you failed to create as
existing is reasonable, though an odd exercise in logic.


> {
> if (errno == EEXIST)
> {
> return true;
> }
> }
>
> return false;
> }
>
> bool shmSegmentIsAbsent(key_t key)
> {
> assert (!(myFlags & IPC_CREAT));
> if ((shmget(key, 0, myFlags) == -1)


Again, key == IPC_PRIVATE could give ENOSPC or EINVAL here.

Other invalid flags or flag combinations could give EINVAL.

> {
> if (errno == ENOENT)
> {
> return true;
> }
> }
>
> return false;
> }
>
>
> --------------------------
>
> 1. Are those functions valid?


With the caveats listed, I believe so, yes.

>
> 2. If the functions are indeed valid, is this always true:
> assert (shmSegmentExists(key) == !shmSegmentIsAbsent (key));


Not if key is IPC_PRIVATE (both return False, False != True) or
for illegal flags (i.e. again, see caveats).

In general and with appropriate pre-checks, it should hold.

Don
--
kernel, n:
A part of an operating system that preserves the medieval traditions
of sorcery and black art.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-20-2008, 12:42 PM
Alex Vinokur
 
Posts: n/a
Default Re: Detect if a segment existes in sharred memoty

On Mar 19, 5:17*pm, Don Morris <dmor...@cup.hp.com> wrote:
> AlexVinokur wrote:
> >> uname -smrsv

> > HP-UX B.11.23 U ia64


[snipped]

> > bool shmSegmentExists(key_t key)


[snipped]

> > bool shmSegmentIsAbsent(key_t key)


[snipped]

Here are new versions of the functions:

bool shmSegmentExists(key_t key)
{
assert (key != IPC_PRIVATE);
if ((shmget(key, 0, IPC_CREAT | IPC_EXCL) == -1)
{
if (errno == EEXIST)
{
return true;
}
}
return false;
}


bool shmSegmentIsAbsent(key_t key)
{
assert (key != IPC_PRIVATE);
if ((shmget(key, 0, 0) == -1)
{
if (errno == ENOENT)
{
return true;
}
}
return false;
}

Don, thank you for your detailed, helpful and useful informatiion (as
usual).

Regards,

Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 08:45 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62