如何使用“锁定和解锁pid文件”;fcntl();
我在网上甚至stackoverflow上进行了研究,以找到一个使用如何使用“锁定和解锁pid文件”;fcntl();,c,linux,fcntl,C,Linux,Fcntl,我在网上甚至stackoverflow上进行了研究,以找到一个使用fcntl()锁定和解锁pid文件“/var/run/myapp.pid”的例子,但我没有找到一个明确的例子 您能给我举一个使用fcntl()锁定和解锁pid文件的例子吗 锁定不应被阻止(如果文件已被锁定)当您标记Linux时,逐字格式man lock(我强调): 在Linux上,lock()只是fcntl(2)锁定之上的一个接口。许多其他系统以这种方式实现了锁,但请注意,POSIX.1-2001没有明确说明锁与fcntl锁之间的
fcntl()
锁定和解锁pid文件“/var/run/myapp.pid”
的例子,但我没有找到一个明确的例子
您能给我举一个使用fcntl()
锁定和解锁pid文件的例子吗
锁定不应被阻止(如果文件已被锁定)当您标记Linux时,逐字格式
man lock
(我强调):
在Linux上,lock()只是fcntl(2)锁定之上的一个接口。许多其他系统以这种方式实现了锁,但请注意,POSIX.1-2001没有明确说明锁与fcntl锁之间的关系。便携式应用程序应该避免混合调用这些接口
因此,查找当前的glibc源(eglibc-2.11.3/io/lockf.c
)可以使用fcntl()
实现锁定,如下所示:
/* Copyright (C) 1994,1996,1997,1998,2000,2003 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
/* lockf is a simplified interface to fcntl's locking facilities. */
int
lockf (int fd, int cmd, off_t len)
{
struct flock fl;
memset ((char *) &fl, '\0', sizeof (fl));
/* lockf is always relative to the current file position. */
fl.l_whence = SEEK_CUR;
fl.l_start = 0;
fl.l_len = len;
switch (cmd)
{
case F_TEST:
/* Test the lock: return 0 if FD is unlocked or locked by this process;
return -1, set errno to EACCES, if another process holds the lock. */
fl.l_type = F_RDLCK;
if (__fcntl (fd, F_GETLK, &fl) < 0)
return -1;
if (fl.l_type == F_UNLCK || fl.l_pid == __getpid ())
return 0;
__set_errno (EACCES);
return -1;
case F_ULOCK:
fl.l_type = F_UNLCK;
cmd = F_SETLK;
break;
case F_LOCK:
fl.l_type = F_WRLCK;
cmd = F_SETLKW;
break;
case F_TLOCK:
fl.l_type = F_WRLCK;
cmd = F_SETLK;
break;
default:
__set_errno (EINVAL);
return -1;
}
/* lockf() is a cancellation point but so is fcntl() if F_SETLKW is
used. Therefore we don't have to care about cancellation here,
the fcntl() function will take care of it. */
return __fcntl (fd, cmd, &fl);
}
<代码> /* Copyright(C)1994年,1996年,1997年,1998年,2000年,2003年,自由软件基金会,Inc.
此文件是GNUC库的一部分。
GNUC库是自由软件;您可以重新分发它和/或
根据GNU小公众的条款对其进行修改
自由软件基金会发布的许可证;任何一个
许可证的2.1版,或(由您选择)任何更高版本。
分发GNU C库是希望它会有用,
但无任何保证;甚至没有任何关于
适销性或适合某一特定目的。见GNU
有关更多详细信息,请参阅较低的通用公共许可证。
您应该已经收到GNU Lesser General Public的副本
许可证以及GNUC库;如果没有,写信给免费的
软件基础,公司,59庙广场,套房330,波士顿,MA
02111-1307美国*/
#包括
#包括
#包括
#包括
#包括
/*lockf是fcntl锁定设施的简化接口*/
int
锁(内部fd、内部cmd、关闭透镜)
{
struct-flock-fl;
memset((char*)&fl,'\0',sizeof(fl));
/*lockf始终相对于当前文件位置*/
fl.l_whence=查找当前;
fl.l_开始=0;
fl.l_len=len;
开关(cmd)
{
案例F_测试:
/*测试锁:如果FD被此进程解锁或锁定,则返回0;
如果另一个进程持有锁,则返回-1,将errno设置为EACCES*/
fl.l_类型=F_RDLCK;
如果(u fcntl(fd、F_GETLK和fl)<0)
返回-1;
if(fl.l_type==F|u UNLCK | fl.l_pid==uu_getpid())
返回0;
__设置错误号(EACCES);
返回-1;
案例F_ULOCK:
fl.l_类型=F_解锁;
cmd=F_SETLK;
打破
案例F_锁:
fl.l_类型=F_WRLCK;
cmd=F_SETLKW;
打破
案例F_t锁定:
fl.l_类型=F_WRLCK;
cmd=F_SETLK;
打破
违约:
__设置错误号(EINVAL);
返回-1;
}
/*lockf()是一个取消点,但如果F_SETLKW是,则fcntl()也是一个取消点
用过了,所以我们不必在意取消,
函数将处理它*/
返回fcntl(fd、cmd和fl);
}
首先需要几个MOD进行编译:
- 将
替换为\u fcntl
fcntl
- 用
errno=
- 将对
的调用替换为对memset()
变量的适当分配李>struct fcntl
lockf()
函数,例如,它的名称将是async\u lockf()
。然后我在这个函数中复制上面函数的内容,并进行您指定的更改。然后新函数async\u lockf()
将是一个异步信号保存函数,然后我可以在sigaction处理程序中使用它。这是真的吗?对于memset()
:我将其更改为:struct flock fl={0}
@MOHAMED:似乎是这样,是的。但我不想把它称为类似于*lock*
,因为它实际上并不是指lock()
,而是模仿它的行为。但是:您知道这是GPL代码,对吗?GPL没有问题。我也在开发一个开源项目(GPL)。版权归keptSo所有,最后我们为解锁文件提供了一个解决方案,该文件带有一个同步信号保存,以便在sigaction处理程序中使用。非常感谢您的支持。这可能是我第一个问题的最终答案