如何将Linux包含flock的程序移植到Solaris

[入库:2005年8月19日] [更新:2007年3月25日]

本文简介:选择自 yayong 的 blog

如何将linux包含flock的程序移植到solaris

作者: badcoffee
email: blog.oliver@gmail.com
blog: http://blog.csdn.net/yayong
2005年5月
一个朋友的linux源程序中包含如下调用:

flock(fd, lock_un)

在linux上用gcc编译没有问题。但在solaris上编译时,出错:error: `lock_ex' undeclared (first use in this function)

显然,这是因为没有include正确的头文件,导致无法找到lock_ex宏的定义所致。

但是,朋友查了solaris的man,确信已经正确的include了头文件,下面是solaris man手册的说明:

man flock

sunos/bsd compatibility library functions flock(3ucb)

name
flock - apply or remove an advisory lock on an open file

synopsis
/usr/ucb/cc[ flag ... ] file ...
#include sys/file.h

int flock( fd, operation);
int fd, operation;

朋友已经grep了/usr/include/sys/file.h确实没有发现lock_ex宏的定义
难道真的是solaris的头文件出了问题?

从flock(3ucb)可以看出,该调用是属于man手册的section 3的3ucb字类中,
让我们看看3ucb是干什么用的:

man -s 3 intro

.......

(3ucb)
these functions constitute the source compatibility
(with bsd functions) library. it is implemented as a
shared object, libucb.so, but is not automatically
linked by the c compilation system. specify -lucb on
the cc command line to link with this library, which
is located in the /usr/ucb subdirectory. headers for
this library are located within /usr/ucbinclude. see
libucb(3libucb).

......

问题得到解决,原来3ucb的函数是为兼容bsd函数而保留下来的,
果然按照说明,在/usr/ucbinclude/sys/file.h中找到了这个宏。

通过这么一查,发现section 3里的子类真的很多,以后查man的时候一定要留心。
下面是常用的文件锁函数说明:

flock();
lockf();
fcntl();

flock()是从bsd中衍生出来的,但目前在大多数unix系统上都能找到,在单个主
机上flock()简单有效,但它不能在nfs上工作。perl中也有一个有点让人迷惑的
flock()函数,但却是在perl内部实现的。

fcntl()是唯一的符合posix标准的文件锁实现,所以也是唯一可移植的。它也同
时是最强大的文件锁——也是最难用的。在nfs文件系统上,fcntl()请求会被递
交给叫rpc.lockd的守护进程,然后由它负责和主机端的lockd对话,和flock()
不同,fcntl()可以实现记录层上的封锁。

lockf()只是一个简化了的fcntl()文件锁接口。

无论你使用哪一种文件锁,请一定记住在锁生效之前用sync来更新你所有的文件
输入/输出。

lock(fd);
write_to(some_function_of(fd));
flush_output_to(fd); /* 在去锁之前一定要冲洗输出 */
unlock(fd);
do_something_else; /* 也许另外一个进程会更新它 */
lock(fd);
seek(fd, somewhere); /* 因为原来的文件指针已不安全 */
do_something_with(fd);
...
一些有用的fcntl()封锁方法(为了简洁略去错误处理):


#include
#include
read_lock(int fd) /* 整个文件上的一个共享的文件锁 */
{
fcntl(fd, f_setlkw, file_lock(f_rdlck, seek_set));
}

write_lock(int fd) /* 整个文件上的一个排外文件锁 */ {
fcntl(fd, f_setlkw, file_lock(f_wrlck, seek_set)); }
append_lock(int fd) /* 一个封锁文件结尾的锁,
其他进程可以访问现有内容 */ {
fcntl(fd, f_setlkw, file_lock(f_wrlck, seek_end));
}

前面所用的file_lock函数如下:

struct flock* file_lock(short type, short whence)
{
static struct flock ret ;
ret.l_type = type ;
ret.l_start = 0 ;
ret.l_whence = whence ;
ret.l_len = 0 ;
ret.l_pid = getpid() ;
return &ret ;
}


而且根据solaris flock(3ucb)的说明,该函数不是线程安全的,因此,强烈建议
用fcntl来取代flock,毕竟它才是posix的标准。另外,对于很多大容量邮件系统的
应用,邮箱是通过nfs共享的,更不能用flock来做了。

在solaris上, 可以使用下面的代码来实现一个新的flock:

#ifndef lock_sh
#define lock_sh 1 /* shared lock */
#define lock_ex 2 /* exclusive lock */
#define lock_nb 4 /* don't block when locking */
#define lock_un 8 /* unlock */
#endif
#ifdef posix
#include

/*
* this function emulates a subset of flock()
*/
int emul_flock(fd, cmd)
int fd, cmd;
{ struct flock f;

memset(&f, 0, sizeof (f));

if (cmd & lock_un)
f.l_type = f_unlck;
if (cmd & lock_sh)
f.l_type = f_rdlck;
if (cmd & lock_ex)
f.l_type = f_wrlck;

return fcntl(fd, (cmd & lock_nb) ? f_setlk : f_setlkw, &f);
}

#define flock(f,c) emul_flock(f,c)
#endif

本文关键:如何将Linux包含flock的程序移植到Solaris
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top