先贴出来flock在man页下的解释
FLOCK(2) Linux Programmer’s Manual FLOCK(2)
NAME
flock - apply or remove an advisory lock on an open file
SYNOPSIS
#include <sys/file.h>
int flock(int fd, int operation);
DESCRIPTION
Apply or remove an advisory lock on the open file specified by fd. The parameter operation is one of the following:
LOCK_SH Place a shared lock. More than one process may hold a shared lock for a given file at a given time.
LOCK_EX Place an exclusive lock. Only one process may hold an exclusive lock for a given file at a given time.
LOCK_UN Remove an existing lock held by this process.
A call to flock() may block if an incompatible lock is held by another process. To make a non-blocking request, include
LOCK_NB (by ORing) with any of the above operations.
A single file may not simultaneously have both shared and exclusive locks.
Locks created by flock() are associated with an open file table entry. This means that duplicate file descriptors (created by, for example, fork(2) or dup(2)) refer to the same lock, and this lock may be modified or released using any of
these descriptors. Furthermore, the lock is released either by an explicit LOCK_UN oper