POSIX共享内存实际上是映射内存的一种变体。主要区别在于使用shm_open()打开共享内存对象(而不是调用open()),并使用shm_unlink()关闭和删除该对象(而不是调用不删除对象的close())。shm_open()中的选项远少于open()提供的选项。
IPC共享内存是在程序之间传递数据的有效方法。一个程序将创建一个内存部分,其他进程(如果允许)可以访问它。
在我看来,它们之间的区别并不是很大或很大,因为POSIX共享内存只是后来和更标准的共享内存概念实现。kill -9问题确实是个问题。您无法设置任何处理程序,甚至atexit也不会被调用。但是您可以为SIGTERM设置处理程序并使用命令终止您的进程:
kill -SIGTERM <pid>
atexit和信号处理程序的示例代码:
#include <signal.h>
#include <unistd.h>
#include <cerrno>
#include <system_error>
#include <iostream>
static sigset_t theMask;
static void
signalWrapper(
int theSignalNumber,
siginfo_t* theSignalDescription,
void* theUserContext)
{
if (theSignalNumber == SIGTERM)
{
std::cerr << "Clear shared memory and exit" << std::endl;
exit(1);
}
struct ::sigaction sa;
sa.sa_sigaction = &signalWrapper;
sa.sa_mask = theMask;
sa.sa_flags = SA_SIGINFO;
try
{
if (::sigaction(theSignalNumber, &sa, NULL) == -1)
throw std::error_code(errno, std::system_category());
}
catch (const std::error_code& ec)
{
std::cerr << ec << std::endl;
}
}
void
setupSignalHandlers()
{
struct ::sigaction sa;
sigemptyset(&theMask);
sigaddset(&theMask, SIGTERM);
sa.sa_mask = theMask;
sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = &signalWrapper;
try
{
if (::sigaction(SIGTERM, &sa, NULL) == -1)
throw std::error_code(errno, std::system_category());
}
catch (const std::error_code& ec)
{
std::cerr << ec << std::endl;
}
}
void
bye()
{
std::cout << "Bye!" << std::endl;
}
int
main()
{
std::cout << "Set handler!" << std::endl;
setupSignalHandlers();
if (std::atexit(bye))
{
std::cerr << "Failed to register atexit" << std::endl;
return 2;
}
std::cout << "Emit SIGTERM signals" << std::endl;
kill(getpid(), SIGTERM);
sleep(100);
return 0;
}
另一种有趣的方法是在 shm_open() 之后直接使用 shm_unlink(),并使用 O_CLOEXEC。我编写了一个小代码示例来说明如何使用unlink和POSIX共享内存重新打开:
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cerrno>
#include <cstring>
#include <system_error>
#include <iostream>
struct ShMem
{
ShMem(
const std::string& theName)
: _handle(-1),
_mode(S_IRUSR | S_IWUSR),
_name(theName)
{
if ((_handle = shm_open(_name.c_str(), O_CREAT | O_RDWR | O_EXCL, _mode)) < 0)
{
std::cerr << strerror(errno) << std::endl;
std::cout << "File " << _name << "exists. Try to reopen it!" << std::endl;
if ((_handle = shm_open(_name.c_str(), O_RDWR | O_TRUNC, _mode)) < 0)
{
std::cerr << strerror(errno) << std::endl;
throw std::error_code(errno, std::system_category());
}
else
std::cout << _name << " reopened OK";
}
else
std::cout << _name << " created OK";
shm_unlink(_name.c_str());
}
~ShMem()
{
}
const int
handle()
{
return _handle;
}
private:
int _handle;
mode_t _mode;
std::string _name;
};
使用 O_CLOEXEC,您可以完全删除 shm_unlink() 并查看在 kill -9 发生时会发生什么。
SIGKILL
信号,这是由kill -9
引发的。 - Drew McGowen