Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
王兴钰 authored and 王兴钰 committed Jul 8, 2018
0 parents commit 5cc1631
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/wxy.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
41 changes: 41 additions & 0 deletions allot.cpp
Original file line number Diff line number Diff line change
@@ -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<length;i++)
{
k = 50-super_block.sbfree; //超级块中表示空闲块的指针
m = super_block.free_block[k]; //栈中的相应盘块的地址
p = super_block.free_block[49]; //栈中的最后一个盘块指向的地址
if(p == -1) //没有剩余盘块
{
printf("内存不足,不能够分配空间\n");
recycle(i);//之前已分配的i个盘块回收;
break;
}
if(super_block.sbfree == 1)
{
memory[m].bfree = 1; //将最后一个盘块分配掉
physic[i] = m;
super_block.free_block[49] = -1;
super_block.sbfree = 0;
for(j=50-memory[m].bfree;j<50;j++) //从最后一个盘块中取出下一组盘块号写入栈中
{
super_block.free_block[j] = memory[m].bfree_address[j];
super_block.sbfree++;
}
continue; //要跳过这次循环,下面的语句在IF中已经执行过
}
physic[i] = m; //如果栈中超过一个盘,栈中的相应盘块的地址写进 文件地址缓冲区
memory[m].flag = 1;
super_block.sbfree--;
cout << "磁盘分配完毕"<< k << endl;
}

}
39 changes: 39 additions & 0 deletions create.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// Created by 王兴钰 on 2018/7/7.
//
#include "index.h"
void create(char filename[],int size,int u_id,int mode,int limit)
{
int i,j;

// 当前目录下是否有重名文件
// TODO 当前目录下查找重复的文件名
for(i = 0;i < 1024;i++)
{
if(strcmp(filename,dirn[i].f_name)==0) //如果有文件名相同,则创建失败
{
cout << "有重复文件名!创建失败" << endl;
return;
}
}

for(i = 0;i <1024;i++)
{
if(dirn[i].i_num == -1)
{
dirn[i].i_num = i;
strcpy(dirn[i].f_name,filename);
i_node[i].i_mode = mode;
i_node[i].i_size = size;
i_node[i].i_Uid = u_id;
i_node[i].i_limit = limit;
allot(size);
for(j=0;j<size;j++)
{
i_node[i].i_address[j] = physic[j];
physic[j] = -1;
}
break;
}
}
}
33 changes: 33 additions & 0 deletions display.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Created by 王兴钰 on 2018/7/7.
//

#include "index.h"

void display(char filename[]) //显示文件信息
{
int i,j,k;
cout<<"文件名字 文件类型 文件长度 读取权限 所属目录 所属用户" << endl;
for(i=0;i<1024;i++)
{
k=dirn[i].i_num;
if(strcmp(filename,dirn[i].f_name)==0 && (i_node[k].i_mode==1))
{
cout<<dirn[i].f_name <<" "; //文件名
cout<<i_node[k].i_mode<<" "; //文件的类型
cout<<i_node[k].i_size<<" "; //文件的长度
//cout<<i_node[k].i_limit<<" ";
//cout<<dirn[i].f_dname<<" "; //文件所在的上级目录
cout<<"user"<< i_node[dirn[i].i_num].i_Uid<<" "<<endl; //文件所属user
for(j=0;j<i_node[k].i_size;j++) //显示物理地址
{
cout<< "占用物理地址"<< i_node[i].i_address[j]<<endl; //文件具体占用的盘块号
}
break;
}
}
if(i==1024)
{
cout<<"没有这个文件 或者这个文件不是正规文件"<<endl;
}
}
17 changes: 17 additions & 0 deletions display_memory.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// Created by 王兴钰 on 2018/7/7.
//
//显示磁盘使用情况
#include "index.h"
void display_memory()
{
int i,m,k=0;
for(i=0;i<1024;i++)
{
if(memory[i].flag==0)
k++;
}
m=1024-k;
cout << "空闲的盘块数是" << k << endl;
cout << "使用的盘块数是" << m <<endl;
}
112 changes: 112 additions & 0 deletions index.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//
// Created by 王兴钰 on 2018/7/5.
//

#ifndef WXY_INDEX_H
#define WXY_INDEX_H
#include <iostream>
#include <cstdlib>
#include <string>
#include <memory.h>
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
70 changes: 70 additions & 0 deletions innitialize.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading

0 comments on commit 5cc1631

Please sign in to comment.