📄 apr-tutorial-6.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><HTML><HEAD> <META NAME="GENERATOR" CONTENT="LinuxDoc-Tools 0.9.21"> <TITLE>libapr(apache portable runtime) programming tutorial: file lock</TITLE> <LINK HREF="apr-tutorial-7.html" REL=next> <LINK HREF="apr-tutorial-5.html" REL=previous> <LINK HREF="apr-tutorial.html#toc6" REL=contents></HEAD><BODY><A HREF="apr-tutorial-7.html">Next</A><A HREF="apr-tutorial-5.html">Previous</A><A HREF="apr-tutorial.html#toc6">Contents</A><HR><H2><A NAME="s6">6.</A> <A HREF="apr-tutorial.html#toc6">file lock</A></H2><P>When we intend to lock file among processes, we can use apr_file_lock(). Historically, there are several confusions about file lock APIs on Unix. Thus, it is very useful that libapr provides one simple API.</P><P>/* excerpted from apr_file_io.h */<BLOCKQUOTE><CODE><PRE>APR_DECLARE(apr_status_t) apr_file_lock(apr_file_t *thefile, int type);APR_DECLARE(apr_status_t) apr_file_unlock(apr_file_t *thefile);</PRE></CODE></BLOCKQUOTE></P><P>apr_file_lock() has two arguments. One is apr_file_t object. The other is flag, by which we specify the lock type. The lock type is either APR_FLOCK_SHARED or APR_FLOCK_EXCLUSIVE. We can use the former as a readable lock, and the latter as a writable lock. To unlock the file, we call apr_file_unlock(). Or, calling apr_file_close() implicitly does unlock. Take a look at <A HREF="../sample/flock-sample.c">flock-sample.c</A> about the usage. </P><P>Additionally, we can use a bitwised-or flag APR_FLOCK_NONBLOCK. Without APR_FLOCK_NONBLOCK flag, calling apr_file_lock() would block. With APR_FLOCK_NONBLOCK, if apr_file_lock() can't lock file, it returns an error code, APR_EAGAIN.</P><P>We should compare apr_file_lock() return value with APR_SUCCESS. If the value is APR_SUCCESS, the file was successfully locked. Othewrise, we failed to lock the file.</P><HR><A HREF="apr-tutorial-7.html">Next</A><A HREF="apr-tutorial-5.html">Previous</A><A HREF="apr-tutorial.html#toc6">Contents</A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -