文件锁定程序不锁定文件

Posted

技术标签:

【中文标题】文件锁定程序不锁定文件【英文标题】:The file locking program is NOT LOCKING FILES 【发布时间】:2021-03-29 22:47:48 【问题描述】:

我已经制作了一个程序来在 c 程序中锁定咨询文件,下面是我为该任务编写的代码。

#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>


int controller, fsize;
char data[1000], ch;
int fd ;
                  /* l_type, l_whence, l_start, l_len, l_pid*/
struct flock fl = F_UNLCK, SEEK_SET,    0,       0,      0;
void Edit();
void Delete();
void Exit();
void Lock();


void main()


do

    clrscr();
    fd = open("demo.txt", "w");
    FILE *fp = fdopen("demo.txt", "w");
    if (fd != NULL)
    
        printf("Opening the file!\n");
        fd = fdopen("demo.txt", "r");
        printf("We can open the file in write mode, meaning no other process has lock enabled on it.");
        printf("Contents of the file are: \n\t");
        readfile(fd);
        fclose(fd);
    
    else if ((fd = fopen("demo.txt", "r"))!= NULL)
    
        fd = fopen("demo.txt", "r");
        printf("We can open the file in read mode, meaning some other process has locked the write permissions on it.");
        printf("Contents of the file are: \n\t");
        readfile(fd);
        fclose(fd);
    
    else
    
        printf("File does not exist.\n");
    
    printf("\n\t\t***** WELCOME USER! THIS IS A SIMPLE TEXT EDITOR *****");

    Lock();
    Operations();

while(1);


void Lock()

fd = fopen("demo.txt", "w");
fl.l_type = F_RDLCK;
fl.l_pid = getpid();
if (fcntl(fd, F_SETLK, &fl) == -1)

    printf("Can't set exclusive lock\n");

else if(fl.l_type!=F_UNLCK)

    printf("File has been exclusively locked by the process %d\n", fl.l_pid);
    fl.l_type = F_UNLCK;
    printf("File Unlocked! Other processes can execute on the file now.");

else

    printf("File is not locked\n");

fclose(fd);


void clrscr()

system("@cls||clear");


void Operations()

    printf("\n\n\tOperations you can perform here:\n\t\n");
    printf("\n\t1.ADD TO FILE\n\t2.DELETE THE FILE\n\t3.EXIT\n");
    printf("\n\tEnter your choice: ");
    scanf("%d",&controller);
    switch(controller)
    
    case 1:
        Add();
        break;
    case 2:
        remove("demo.txt");
        break;
    case 3:
        exit(0);
    




void Add()

    fd = fopen("demo.txt", "a");
    printf("Enter contents to store in file : \n");
    fgets(data, 1000, stdin);
    fputs(data, fd);
    fclose(fd);
    printf("Data added to the file successfully");
    fd = fopen("demo.txt", "r");
    readfile(fd);
    fclose(fd);




void readfile(FILE *fPtr)

char c = getc(fd);
while (c != EOF)

    printf("%c", c);
    c = getc(fd);


该程序是这样设计的,当在另一个进程中再次尝试打开该特定文件时,它会发出建议锁定。然后,新用户(来自新进程)可以编辑该文件。

但程序运行不正常。谁能帮我找出代码中的错误。我无法确定我做错了什么?

【问题讨论】:

你得到了一个锁,但一瞬间释放它(fclose(fd); 我无法打开和丢失文件。我应该在哪里打开文件,我应该在哪里关闭它?我想我应该在程序执行的整个过程中都有一个文件锁。或者至少在进程请求切换之前拥有它 然后在您即将退出时关闭文件(或者根本不关闭,让操作系统在程序退出时自动关闭它)。 您似乎创建了一个新用户帐户,以便再次提出 [previous closed question]() 而不解决导致问题被关闭的任何问题。我不认为这特别有帮助。您还应该尝试了解在 cmets 中发现的问题,包括文件描述符 (ints) 和标准库 FILE* 之间的混淆。如果你在编译时启用了警告,编译器也会产生警告信息。 --> previously closed question 【参考方案1】:

该程序甚至可以编译吗?咨询锁只是咨询性的,它们不会阻止其他进程打开文件或进行读/写。它们只会告诉您何时可以安全 使用文件描述符。所以使用它们的方法是,只有在你真正拥有锁时才进行读/写。

在这种情况下,“安全”的含义不能有一个通用的答案,它取决于用例。但通常意味着程序员已确保文件在获取锁之前和释放锁之后处于一致状态。

【讨论】:

我在笔记本电脑上使用 Windows,并为需要该程序在 linux 中工作的客户执行此操作。所以我使用了fcntl(因为它在linux中受支持,如果我使用win32 ..它对客户端没有用处)。我自己无法测试代码,我知道它有错误(因为我的代码跟踪记录)所以我只需要知道“这次”我做错了什么,并希望在提交给客户之前修复它 我应该如何实施咨询锁定先生?我什至尝试在网上搜索答案,但找不到答案。你至少能指引我正确的方向吗? 正确的方向是先了解文件流和低级文件描述符的区别,还要学习如何用-Wall编译。然后,使用flock 函数可能会更好地完成锁定。何时以及如何使用它取决于您的用例。通常,您必须确定您的原子操作是什么并锁定它。这需要编写干净代码的能力。我无法展示如何根据您的代码进行锁定,因为它很混乱。它首先需要市长清理。抱歉,我知道这听起来很精英,但你需要以正确的顺序积累编程经验。

以上是关于文件锁定程序不锁定文件的主要内容,如果未能解决你的问题,请参考以下文章

linux fcntl文件锁定超时

用于原子访问的文件锁定不起作用

如何用java实现文件操作的锁定及解锁

perf 是不是锁定配置文件用户空间互斥锁?

SylixOS文件记录锁使用

Java NIO基础3文件锁