POSIX信号量或RW锁定共享内存

时间:2021-06-03 21:02:55

I have a server C application that writes the logs in synchronous mode with write(2), but because of this, it sometimes blocks for 100-400 ms (sometimes more) as the IO load on the server machine is very high.

我有一个服务器C应用程序,它使用write(2)以同步模式写入日志,但由于这一点,它有时会阻塞100-400毫秒(有时更多),因为服务器计算机上的IO负载非常高。

I want to write the logs to a POSIX shared memory area mmap-ed in the address space of the process. In this case, another process only will read from that POSIX shared memory and will write to disk. This way, if the process crashes, the log information is not lost, the only problem is in case of power failure.

我想将日志写入进程地址空间中mmap-ed的POSIX共享内存区域。在这种情况下,另一个进程只读取该POSIX共享内存并写入磁盘。这样,如果进程崩溃,日志信息不会丢失,唯一的问题是电源故障。

It is safe to put a POSIX semaphore or POSIX RW lock inside the shared memory (and thus inside the mmap-ed area) for synchronization between the writer and the reader? Are there any special things to consider in this case?

将POSIX信号量或POSIX RW锁定放在共享内存中(因此在mmap-ed区域内)以便在writer和reader之间进行同步是安全的吗?在这种情况下有什么特别的事情需要考虑吗?

1 个解决方案

#1


1  

You could certainly re-invent the wheel but you might want to consider using syslog [1]. However, if you are certain that you -have- to do things your way you might want to consider using mqueues[2] or named semaphores[3] in conjunction with shared memory.

你当然可以重新发明*,但你可能想考虑使用syslog [1]。但是,如果您确定自己 - 以自己的方式做事,则可能需要考虑使用mqueues [2]或将命名信号[3]与共享内存结合使用。

[1] http://www.gnu.org/software/libc/manual/html_node/Syslog.html#Syslog
[2] man 7 mq_overview
[3] man 7 sem_overview

[1] http://www.gnu.org/software/libc/manual/html_node/Syslog.html#Syslog [2] man 7 mq_overview [3] man 7 sem_overview

#1


1  

You could certainly re-invent the wheel but you might want to consider using syslog [1]. However, if you are certain that you -have- to do things your way you might want to consider using mqueues[2] or named semaphores[3] in conjunction with shared memory.

你当然可以重新发明*,但你可能想考虑使用syslog [1]。但是,如果您确定自己 - 以自己的方式做事,则可能需要考虑使用mqueues [2]或将命名信号[3]与共享内存结合使用。

[1] http://www.gnu.org/software/libc/manual/html_node/Syslog.html#Syslog
[2] man 7 mq_overview
[3] man 7 sem_overview

[1] http://www.gnu.org/software/libc/manual/html_node/Syslog.html#Syslog [2] man 7 mq_overview [3] man 7 sem_overview