From 5cc1631b25c6fb3f2cdccfd78cfc185aabcae63a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=85=B4=E9=92=B0?= Date: Sun, 8 Jul 2018 14:36:25 +0800 Subject: [PATCH] Initial commit --- .idea/codeStyles/Project.xml | 29 +++++++++ .idea/misc.xml | 7 +++ .idea/modules.xml | 8 +++ .idea/wxy.iml | 2 + CMakeLists.txt | 6 ++ allot.cpp | 41 +++++++++++++ create.cpp | 39 ++++++++++++ display.cpp | 33 +++++++++++ display_memory.cpp | 17 ++++++ index.h | 112 +++++++++++++++++++++++++++++++++++ innitialize.cpp | 70 ++++++++++++++++++++++ main.cpp | 19 ++++++ recycle.cpp | 37 ++++++++++++ 13 files changed, 420 insertions(+) create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/wxy.iml create mode 100644 CMakeLists.txt create mode 100644 allot.cpp create mode 100644 create.cpp create mode 100644 display.cpp create mode 100644 display_memory.cpp create mode 100644 index.h create mode 100644 innitialize.cpp create mode 100644 main.cpp create mode 100644 recycle.cpp diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..30aa626 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..8822db8 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..6d89d23 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/wxy.iml b/.idea/wxy.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/.idea/wxy.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..390f3aa --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.10) +project(wxy) + +set(CMAKE_CXX_STANDARD 11) + +add_executable(wxy main.cpp index.h innitialize.cpp allot.cpp recycle.cpp display_memory.cpp display.cpp create.cpp) \ No newline at end of file diff --git a/allot.cpp b/allot.cpp new file mode 100644 index 0000000..44dd545 --- /dev/null +++ b/allot.cpp @@ -0,0 +1,41 @@ +// +// Created by 王兴钰 on 2018/7/5. +// +#include "index.h" + +//磁盘分配 +void allot(int length) +{ + int i,j,k,p; + int m = -1; + for(i=0;i +#include +#include +#include +using namespace std; + +#define blocksize 1024 //盘块大小 +#define memorysize 1024 //内存中块个数 +#define NDDR 10 //每个i节点最多 +#define OFile 10 //每个用户最多打开的文件数 +#define OSopenfile 20 //系统打开的最大文件数 +#define login_default 0 //默认登录用户号 +#define FOLDER 0 //文件夹 +#define DOCUMENT 1 //文件 + +extern int physic[100]; //文件地址缓冲区 +// string UserName[8] = { "root","user1","user2","user3","user4","user5","user6","user7" }; +// string PassWord[8] = { "0","1","2","3","4","5","6","7" }; +extern char filename[100]; //暂存文件名 +//盘块 +//盘块 +struct block +{ + int bfree; //存放的空闲盘块的个数 + int bfree_address[50]; //存放空闲盘块的地址 + int flag; //盘块是否被占用标志 + char content[blocksize]; //块上每个字节存放的字符 +}; + +//超级块 +struct Super_Block +{ + int sbfree; //空闲盘块个数 + int free_block[50]; //进入栈中的空闲块 + int free_inode[50]; //空闲i节点 +}; + +//i节点 +struct inode +{ + int i_mode; //文件模式 + int i_size; //文件大小 + int i_address[NDDR]; //物理块号 + int i_limit; //读写权限,0:可读可写;1:可读不可写;2:可写不可读 + int i_Uid; //所属用户 + int i_count; //文件计数 + time_t i_time; //文件修改时间 + + bool operator==(const inode& rhs) + { + return(i_mode == rhs.i_mode) && (i_size == rhs.i_size) && (i_address == rhs.i_address) + && (i_limit == rhs.i_limit) && (i_Uid == rhs.i_Uid) && (i_count == rhs.i_count) && (i_time == rhs.i_time); + } +}; + +//目录项 +struct dir +{ + char f_name[20]; //文件名 + int i_num; //文件的i节点号 + char f_dname[20]; //文件上级目录 +}; + +//用户名 +struct Username +{ + int u_id; //用户ID + char u_name[20]; //用户名字 + int u_open; //用户已经打开的文件数 + int u_ofile[OFile]; //用户最多打开的文件数 +}; + +//密码 +struct Password +{ + int p_id; //用户ID + char p_password[12];//密码 +}; + +//文件打开表 +struct file +{ + int f_count; //引用计数 + struct inode f_inode; //指向内存索引节点 + long f_off; //读写指针文件当前读写到的文件位置 +}; + +// extern string UserName[8]; +// extern string PassWord[8]; +extern struct block memory[memorysize]; +extern struct Super_Block super_block; +extern struct inode i_node[1024]; +extern struct dir dirn[1024]; +extern struct Username uname[8]; +extern struct Password pwd[8]; +extern struct file sys_openfile[OSopenfile]; + + +//系统初始化 +extern void initialize(); +extern void recycle(int length); +extern void allot(int length); +extern void display_memory(); +extern void create(char filename[],int size,int u_id,int mode,int limit); +void display(char filename[]); +#endif //WXY_INDEX_H diff --git a/innitialize.cpp b/innitialize.cpp new file mode 100644 index 0000000..a220514 --- /dev/null +++ b/innitialize.cpp @@ -0,0 +1,70 @@ +// +// Created by 王兴钰 on 2018/7/5. +// +#include "index.h" + +void initialize() +{ + int i = 0,j,k=0; + super_block.sbfree = 50; + for (i = 0;i < 50;i++) + { + super_block.free_block[i] = i; //初始化进入栈的空闲块 + } + + + for (i = 0;i < 1024 ;i++) //目录项初始化 + { + strcpy(dirn[i].f_name,""); + dirn[i].i_num = -1; + } + + for (i = 0;i < 1024 ;i++) //文件结点初始化 + { + i_node[i].i_limit = -1; + i_node[i].i_count = 0; + i_node[i].i_Uid = -1; + i_node[i].i_size = -1; + for(j = 0;j<50;j++) + { + i_node[i].i_address[j] = -1; //文件地址初始化 + } + + } + + for (i = 0; i < 1024; ++i) //存储空间初始化 + { + memory[i].bfree = 0; + memory[i].flag = 0; + memset(memory[i].bfree_address,-1, sizeof(memory[i].bfree_address)); + memset(memory[i].content,'\0', sizeof(memory[i].content)); + } + + for(i = 0;i < 1024; i++) //将空闲块的信息用成组链接的方法写进每组的最后一个块中 + { //存储空间初始化 + + if ((i + 1) % 50 == 0) { + cout << "组号" << k << endl; + k = i + 1; + for (j = 0; j < 50; j++) { + if (k < 1025) { + memory[i].bfree_address[j] = k;//下一组空闲地址 + memory[i].bfree++; //下一组空闲个数 注意在memory[i].n++之前要给其赋初值 + k++; + } else { + memory[i].bfree_address[j] = -1; + } + } + memory[i].flag = 0; //标记为没有使用 + continue; //处理完用于存储下一组盘块信息的特殊盘块后,跳过本次循环 + } + + for (j = 0; j < 50; j++) { + memory[i].bfree_address[j] = -1; + } + memory[i].flag = 0; + } + for(int i = 0; i < 100; i++) { + physic[i] = -1; + } +} diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..6070789 --- /dev/null +++ b/main.cpp @@ -0,0 +1,19 @@ +#include +#include "index.h" +using namespace std; +struct inode i_node[1024]; +struct dir dirn[1024]; +struct Super_Block super_block; +struct block memory[memorysize]; +int physic[100]; //文件地址缓冲区 + +int main() { + initialize(); + cout << "worked!" << endl; + char tempfilename[100]; + cin >> tempfilename; + create(tempfilename,10,3,1,1); + display_memory(); + display(tempfilename); + return 0; +} \ No newline at end of file diff --git a/recycle.cpp b/recycle.cpp new file mode 100644 index 0000000..01cd46b --- /dev/null +++ b/recycle.cpp @@ -0,0 +1,37 @@ +// +// Created by 王兴钰 on 2018/7/6. +// +#include "index.h" + +//磁盘回收 +void recycle(int length) +{ + int i,j,k,m; + for (i = 0;i < length-1;i++) + { + k = physic[i]; //需回收空闲块地址 + m = 49 - super_block.sbfree; //放入空闲区位置 + if(super_block.sbfree == 50) //代表空闲块已满,需要将当前栈中内容写入下一个栈 + { + for(j = 0;j<50;j++) + { + memory[k].bfree_address[j] = super_block.free_block[j]; + } + int u =0; + for (;u<50;u++) + { + super_block.free_block[u] = -1; + } + super_block.sbfree = 0; + memory[k].bfree = 50; + } + memory[k].flag = 0; + if(m == -1) //如果m=-1,地址中存放刚才满栈的地址的信息 + { + m = 49; + } + super_block.free_block[m]=physic[i]; //将下一个文件地址中的盘块号回收到栈中 + super_block.sbfree++; + } + cout << "磁盘回收完毕" << k << endl; + }