简体   繁体   中英

POSIX Semaphore or RW lock inside shared memory

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.

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.

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM