簡單的檔案系統模擬C/C++(作業系統)
一、目的與要求
- 目的
檔案系統是作業系統的一個重要組成部分,也是與使用者關係極為密切的部分。學生應獨立的用高階語言編寫和除錯一個簡單的檔案系統,模擬檔案管理的工作過程,從而對各種檔案操作命令的實質內容和執行過程有比較深入的瞭解,掌握它們的實施方法,加深對教材中有關內容的理解。‘
- 要求
(1) 設計一個n個使用者的檔案系統,每個使用者最多可儲存m個檔案。
(2) 限制使用者在一次執行中只能開啟一個檔案。
(3) 系統應能檢查輸入命令的正確性,出錯時要能顯示出錯原因。
(4) 對檔案必須設定保護措施,如只能執行,允許讀,允許寫等。在每
次開啟檔案時,根據本次開啟的要求,再次設定保護級別,即可有二級保護。
(5) 對檔案的操作至少應有下面幾條命令:
create 建立檔案
delete 刪除檔案
open 開啟檔案
close 關閉檔案
read 讀檔案
write 寫檔案
二、內容
(1) 設計一個共有10個使用者的檔案系統,每個使用者最多可儲存10個檔案,一次執行過程中使用者可同時開啟5個檔案。
(2) 程式採用二級檔案目錄,即設定了主檔案目錄(MFD)和使用者檔案目錄(UFD)。前者應包含檔案主(即使用者)及他們的目錄區指標;後者應給出每個檔案主佔有的檔案目錄,即檔名、保護碼、檔案長度以及它們存放的位置等。另外為開啟檔案設定了執行檔案目錄(AFD),在檔案開啟時應填入開啟檔案號,本次開啟保護碼和讀寫指標等。
(3) 為了便於實現,對檔案的讀寫作了簡化,在執行讀寫命令時,只修改讀寫指標,並不進行實際檔案的讀寫操作。
主要資料結構:
① 主檔案目錄(MFD)和使用者檔案目錄(UFD)
② 開啟檔案目錄(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) ;
/*
*進行系統使用者的輸出
*/
void displayUser(MFD *mfd) ;
/*
*進行使用者的查詢,找到則返回使用者對映指標
*/
Map * queryUser(char userName[] , MFD *mfd) ;
/*
*進行檔案的建立操作
*成功則返回true , 否則返回false
*/
bool createFile(Map *user , char file_name[] , bool file_protect[3] , int file_length) ;
/*
*進行檔案刪除操作
*/
bool deleteFile(Map *user , char file_name[] , AFD *afd) ;
/*
*進行檔案開啟操作
*/
bool openFile(Map *user , char file_name[] , AFD *afd , bool open_file_protect[]) ;
/*
*進行讀操作
*/
bool readFile(AFD *afd , char file_name[]) ;
/*
*進行檔案的寫操作
*/
bool writeFile(AFD *afd , char file_name[]) ;
/*
*關閉檔案
*/
bool closeFile(AFD *afd , char file_name[]) ;
/*
*進行使用者檔案的顯示
*/
void displayUserFile(Map *user) ;
/*
*顯示開啟的檔案
*/
void displayOpenFile(AFD *afd , Map *user) ;
/*
*測試主函式
*/
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<<"Please choose user to login : " ;
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<<"The memory is not enough ! "<<endl ;
exit(0) ;
}
afd->head = afd->tail = NULL ;
afd->max_open = 5 ;
afd->current_open = 0 ;
char command[20] ;
char file_name[20] ;
bool file_protect[3] ;
bool open_file_protect[3] ;
int file_length ;
while(true)
{
cout<<userName<<">>" ;
cin>>command ;
//輸入命令進行操作
if(strcmp(command , "create") == 0)
{
cout<<"Please file (file_name file_protect file_length) : " ;
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(strcmp(command , "delete") == 0)
{
cout<<"Please input the file's name you want to delete : " ;
cin>>file_name ;
deleteFile(user , file_name , afd) ;
displayUserFile(user) ;
}
else if(strcmp(command , "open") == 0)
{
cout<<"Please input the file name you want to open : " ;
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(strcmp(command , "close") == 0)
{
cout<<"Please input the file you want to close : " ;
cin>>file_name ;
closeFile(afd , file_name) ;
displayOpenFile(afd , user) ;
}
else if(strcmp(command , "read") == 0)
{
cout<<"Please input the file you want to read : " ;
cin>>file_name ;
readFile(afd , file_name) ;
displayOpenFile(afd , user) ;
}
else if(strcmp(command , "write") == 0)
{
cout<<"Please input the file you want to write : " ;
cin>>file_name ;
writeFile(afd , file_name) ;
displayOpenFile(afd , user) ;
}
else if(strcmp(command , "exit") == 0)
{
break ;
}
else
{
cout<<"No such command \""<< command <<"\""<<endl ;
}
}
}
}
return 0 ;
}
void initUser(MFD *mfd)
{
//初始化十個不同使用者
for(int i = 1 ; i <= 10 ; i++)
{
Map *m ;
m = (Map*)malloc(sizeof(Map)) ;
if(m == NULL)
{
exit(0) ;
}
cout<<"Please input init user name : " ;
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 ;
}
}
}
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 ;
}
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 ;
//查詢是否存在同名檔案
while(op)
{
if(strcmp(op->file_name , file->file_name) == 0)
{
cout<<"The file name "<<file->file_name<<" is already exit ! "<<endl ;
return false ;
}
preOp = op ;
op = op->next ;
}
preOp->next = file ;
}
}
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 ;
}
}
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<<"\" is open , please close it before ! \n" ;
return false ;
}
file = file->next ;
}
file = user->file ;
//在檔案中進行查詢
while(file)
{
if(strcmp(file_name , file->file_name) == 0)
{
if(file == user->file)
{
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 "<<user->userName<<" has not the file \""<<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<<"no read priority ! "<<endl;
return false ;
}
if(xfile->file_protect[1] >= open_file_protect[1])
{
xfile->open_file_protect[1] = open_file_protect[1] ;
}
else
{
cout<<"no write priority ! "<<endl;
return false ;
}
if(xfile->file_protect[2] >= open_file_protect[2])
{
xfile->open_file_protect[2] = open_file_protect[2] ;
}
else
{
cout<<"no excute priority ! "<<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<<"The open file is too many ! " <<endl ;
return false ;
}
}
else
{
cout<<"the "<<file_name<<" is not exit !"<<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<<"The file is not exit ! "<<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<<"no read priority ! \n"<<endl ;
return false ;
}
}
file = file->next ;
}
cout<<"no such file ! "<<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<<"no write priority ! \n"<<endl ;
return false ;
}
}
file = file->next ;
}
cout<<"no such file ! "<<endl ;
return false ;
}
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 ;
}
}
四、測試結果
相關推薦
cramfs檔案系統製作與移植(二)
[ [email protected] ]# bootm ## Booting kernel from Legacy Image at 30008000 ... Image Name: Linux Kernel Created: 2013-04-23 12:05:15
FAT32檔案系統之結構初探(一)
FAT32檔案系統總結 FAT32能夠支援大於32M小於32G的分割槽。雖然第三方的格式化程式可以把超過32G的分割槽格式化為FAT32,會是微軟的系統不允許將大於32G的分割槽格式化為FAT32檔案系統。 FAT32檔案系統由DBR及保留扇區,FAT1,FAT2,DATA
簡單的檔案系統模擬C/C++(作業系統)
一、目的與要求 目的 檔案系統是作業系統的一個重要組成部分,也是與使用者關係極為密切的部分。學生應獨立的用高階語言編寫和除錯一個簡單的檔案系統,模擬檔案管理的工作過程,從而對各種檔案操作命令的實質內容和執行過程有比較深入的瞭解,掌握它們的實施方法,加深對
[原始碼和文件分享]基於C語言的簡單檔案系統的實現
1 題目介紹 通過具體的檔案儲存空間的管理、檔案物理結構、目錄結構和檔案操作的實現,加深對檔案系統內部的資料結構、功能以及實現過程的理解。 1.1 要求 在記憶體中開闢一個虛擬磁碟空間作為檔案儲存分割槽,在其上實現一個簡單的基於多級目錄的單使用者單任務系統中的檔案系統。在推出該檔
記憶體檔案系統c/c++實現(VS2008)
#include <stdio.h> #include<string.h> #include <map> #include <malloc.h> #define NO 0 #define OK 1 //檔案定義 struct file { char fil
C++設計模式-使用Qt框架模擬策略模式(Strategy)+簡單工廠實現商場促銷
商城促銷: 1.簡單工廠模式:客戶端認識兩個類,CashSuper與CashFactory 2.簡單工廠模式 + 策略模式:客戶端只要認識CashContext就可以了,更加降低耦合性 策略模式解析: 策略模式是一種定義一系列演算法的方法,從概念上看,所有這些演算法完成的都
基於qml創建最簡單的圖像處理程序(2)-使用c++&qml進行圖像處理
.cn turn isnull 按鈕 編寫 可能 finish height 通過 《基於qml創建最簡單的圖像處理程序》系列課程及配套代碼基於qml創建最簡單的圖像處理程序(1)-基於qml創建界面http://www.cnblogs.com/jsxyhelu/p/83
《深入理解計算機系統》讀書筆記(ch2)+ C 泛型
tex byte 指向 get 讀書筆記 class its n) 支持 本章主要介紹各類型的機器表示,Stanford的CS107的lec2和lec3有精彩解釋,比看書快(當作書中只是的cache吧)。 lec4中介紹的C裏面如何使用泛型(沒有template, refe
Linux下一個最簡單的不依賴第三庫的的C程式(1)
如下程式碼是一段彙編程式碼,雖然標題中使用了C語言這個詞語,但下面確實是一段彙編程式碼,弄清楚了這個程式碼,後續的知識點才會展開。 #PURPOSE: Simple program that exits and returns a # status code back to the Lin
C#傳送簡單的post和get請求(轉載)
POST傳送請求及接受響應流程 根據目標地址址建立HttpWebRequest物件 設定響應的請求引數------Method、ContentType 等 使用HttpWebRequest物件獲取請求流並且寫入訊息體 使用H
C語言中,標頭檔案和原始檔的關係(轉)
//a.h void foo(); //a.c #include "a.h" //我的問題出來了:這句話是要,還是不要? void foo() { return; } //main.c #include "a.h" int main(int argc
【電腦常用操作記錄】調整和刪除windows休眠檔案Hiberfil.sys釋放C盤(轉載)
【重要指令--(管理員命令提示符)powercfg -h -size 70 即可將這臺計算機的 C:\Hiberfil.sys 減小為70%,即 2.8GB)】 Hiberfil.sys 是 Windows 休眠功能(Windows Hibernation)將記憶
C# 基礎(十三)C# 軟體開發過程中,可執行檔案.exe 出現bug,該怎麼排查、解決:找到出現bug的位置
一、簡介 之前是完成了寫軟體,從而實現軟體的基本功能。到了今天,需要對自己寫的可執行檔案.exe找bug了。那麼下面,我將結合自己的軟體的使用過程中,講自己的經驗融入進來,解釋下什麼是bug、以及該怎麼找Bug。 二、Bug的分類 主要參考: https://blog.csdn.ne
C++ 基礎(六).dll檔案的動態載入和靜態載入的區別:畫圖並舉例說明
一、簡介 本部落格主要介紹.dll檔案的動態載入和靜態載入的區別,畫圖並舉例說明。此外,我的上一篇關於靜態載入的部落格如下: C++ 基礎(五)使用vs2015封裝c++生成.dll檔案、.lib檔案、.h檔案後,給另一個工程使用:使用前,需配置標頭檔案(.h)靜態庫(.lib)和 動態庫(
C++ 基礎(五)使用vs2015封裝c++生成.dll檔案、.lib檔案、.h檔案後,給另一個工程使用:使用前,需配置標頭檔案(.h)靜態庫(.lib)和 動態庫(.dll )專案屬性
一、簡介 我是一個認真的人,要麼不寫,要麼我就把步驟、截圖、程式碼,一 一 附上,方便大家參考學習。 1、為什麼要生成DLL和lib? 就是為了給VS工程呼叫(比如C#呼叫、C++工程)。 2、C#為什麼要呼叫DLL和lib? C++寫的程
C++ 基礎(三)C++標頭檔案與原始檔的使用方法 舉例
看完這篇文章,你就知道怎麼用標頭檔案和原始檔了 http://www.cnblogs.com/fenghuan/p/4794514.html 標頭檔案:常量、變數、函式、類的宣告 原始檔:變數的定義和函式的實現 步驟一、先建立標頭檔案 #ifndef CIRCLE_H #defi
C# 基礎(十三)C# XML配置檔案、ini配置檔案的建立、讀寫:動態修改IP
一、簡介 傳統的配置檔案ini已有被xml檔案逐步代替的趨勢,這裡主要討論XML配置檔案。 二、ini網址 https://www.cnblogs.com/cncc/p/3415694.html(重點) https://www.cnblogs.com/xmy-007/p/640022
C++筆記(五) UML/設計模式(簡單工廠模式及工廠模式)
一、UML(統一建模語言) 模型:對問題的書面上的無歧義文字或圖形的描述,簡言之,模型是對現實的簡化。 建模:對現實系統進行適當的過濾,用適當的表現規則描述出簡介的模型問題。 UML:是一種基於面向物件的視覺化建模語言。UML採用了一組形象畫的額圖形符號作為建模語言,使
C檔案讀寫函式介紹(轉)
1. 首先要理解幾個概念: 檔案: 按一定規則儲存在磁碟上的資料集合。 檔名: 能唯一標識某個磁碟檔案的字串。形式: 碟符:/ 路徑 / 檔名.副檔名 文字檔案:: 資料以其數字字元的ASCII碼形式、一個位元組一個位元組地儲存在磁碟上。 二進位制檔案:資料以二進位制形式在儲存在磁碟上。 裝置檔案:輸入
C語言中,標頭檔案的作用,標頭檔案和原始檔的關係(轉)
簡單的說其實要理解C檔案與標頭檔案(即.h)有什麼不同之處,首先需要弄明白編譯器的工作過程,一般說來編譯器會做以下幾個過程: 1.預處理階段 2.詞法與語法分析階段 3.編譯階段,首先編譯成純彙編語句,再將之彙編成跟CPU相關的二進位制碼,生成各個目標檔案 (.obj檔案) 4.連線階段,將各個目標檔