The file module has functions lock files across several operations that are almost exact equivalents to POSIX API functions; the primary difference is the advantage of valuable return error codes to the application.
File Module Function
POSIX API Equivalent
For more information about and an example of using file locking, see section 10-3-5 “Atomic File Operations Using File Lock”.