Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Are shmat and shmdt expensive?

307 views
Skip to first unread message

Alvaro Nieto

unread,
Jan 2, 2003, 9:42:25 AM1/2/03
to
Hi,

I'm a newbie unix system programmer and I've a doubt with shmat and
shmdt functions (some people tell me it's expensive call shmat and
shmdt). I'd like to know if attach and deattach shared memory via
shmat an shmdt is an expensive task.

Thanks,

DINH Viêt Hoà

unread,
Jan 2, 2003, 10:50:01 AM1/2/03
to

generally speaking, calls to operating system (system calls) are
expensive, maybe some gurus can give some reasons ...

I heard about things such as cost of context switch between user mode and
kernel mode.

--
DINH V. Hoa,
libEtPan! - a mail library - http://libetpan.sourceforge.net

"elle est maquillée comme une voiture volée" -- Elisa

Michael Fuhr

unread,
Jan 2, 2003, 11:59:14 AM1/2/03
to
alv...@gsmbox.es (Alvaro Nieto) writes:

How do you define "expensive"? How often are you planning to call
shmat() and shmdt()? It may not matter how expensive the calls are
if the amount of time they take is negligible compared to the total
amount of time your program runs.

If you want to know how fast shmat() and shmdt() are, write a program
that calls them a number of times and time how long it takes.

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

Alvaro Nieto

unread,
Jan 3, 2003, 4:02:37 AM1/3/03
to
mf...@fuhr.org (Michael Fuhr) wrote in message news:<3e146fe2$1...@omega.dimensional.com>...

> alv...@gsmbox.es (Alvaro Nieto) writes:
>
> > I'm a newbie unix system programmer and I've a doubt with shmat and
> > shmdt functions (some people tell me it's expensive call shmat and
> > shmdt). I'd like to know if attach and deattach shared memory via
> > shmat an shmdt is an expensive task.
>
> How do you define "expensive"? How often are you planning to call
> shmat() and shmdt()? It may not matter how expensive the calls are
> if the amount of time they take is negligible compared to the total
> amount of time your program runs.

About 1000 per function call.


> If you want to know how fast shmat() and shmdt() are, write a program
> that calls them a number of times and time how long it takes.

I've wrote a program that call shmat() and shmdt() 100.000 times and
it was very fast. The profiling tool show me;

------------------------------------------------------------------------------
granularity: each sample hit covers 4 byte(s) for 1.16% of 0.08
seconds

% cumulative self self total
time seconds seconds calls ms/call ms/call name
31.4 0.03 0.03 100000 0.00 0.00 strcpy [3]
29.1 0.05 0.02 100000 0.00 0.00 __shmdt [4]
20.9 0.07 0.02 1 17.58 83.98 main [1]
18.6 0.08 0.02 100000 0.00 0.00 __shmat [5]
------------------------------------------------------------------------------

The client program is quite simple;

------------------------------------------------------------------------------
int main()
{
int hd;
void *memoria;
char salida[200] = "";

// Conectarse a la zona de memoria
hd = shmget(1010, 100, IPC_CREAT | 0660);
if ( -1 == hd )
{
perror("shmget:");
exit(-2);
}

// Enlazar con memoria compartida
for ( register int i = 0; i < 100000; i++ )
{
memoria = shmat(hd, 0, 0);
if ( (int)memoria == -1 )
{
perror("shmat:");
exit(-2);
}

// Copiar datos a variable
strcpy(salida, (char *)memoria);

// Desenlazar de memoria compartida
if ( -1 == shmdt(memoria) )
{
perror("shmdt:");
exit(-2);
}
}

return 0;
}
------------------------------------------------------------------------------

strcpy() is slower than shmat() and shmdt() !!!. It seems that shmat()
and shmdt() are not expensive, I'm wrong?

Chuck Dillon

unread,
Jan 3, 2003, 9:54:32 AM1/3/03
to
Alvaro Nieto wrote:
> strcpy() is slower than shmat() and shmdt() !!!. It seems that shmat()
> and shmdt() are not expensive, I'm wrong?

As the previous poster said, it depends. If you don't think it's
expensive in the context of your implementation then it's not expensive.

In your example you need to compare the time to do shmat+strcpy+shmdt
to just doing the strcpy alone. If you are copying a significant
number of chars the overhead of the shm* calls is probably negligable.

But if instead of doing a strcpy you were just copying an int with an
assignment you would find that the shm* calls add an order of magnitude
of overhead. Then they would be expensive.

So it depends.

-- ced


--
Chuck Dillon
Senior Software Engineer
NimbleGen Systems Inc.

0 new messages