CodePudding user response:
Good question to answerCodePudding user response:
Good question, you this problem involves the underlying system, it depends on the operating system, unlike Windows, Linux allows a file at the time of writing are read when be read (or write)Linux file descriptor table maintenance through the open file descriptor information, and the file descriptor table of each point to a kernel maintenance file table, the table to open the file vnode (Unix) and inode, and at the same time, the file to read and write file offset table holds the process information, such as
But then how do we ensure consistency of read and write? Linux provides an FCNTL system calls, you can lock files,
File locks are related to process, multiple threads in a process/coroutines lock on the same file for operation will overwrite each other, which is invalid,
Lock is created by an FCNTL advisory locks, only the process of writing and reading processes are to follow the advice is valid; Corresponding with mandatory locks, to judge in each file operations, but the performance is poorer, therefore the default Linux/Unix system USES is advisory locks,
CodePudding user response:
related to the bottomCodePudding user response:
Because the operating system kernel is to help you achieve the atomicity of read and write operations to ensure (or lock control)CodePudding user response:
Can use foepn (FILE_NAME, "a") to append mode to open the file, and use setlinebuf () set the file buffer line, each time you write the log with \ n the end, finally to ensure that the different processes do not overlap,CodePudding user response:
Learn