微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

文件锁定程序未锁定文件

如何解决文件锁定程序未锁定文件

我制作了一个程序来在 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};
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);
}
}

程序被制作成当有另一次尝试在另一个进程中打开该特定文件时,它会发出建议锁定。然而,新用户(来自新进程)可以编辑文件

但是程序运行不正常。任何人都可以帮我找到代码中的错误。我无法确定我做错了什么?

解决方法

那个程序还能编译吗?建议锁只是建议性的,它们不会阻止其他进程打开文件或进行读/写。它们只告诉您何时使用文件描述符安全。因此,使用它们的方法是仅在您实际拥有锁时才进行读/写操作。

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?