操作系统文件管理实验

1. 实验目的

用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程,从而对各种文件操作命令的实质内容和执行过程有比较深入的了解。

2. 实验内容与要求

要求设计一个 n个用户的文件系统,每次用户可保存m个文件,用户在一次运行中只能打开一个文件,对文件必须设置保护措施,提供以下有关操作命令:create、delete、open、close、read、write以及在此基础上增加 2~3个其它文件操作命令(如移动读写指针,改变文件属性,更换文件名,改变文件保护级别),并加以实现。

3. 流程图与模块调用

在这里插入图片描述

4. 实验分析

(1)文件的数据结构

class file:
    def __init__(self, uid, fid, name, status):
        self.uid = uid          #文件的UID
        self.fid = fid          #文件的FID
        self.name = name        #文件名
        self.status = status    #文件的状态

(2)文件目录展示函数

def Lfile(arr):
    str = '更新后的文件目录为:\n'
    for index, item in enumerate(arr):
        str += '{}、文件名:{} \t文件FID:{} \t文件状态:{}\n'.format((index + 1), item.name, item.fid, item.status)
    print(str)   

(3)主函数

def main():
    print("有以下用户:A B C D E F G H I J\n")
    option = input('请输入用户名:')
    print('操作目录:0、exit\t\t1、creat\t2、delest\n\t\t3、open\t\t4、close\t5、read\n\t\t6、write\n')
    if option in mfd:
        files = []  # 存放属于该用户的文件
        for item in ufd:
            if item.uid == option:
                files.append(item)

        instruct = int(input('请输入指令:'))
        while True:
            if instruct==0:
                print("系统退出")
                break
            elif instruct == 1:           #创建新的文件
                if len(files) < 10:
                    a = input('请输入文件名:')
                    p = file(option, len(files) + 1, a, 'close')
                    ufd.append(p)
                    files.append(p)
                    Lfile(files)
                    instruct = int(input('请输入指令:'))
                else:
                    print('文件库已满')
                    instruct = int(input('请输入指令:'))
            elif instruct == 2:             #删除文件
                b = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == b:
                        files.remove(item)
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            elif instruct == 3:            #打开文件
                c = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == c:
                        item.status = 'open'
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            elif instruct == 4:              #关闭文件
                d = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == d:
                        item.status = 'close'
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            elif instruct == 5:             #读文件
                e = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == e:
                        item.status = 'read'
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            elif instruct == 6:             #写文件
                f = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == f:
                        item.status = 'write'
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            else:
                print("指令错误")
                break
    else:
        print("查无此人,系统退出")

5. 总代码

# coding=gbk
mfd = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']
ufd = []


class file:
    def __init__(self, uid, fid, name, status):
        self.uid = uid
        self.fid = fid
        self.name = name
        self.status = status


def Lfile(arr):
    str = '更新后的文件目录为:\n'
    for index, item in enumerate(arr):
        str += '{}、文件名:{} \t文件FID:{} \t文件状态:{}\n'.format((index + 1), item.name, item.fid, item.status)
    print(str)       

def main():
    print("有以下用户:A B C D E F G H I J\n")
    option = input('请输入用户名:')
    print('操作目录:0、exit\t\t1、creat\t2、delest\n\t\t3、open\t\t4、close\t5、read\n\t\t6、write\n')
    if option in mfd:
        files = []  # 存放属于该用户的文件
        for item in ufd:
            if item.uid == option:
                files.append(item)

        instruct = int(input('请输入指令:'))
        while True:
            if instruct==0:
                print("系统退出")
                break
            elif instruct == 1:           #创建新的文件
                if len(files) < 10:
                    a = input('请输入文件名:')
                    p = file(option, len(files) + 1, a, 'close')
                    ufd.append(p)
                    files.append(p)
                    Lfile(files)
                    instruct = int(input('请输入指令:'))
                else:
                    print('文件库已满')
                    instruct = int(input('请输入指令:'))
            elif instruct == 2:             #删除文件
                b = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == b:
                        files.remove(item)
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            elif instruct == 3:            #打开文件
                c = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == c:
                        item.status = 'open'
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            elif instruct == 4:              #关闭文件
                d = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == d:
                        item.status = 'close'
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            elif instruct == 5:             #读文件
                e = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == e:
                        item.status = 'read'
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            elif instruct == 6:             #写文件
                f = int(input('请输入文件的FID:'))
                for item in files:
                    if item.fid == f:
                        item.status = 'write'
                        Lfile(files)
                instruct = int(input('请输入指令:'))
            else:
                print("指令错误")
                break
    else:
        print("查无此人,系统退出")

if __name__ == '__main__':
    main()

6. 运行情况

(1)创建、删除、打开文件
在这里插入图片描述
(2)读取、写入、退出文件
在这里插入图片描述
(3)指令错误
在这里插入图片描述

  • 8
    点赞
  • 99
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

啊噗呲咔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值