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,
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
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/
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?
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.