操作系统实验四

操作系统实验连接

实验一 进程调度算法
实验二 预防死锁-银行家算法
实验三 分区分配算法+请求式分页存储管理算法
实验四 文件系统


文件系统

一、目的与要求

目的:

文件系统是操作系统的一个重要组成部分,也是与用户关系极为密切的部分。学生应独立的用高级语言编写和调试一个简单的文件系统,模拟文件管理的工作过程,从而对各种文件操作命令的实质内容和执行过程有比较深入的了解,掌握它们的实施方法,加深对教材中有关内容的理解。

要求:

(1)设计一个n个用户的文件系统,每个用户最多可保存m个文件。
(2)限制用户在一次运行中只能打开一个文件。
(3)系统应能检查输入命令的正确性,出错时要能显示出错原因。
(4)对文件必须设置保护措施,如只能执行,允许读,允许写等。在每次打开文件时,根据本次打开的要求,再次设置保护级别,即可有二级保护。
(5)对文件的操作至少应有下面几条命令:
create 建立文件
delete 删除文件
open 打开文件
close 关闭文件
read 读文件
write 写文件

二、实验内容

(1)本次实验设计一个共有10个用户的文件系统,每个用户最多可保存10个文件,一次运行过程中用户可同时打开5个文件。
(2)程序采用二级文件目录,即设置了主文件目录(MFD)和用户文件目录(UFD)。前者应包含文件主(即用户)及他们的目录区指针;后者应给出每个文件主占有的文件目录,即文件名、保护码、文件长度以及它们存放的位置等。另外为打开文件设置了运行文件目录(AFD),在文件打开时应填入打开文件号,本次打开保护码和读写指针等。
(3)为了便于实现,对文件的读写作了简化,在执行读写命令时,只修改读写指针,并不进行实际文件的读写操作。

数据结构:

MFD和UFD

MFD和UFD

AFD

AFD

结果显示

在这里插入图片描述
在这里插入图片描述

代码

/*
 *模拟文件管理
 */
#include<iostream>
#include<malloc.h>
#include<string.h>
using namespace std ;

/*
 *定义文件数据结构
 */
typedef struct file
{
    char file_name[20] ;
    bool file_protect[3] ;
    bool open_file_protect[3] ; //仅在文件打开时有效
    int  read , write ; //定义为读写指针
    int  file_length ;
    struct file *next ;
} File ;

/*
 *用户与文件的映射
 */
typedef struct x_map
{
    char userName[20] ;
    File *file ;
    struct x_map *next ;
} Map ;

/*
 *定义主文件目录
 */
typedef struct mfd
{
    Map *head , *tail ;
} MFD ;

/*
 *打开文件目录
 */
typedef struct afd
{
  File *head , *tail ;
  int max_open ;
  int current_open ;
} AFD ;

/*
 *进行用户的初始化
 */
void initUser(MFD *mfd)
{
    cout<<"请输入用户名 :"<<endl;
    //初始化十个不同用户
    for(int i = 0 ; i < 10 ; i++)
    {
        Map *m ;
        m = (Map*)malloc(sizeof(Map)) ;
        if(m == NULL)
        {
            exit(0) ;
        }
        cin>>m->userName ;
        m->file = NULL ;
        m->next = NULL ;
        if(mfd->head == NULL)
        {
            mfd->head = mfd->tail = m ;
        }
        else
        {
            mfd->tail->next = m ;
            mfd->tail = m ;
        }
    }
    cout<<endl;
}

/*
 *进行系统用户的输出
 */
void displayUser(MFD *mfd)
{
    Map *m = NULL ;
    m = mfd->head;
    cout<<"user : " ;
    while(m)
    {
        cout<<m->userName<<" " ;
        m = m->next ;
    }
    cout<<endl ;
}

/*
 *进行用户的查找,找到则返回用户映射指针
 */
Map * queryUser(char userName[] , MFD *mfd)
{
    Map *m = NULL ;
    m = mfd->head ;
    while(m)
    {
        if(strcmp(userName , m->userName) == 0)
        {
            return m ;
        }
        m = m->next ;
    }
    return NULL ;
}

