I am making a logging API where I use fprintf
to print to a log file. Since fprintf is an atomic operation, the log messages won't overwrite each other even if multiple threads of a same process writes to the same file simultaneously.
I am not sure I fully understand the concept of atomic operation in context of multiprocessing. What will happen if multiple processes start writing to the same file simultaneously using fprintf. For example, in case of fork, parent and child process writes simultaneously to the same file using fprintf.
Notes: The log file is open once
My questions:
- Is there a chance of different log messages overwriting each other?
- Is concept of atomic operation applicable to multiple processes? (Is it guaranteed that a called to fprintf in a process will complete entirely before another process calls fprintf?)
In short: Can I get away with just fprintf without using any locking mechanism and have a logging api that is safe to write simultaneously to the same file from multiple processes?