#include
#include
#include
#include
int main(int argc, char *argv[])
{
int fd;
struct flock fl;
fd = open("testfile", O_RDWR);
if (fd == -1)
/* Handle error */;
/* Make a non-blocking request to place a write lock on bytes 100-109 of testfile */
{
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 100;
fl.l_len = 10;
}
if (fcntl(fd, F_SETLK, &fl) == -1)
{
if (errno == EACCES || errno == EAGAIN) {
printf("Already locked by another process\n");
/* We can't get the lock at the moment */
} else {
/* Handle unexpected error */;
}
} else { /* Lock was granted... */
/* Perform I/O on bytes 100 to 109 of file */
/* Unlock the locked bytes */
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
fl.l_start = 100;
fl.l_len = 10;
if (fcntl(fd, F_SETLK, &fl) == -1)
/* Handle error */;
}
exit(EXIT_SUCCESS);
}
在我的机子上没有通过编译,不知道为什么,我不太懂啊。
我的GCC的版本是:在此命令gcc -v后的显示如下:
cwqing1973@cwqing1973-desktop:~$ gcc -v
使用内建 specs。
目标:x86_64-linux-gnu
配置为:../src/configure -v --enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --enable-nls --with-gxx-include-dir=/usr/include/c++/4.2 --program-suffix=-4.2 --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-mpfr --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
线程模型:posix
gcc 版本 4.2.3 (Ubuntu 4.2.3-2ubuntu7)
cwqing1973@cwqing1973-desktop:~$
谢谢了。
mech 于 2008-09-08 15:12:44发表:
#include没内容啊?