/*
 *进行用户文件的显示
 */
void displayUserFile(Map *user)
{
    cout<<"The fileList of "<<user->userName<<endl ;
    File *file = NULL ;
    file = user->file ;
    while(file)
    {
        cout<<file->file_name<<" "<<file->file_protect[0]<<" "<<file->file_protect[1]<<" "<<file->file_protect[2]<<" "<<file->file_length<<endl ;
        file = file->next ;
    }
}

/*
 *显示打开的文件
 */
void displayOpenFile(AFD *afd , Map *user)
{
    cout<<"The open file of "<<user->userName<<" : "<<endl ;
    File *file ;
    file = afd->head ;
    while(file)
    {
        cout<<file->file_name<<" "<<file->file_protect[0]<<" "<<file->file_protect[1]<<" "<<file->file_protect[2]<<" "<<file->file_length<<" " ;
        cout<<"readcout : "<<file->read<<" writecout : "<<file->write<<endl ;
        file = file->next ;
    }
}

/*
 *进行文件的创建操作
 *成功则返回true , 否则返回false
 */
bool createFile(Map *user , char file_name[] , bool file_protect[3] , int file_length)
{
    File *file ;
    file = (File*)malloc(sizeof(File)) ;
    if(file == NULL)
    {
        return false ;
    }

    //进行文件的初始化
    strcpy(file->file_name , file_name) ;
    file->file_protect[0] = file_protect[0] ;
    file->file_protect[1] = file_protect[1] ;
    file->file_protect[2] = file_protect[2] ;
    file->file_length = file_length ;
    file->read = file->write = 0 ;
    file->next = NULL ;

    if(user->file == NULL)
    {
        user->file = file ;
    }
    else
    {
       File *op  , *preOp = NULL ;
       op = user->file ;
       //遍历AFD,查找是否存在同名文件
       while(op)
       {
           if(strcmp(op->file_name , file->file_name) == 0)
           {
               cout<<"文件名为 "<<file->file_name<<" 的文件已经存在 ! "<<endl ;
               return false ;
           }
        preOp = op ;
        op = op->next ;
       }
       preOp->next = file ;//循环结束op为空,op的前一个preOp指向末尾文件
    }
}

/*
 *进行文件删除操作
 */
bool deleteFile(Map *user , char file_name[] , AFD *afd)
{
  File *file = NULL , *prefile = NULL , *temp ;
  file = afd->head ;
  //在打开文件中查找
  while(file)
  {
      if(strcmp(file_name , file->file_name) == 0)
      {
          cout<<"\""<<file_name<<"\" 正在打开中,请关闭它之后删除 ! \n" ;
          return false ;
      }
      file = file->next ;
  }
  file = user->file ;
  //在文件中进行查找
  while(file)
  {
    if(strcmp(file_name , file->file_name) == 0)
    {
       if(file == user->file)//如果UFD中第一个文件就是要删除的文件
       {
           temp = file ;
           user->file = file->next ;
       }
       else
       {
           temp = file ;
           prefile->next = file->next ;
       }
       delete temp ;
       return true ;
    }
    prefile = file ;
    file = file->next ;
  }
  if(prefile->next == NULL)
  {
      cout<<"用户 "<<user->userName<<" 没有文件 \""<<file_name<<"\""<<endl;
  }
  return false ;
}

/*
 *进行文件打开操作
 */
