⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 7-3.c

📁 《Linux应用开发技术详解》附书光盘中的例程。
💻 C
字号:
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
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);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -