UNIX 共享内存应用中的问题及解决方法( 六 )


if (isAttached) {
 // Attention: the following line should be "shmdt((char
*)mem_addr);", if on Solaris
shmdt((void *)mem_addr);
printf("%s, -.Successfully detached, times [d]n", argv[0], i);
}
// purposely remove the shared memory at times [5]
if (i==5) {
shmctl(mem_id, IPC_RMID, NULL);
printf("%s, *.Remove executed, times [d], errno=%dn",
argv[0], i, errno);
}
}
return 0;
}
上述程序均可在AIX、HP-UX、Linux平台上编译通过;在Solaris平台上只需按注释提示的要求,将shmdt的参数强制为char *类型也可编译通过(第5节中已介绍过) 。
将test03.P1.c、test03.P2.c各自编译为可执行程序test03.P1、test03.P2,并通过下面的shell脚本:runtest,运行它们:
#!/bin/sh
./test03.P1&
sleep 2
./test03.P2
在Linux平台(Red Hat 8.0)上的运行结果如下:
[root@localhost tmp]# ./runtest
./test03.P1,.Successfully attached shared memory
./test03.P2,.Successfully attached shared memory, times [01]
./test03.P2, -.Successfully detached, times [01]
./test03.P2,.Successfully attached shared memory, times [02]
./test03.P2, -.Successfully detached, times [02]
./test03.P2,.Successfully attached shared memory, times [03]
./test03.P2, -.Successfully detached, times [03]
./test03.P2,.Successfully attached shared memory, times [04]
./test03.P2, -.Successfully detached, times [04]
./test03.P2,.Successfully attached shared memory, times [05]
./test03.P2, -.Successfully detached, times [05]
./test03.P2, *.Remove executed, times [05], errno=0
./test03.P2,.Successfully attached shared memory, times [06]
./test03.P2, -.Successfully detached, times [06]
./test03.P2,.Successfully attached shared memory, times [07]
./test03.P2, -.Successfully detached, times [07]
./test03.P2,.Successfully attached shared memory, times [08]
./test03.P2, -.Successfully detached, times [08]
./test03.P2,.Successfully attached shared memory, times [09]
./test03.P2, -.Successfully detached, times [09]
[root@localhost tmp]# ./test03.P1, -.Successfully detached shared memory
./test03.P1, Failed to attach the removed shared memory, errno:22
根据运行结果,我们可以看到,在Linux平台上,即便对共享内存执行了删除操作(在第5次连接之后,test03.P2进程调用了shmctl的IPC_RMID删除操作),只要该共享内存依然存在(test03.P1进程保持着连接,因此共享内存不会被立即删除),则它仍然是可连接的(test03.P2进程的第6到第9次连接均是成功的) 。
然而,在AIX、HP-UX、Solaris平台上的运行结果却不同于Linux:
# ./runtest
./test03.P1,.Successfully attached shared memory
./test03.P2,.Successfully attached shared memory, times [01]
./test03.P2, -.Successfully detached, times [01]
./test03.P2,.Successfully attached shared memory, times [02]
./test03.P2, -.Successfully detached, times [02]
./test03.P2,.Successfully attached shared memory, times [03]
./test03.P2, -.Successfully detached, times [03]
./test03.P2,.Successfully attached shared memory, times [04]
./test03.P2, -.Successfully detached, times [04]
./test03.P2,.Successfully attached shared memory, times [05]
./test03.P2, -.Successfully detached, times [05]
./test03.P2, *.Remove executed, times [05], errno=0
./test03.P2, Failed to attach shared memory, times [06], errno:22
./test03.P2, Failed to attach shared memory, times [07], errno:22
./test03.P2, Failed to attach shared memory, times [08], errno:22
./test03.P2, Failed to attach shared memory, times [09], errno:22
# ./test03.P1, -.Successfully detached shared memory
./test03.P1, Failed to attach the removed shared memory, errno:22
根据结果,可以发现,test03.P2进程的第6到第9次连接都是失败的,也就说明,在AIX、HP-UX、Solaris平台上一旦通过shmctl对共享内存进行了删除操作,则该共享内存将不能再接受任何新的连接,即使它依然存在于系统中!

推荐阅读