bool openFile(Map *user , char file_name[] , AFD *afd , bool open_file_protect[])
{
   File *file = NULL ;
   file = user->file ;
   while(file)
   {
       if(strcmp(file->file_name , file_name) == 0)
       {
           break ;
       }
       file = file->next ;
   }
   if(file)
   {
       File *xfile ;
       xfile = (File*)malloc(sizeof(File)) ;
       if(xfile == NULL)
       {
           return false ;
       }
       *xfile = *file ;
       //根据文件的权限进行打开权限的赋值
       if(xfile->file_protect[0] >= open_file_protect[0])
       {
           xfile->open_file_protect[0] = open_file_protect[0] ;
       }
       else
       {
            cout<<"没有读权利 ! "<<endl;
            return false ;
       }
       if(xfile->file_protect[1] >= open_file_protect[1])
       {
           xfile->open_file_protect[1] = open_file_protect[1] ;
       }
       else
       {
            cout<<"没有写权利 ! "<<endl;
            return false ;
       }
        if(xfile->file_protect[2] >= open_file_protect[2])
       {
           xfile->open_file_protect[2] = open_file_protect[2] ;
       }
       else
       {
            cout<<"没有执行权利 ! "<<endl;
            return false ;
       }
       xfile->next = NULL ;
       if(afd->head == NULL)
       {
           afd->head = afd->tail = xfile ;
           afd->current_open += 1 ;
       }
       else if(afd->current_open < afd->max_open)
       {
           afd->tail->next = xfile ;
           afd->tail = xfile ;
           afd->current_open += 1 ;
       }
       else
       {
           cout<<"打开文件太多 ! " <<endl ;
           return false ;
       }
   }
   else
   {
       cout<<"文件 "<<file_name<<" 不存在 !"<<endl ;
       return false ;
   }
}

/*
 *进行读操作
 */
bool readFile(AFD *afd , char file_name[])
{
    File *file = NULL ;
    file = afd->head ;
    while(file)
    {
        if(strcmp(file->file_name , file_name) == 0)
        {
            if(file->open_file_protect[0])
            {
                file->read++ ;
                return true ;
            }
            else
            {
                cout<<"没有读权利 ! \n"<<endl ;
                return false ;
            }
        }
        file = file->next ;
    }
  cout<<"没有这个文件 ! "<<endl ;
  return false ;
}

/*
 *进行文件的写操作
 */
bool writeFile(AFD *afd , char file_name[])
{
    File *file = NULL ;
    file = afd->head ;
    while(file)
    {
        if(strcmp(file->file_name , file_name) == 0)
        {
            if(file->open_file_protect[1])
            {
                file->write++ ;
                return true ;
            }
            else
            {
                cout<<"没有写权利 ! \n"<<endl ;
                return false ;
            }
        }
        file = file->next ;
    }
  cout<<"no such file ! "<<endl ;
  return false ;
}

/*
 *关闭文件
 */
 bool closeFile(AFD *afd , char file_name[])
{
    File *file = NULL  , *preFile = NULL  , *temp = NULL ;
    //在打开文件链表中进行查找
    file = afd->head ;
    while(file)
    {
        if(strcmp(file->file_name , file_name) == 0)
        {
            if(file == afd->head)
            {
                if(file == afd->tail)
                {
                    temp = file ;
                    afd->head = afd->tail = NULL ;
                }
                else
                {
                    temp = file ;
                    afd->head = file->next ;
                }
            }
            else if(file == afd->tail)
            {
                temp = file ;
                preFile->next = NULL ;
                afd->tail = preFile ;
            }
            else
            {
                temp =file ;
                preFile->next = file->next ;
            }
            delete temp ;
            return true ;
        }
        preFile = file ;
        file = file->next ;
    }
    cout<<"这个文件不存在 ! "<<endl ;
    return false ;
}

/*
 *测试主函数
 */
int main()
{
    MFD *mfd ;
    mfd = (MFD*)malloc(sizeof(MFD)) ;
    if(mfd == NULL)
    {
        exit(0) ;
    }
    mfd->head = mfd->tail = NULL ;
    initUser(mfd) ;
    displayUser(mfd) ;

    char userName[20] ;
    while(true)
    {
        cout<<"请选择用户登录 : " ;
        cin>>userName ;
        Map *user ;
        user = queryUser(userName , mfd) ;
        if(user == NULL)
        {
            cout<<"No such user ! "<<endl;
        }
        else
        {
          //为用户初始化打开文件目录
          AFD *afd ;
          afd = (AFD*)malloc(sizeof(AFD)) ;
          if(afd == NULL)
          {
              cout<<"用户文件夹中没有文件 ! "<<endl ;
              exit(0) ;
          }
          afd->head = afd->tail = NULL ;
          afd->max_open = 5 ;
          afd->current_open = 0 ;

          int command ;
          char file_name[20] ;
          bool file_protect[3] ;
          bool open_file_protect[3] ;
          int file_length ;
          while(true)
          {
              cout<<"******** 请选择操作执行: *******"<<endl;
              cout<<"********** 1 : create **********"<<endl;
              cout<<"********** 2 : delete **********"<<endl;
              cout<<"********** 3 : open ************"<<endl;
              cout<<"********** 4 : close ***********"<<endl;
              cout<<"********** 5 : read  ***********"<<endl;
              cout<<"********** 6 : write ***********"<<endl;
              cout<<"********** 0 : exit  ***********"<<endl;
              cout<<userName<<">>" ;
              cin>>command ;
              //输入命令进行操作
              if(command == 1)
              {
                  cout<<"请输入文件名 文件保护码(读 写 执行) 文件长度 : " ;
                  cin>>file_name>>file_protect[0]>>file_protect[1]>>file_protect[2]>>file_length ;
                  createFile(user , file_name , file_protect , file_length) ;
                  displayUserFile(user) ;
              }
              else if(command == 2)
              {
                  cout<<"请输入你想删除文件的文件名 : " ;
                  cin>>file_name ;
                  deleteFile(user , file_name , afd) ;
                  displayUserFile(user) ;
              }
              else if(command == 3)
              {
                  cout<<"请输入你想打开文件的文件名 文件保护码(读 写 执行): " ;
                  cin>>file_name>>open_file_protect[0]>>open_file_protect[1]>>open_file_protect[2] ;
                  openFile(user , file_name , afd , open_file_protect) ;
                  displayOpenFile(afd , user) ;
              }
              else if(command == 4)
              {
                  cout<<"请输入你想要关闭文件的文件名 : " ;
                  cin>>file_name ;
                  closeFile(afd , file_name) ;
                  displayOpenFile(afd , user) ;
              }
              else if(command == 5)
              {
                  cout<<"请输入你想读文件的文件名 : " ;
                  cin>>file_name ;
                  readFile(afd , file_name) ;
                  displayOpenFile(afd , user) ;
              }
              else if(command == 6)
              {
                  cout<<"请输入你想写文件的文件名 : " ;
                  cin>>file_name ;
                  writeFile(afd , file_name) ;
                  displayOpenFile(afd , user) ;
              }
              else if(command == 0)
              {
                  break ;
              }
              else
              {
                  cout<<"没有这个选择 \""<< command <<"\""<<endl ;
              }
          }
        }
    }
    return 0 ;
}

设计内容: (1) 设计一个10个用户的文件系统。每个用户最多可以保存10个文件,一次运行用户可打开多个文件。 (2) 程序采用二级文件目录。(即设置主目录(MFD)和用户文件目录(UFD))。另外,可打开文件设置指针。 (3) 为了方便实现,对文件的读写作了简化。在执行读写命令时,只需改读写指针。并不进行实际的读写操作。 (4) 实现的基本功能主要包括:改变目录(CD),创建目录(MD),显示目录(DIR),删除目录(RD),打开全部文件(openall),打开单个文件(open),建立一个文件(create),删除一个文件(delete),写文件(write),读文件(read),改文件的保护码(change),退出(exit)等。 要求: 考虑特殊情况如:各个命令对全路径和相对路径的支持、目录不存在时,给出错误信息、不能用cd进入文件、命令之中不能有空格(如 ex it,给出错误提示)、相对路径的解析、路径中的空格剔除、新建目录或文件时的问题、重名问题、目录或文件的名字长度限制、目录或文件的名字中包含不合法字符(注意空格)、删除目录或文件时的问题、删除不存在的文件或目录给出错误提示、删除目录时目录不为空(如果该目录为空,则可删除,否则给出是否做删除提示,删除操作将该目录下的全部文件和子目录都删除)、进入到某个目录下,却要删除本目录或上级目录、不能用delete删除目录、不能用RD删除文件等都要考虑在内。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值