操作系统课程设计 简单的二级文件系统

合集下载

计算机操作系统课程设计_简单的二级文件系统

计算机操作系统课程设计_简单的二级文件系统

《操作系统》课程设计题目:设计一个简单二级文件系统专业:计算机科学与技术年级:文计091-1*名:***学号:200990514103指导教师:任满杰时间:2011-2012第二学期2012年9月1日一、设计内容1、可以实现下列几条命令。

Format 格式化Dir 列文件目录Create 创建文件Delete 删除文件Deldir 删除目录Open 打开文件Close 关闭文件Search 查询文件Copy 拷贝文件Cut 剪切文件二、开发环境Windows操作系统Microsoft Visual C++三、分析设计(一)实验原理通过程序模拟Linux文件系统,用一个二进制文件(FileSystem.disk)来模拟磁盘.设计一个多用户的二级文件系经统、实现一般的创建文件、目录,删除文件、目录,切换目录,打开、关闭文件、读写文件等操作。

文件系统,包含格式化,显示文件(目录),创建文件等几个简单命令的实现,而且能完成超级块的读写,节点的读写等过程. 本文件系统采用两级目录,其中第一级对应于用户账号,第二级对应于用户账号下的文件。

另外,为了简单本文件系统未考虑文件共享、文件系统安全以及管道文件与设备文件等特殊内容。

1.程序执行流程图:2.数据块的分配和回收(二)程序结构设计FileSystem类负责管理磁盘空间和磁盘内存I节点,负责对磁盘空间和磁盘数据进行优化管理。

并提代接口言方法供用户或程序调用。

(三)数据结构int physic[100]; //文件地址缓冲区int style=1; //文件的类型char cur_dir[10]="root"; //当前目录int search_i=0;struct command{char com[10];}cmd[20];struct block{int n; //空闲的盘快的个数int free[50]; //存放空闲盘快的地址int a; //模拟盘快是否被占用}memory[20449];struct block_super{int n; //空闲的盘快的个数int free[50]; //存放进入栈中的空闲块int stack[50]; //存放下一组空闲盘快的地址}super_block;struct node //i结点信息{int file_style; //i结点文件类型int file_length; //i结点文件长度int file_address[100]; //i结点文件的物理地址 char file_message[100];} i_node[640];struct dir //目录项信息{char file_name[10]; //文件名int i_num; //文件的结点号char dir_name[10]; //文件所在的目录} root[640];四.运行示例及结果分析菜单删除目录文件查询拷贝剪切五、程序实现详细程序请参见源文件, 在此只列举3点1、剪切文件程序片段:void cut(char *tmp,char *newname) {int i; //,jchar t[20];_strtime(t);for(i=0;i<640;i++)if(strcmp(newname,root[i].file_name)==0){break;}if(i==640){printf("目录不存在,不能剪切!\n");return;}for(i=0;i<640;i++){if(strcmp(tmp,root[i].file_name)==0){strcpy(root[i].dir_name,newname);strcpy(i_node[root[i].i_num].change_t,t);//printf("剪切成功!\n");//return;}}if(i==640)printf("文件不存在,执行失败!\n");}2.无文件查询程序片段:void search(char* filename){int sign=0;for(search_i=0;search_i<640;search_i++){if(strcmp(root[search_i].file_name,filename)==0) //查询文件中所在目录信息和当前目录信息相同的数据{sign=1;int k=root[search_i].i_num;printf("%s\t",root[search_i].file_name); //文件名printf(" %d\t",i_node[k].file_style); //文件的类型printf(" %d\t\t",i_node[k].file_length); //文件的长度printf("%s\n",root[search_i].dir_name); //文件所在的目录}}if (sign==0){printf("%s\n","未找到该文件!");}}六、心得与体会这次设计中遇到最难的问题就是拷贝功能的实现,由于之前没有接触过拷贝的原理,所以通过和同伴的讨论和试验,终于突发奇想的运用改变文件路径的源代码和创建文件相结合,终于实现了拷贝功能,能够将一个文件从一个目录拷贝到另一目录(即先移动过去然后再在原位置创建一个原文件),可是却无法拷贝到root根目录,为了解决这个问题,又在创建文件的程序里添加了一个判断是否为根目录的语句,可是后来又发现了真个系统存在重命名的问题。

模拟简单二级文件管理系统

模拟简单二级文件管理系统

操作系统课程设计模拟简单二级文件管理系统*名:***学号:系别:计算机学院专业:网络工程年级:16级指导教师:2019年05 月11 日一、课程设计项目介绍(含项目介绍及设计目的)1、设计目的:通过具体的文件存储空间的管理、文件的物理结构、目录结构和文件操作的实现,加深对文件系统内部功能和实现过程的理解。

2、项目介绍:为LINUX 设计一个简单的二级文件系统。

本文件系统采用类似DOS系统的文件管理方式,每次调用该文件系统时,首先申请一定的内存空间,然后对该内存空间进行分配。

将申请到的空间划分为目录区,文件区;采用位示图进行空间管理,盘块的分配使用显示链接(FAT表)的方式。

每次调用该文件系统时自动为其分配空间,并将上次操作的结果从硬盘上调入内存;当结束调用时则将操作的结果重新存入硬盘,以便下次调用。

(每次使用都会自动搜索文件系统,以此确定是否是第一次使用;若是则格式化生成文件系统,否则读取已存在的文件系统。

)本项目通过VC编写简单的二级文件系统的代码,实现文件管理方式,使用者只需要给出相应的文件操作命令就可以分别得到各类文件操作的相应,并且可以选择登陆或注销不同用户。

二、总体设计(含系统的总体结构、原理框图或各模块介绍等)1、系统总体结构图:2、模块介绍(1)、主函数部分:在主函数系统接受输入信息,包括登陆用户和注册用户,登陆完成后选择相应的文件操作,可以选择创建目录、浏览目录、修改目录、创建文件等操作,如果选择错误就会出现相应的提示信息。

(2)、命令解释层函数:在命令解释层函数cmdexp()里加了一些选择和操作功能,增加程序实现的功能,如原来程序只有显示当前目录和文件、创建目录和修改目录的功能,把它拓展到系统所要求的全部功能,并在原有的程序的基础上进行相应的修改,使程序更加完善。

(3)、文件系统格式化函数:该函数首先建立文件,申请空间,在设置成功时将其空间写入filesystem.dat,使filesystem.dat为1M。

操作系统课程设计--为linux系统设计一个简单的二级文件系统

操作系统课程设计--为linux系统设计一个简单的二级文件系统

操作系统课程设计报告题目: 为Linux系统设计一个简单的二级文件系统指导老师:时间:2021.8.30一课程设计的目的课程设计目的使学生熟悉文件管理系统的设计方法;加深对所学各种文件操作的了解及其操作方法的特点。

通过模拟文件系统的实现,深入理解操作系统中文件系统的理论知识, 加深对教材中的重要算法的理解。

同时通过编程实现这些算法,更好地掌握操作系统的原理及实现方法,提高综合运用各专业课知识的能力。

二课程设计的要求1.可以实现下列几条命令:login用户登录dir 列目录create创建文件delete 删除文件open 打开文件close 关闭文件read 读文件write 写文件2.列目录时要列出文件名,物理地址,保护码和文件长度3.源文件可以进行读写保护三算法设计本次二级文件系统主要分为五大模块,分别是用户登录模块、新建目录模块、新建文件模块、删除文件模块和读取文件模块。

用户登录成功后才可以进行其他模块的操作。

1 用户登录模块用户登录模块要求用户输入用户,当输入正确后才能进行其他模块操作,否则提示用户名不存在并询问用户是否用此名进行注册。

若用户名未满,则提示注册成功,否则提示用现有注册用户,进行登录,并返回到登录界面。

用户登录模块流程图如图1所示。

图1 用户登录模块流程图2新建文件模块新建文件模块是在用户出入create 指令后进行的,进入后会要求用户输入文件名,并判断文件名是否存在,若没有则在要求用户输入文件读写权限,否则重新输入新的文件名。

新建文件模块流程图如图2所示。

图2 新建文件流程图3 删除文件模块删除文件模块是根据用户鼠标右击时选择到的节点来确定要删除节点的名字与路径,然后判断该节点是目录还是文件。

若是文件则直接删除文件,若是目录则进入该目录再删除其全部文件。

删除文件模块流程图如图4所示。

图4 删除文件模块流程图4读取文件模块读取文件模块,要求用户要在文件打开的前提下,将磁盘中的内容读取到内存中。

操作系统课程设计--为LINUX设计一个简单的二级文件系统

操作系统课程设计--为LINUX设计一个简单的二级文件系统

操作系统课程设计--为LINUX设计一个简单的二级文件系统理工大学华夏学院课程设计课程名称操作系统课程设计题目为LINUX 设计一个简单的二级文件系统专业软件技术班级姓名成绩指导教师2011年1月17日至 2011年1月21日课程设计任务书设计题目:为LINUX 设计一个简单的二级文件系统设计目的:1、通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现。

2、提高学生的程序设计能力、提高算法设计质量与程序设计素质。

设计任务:(在规定的时间内完成下列任务)为LINUX 设计一个简单的二级文件系统。

要求做到以下几点:1、可以实现下列几条命令(至少4条)用户登录 Login列文件目录 Dir创建文件 Create删除文件 Delete打开文件 Open关闭文件 2、列目录时要列出文件名、物理地址、Close读文件保护码和文件长度。

Read写文件 3、源文件可以进行读写保护。

Write时间安排:1月 17日布置课程设计任务;分配题目后,查阅资料、准备程序;1月 18日,1月20 日上机调试程序、书写课程设计报告;1月21 日上午提交课程设计报告及相关文档。

地点:学校机房(具体见现代教育中心大屏幕安排)具体要求:1、课程设计报告按统一通用格式书写,具体格式要求请在网络上查阅2、每位学生应独立完成各自的任务且每天至少在设计室工作半天指导教师签名: 11年 1月 7日教研室主任(或责任教师)签名: 11年 1月 7 日 1.实现原理首先~实现Login用户登录~,用户名:zhaoran 密码:1234,必须登录成功才能进入文件系统进行查看~进入后~通过树状展示文件目录,默认文件目录是C:\Program Files,,右侧显示文件夹图标或者文件图标~选中节点鼠标右击弹出菜单~有新建目录~新建文件~删除文件~打开文件~属性菜单项~并分别实现其功能。

2.开发环境开发工具 MyEclipse 7.0开发语言 Java3.关键算法实现流程图3.1流程图登陆账号错误验证账号密码账号正确目录右键选择操作新建目录新建文件删除打开属性结束3.2具体算法代码3.2.1登陆模块public class Login extends JFrame {private static final long serialVersionUID = 1L; private JPanel jContentPane = null;private JLabel jLabel1 = null;private JLabel jLabel2 = null;private JLabel jLabel = null;private JTextField jTextField1 = null;private JPasswordField jPasswordField = null; private JButton jButton1 = null;private JButton jButton2 = null;private JTextField getJTextField1() {if (jTextField1 == null) {jTextField1 = new JTextField();jTextField1.setPreferredSize(new Dimension(10, 25)); jTextField1.setBounds(new Rectangle(183, 59, 169, 30));}return jTextField1;}private JPasswordField getJPasswordField() {if (jPasswordField == null) {jPasswordField = new JPasswordField();jPasswordField.setBounds(new Rectangle(183, 99, 169, 30)); jPasswordField.addKeyListener(newjava.awt.event.KeyAdapter(){public void keyPressed(java.awt.event.KeyEvent e){if(e.getKeyChar()==KeyEvent.VK_ENTER){test();}}});}return jPasswordField;}public static void main(String[] args) {SwingUtilities.invokeLater(new Runnable() {public void run() {Login thisClass = new Login();thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); thisClass.setVisible(true);}});}public Login() {super();initialize();}private void initialize() {this.setSize(400, 250);Dimension d=Toolkit.getDefaultToolkit().getScreenSize(); Dimension fsize=this.getSize();this.setLocation((d.width-fsize.width)/2,(d.height-fsize.height)/2);this.setContentPane(getJContentPane());this.setTitle("二级文件系统");}private JButton getJButton2() {if (jButton2 == null) {jButton2 = new JButton();jButton2.setBounds(new Rectangle(218, 167, 90, 32)); "); jButton2.setText("取消jButton2.addActionListener(newjava.awt.event.ActionListener() {public void actionPerformed(java.awt.event.ActionEvent e){System.exit(0);}});}return jButton2;}private JPanel getJContentPane() {if (jContentPane == null) {jLabel = new JLabel();jLabel.setText("密码:");jLabel.setHorizontalAlignment(SwingConstants.CENTER);jLabel.setBounds(new Rectangle(27, 101, 132, 29));jLabel2 = new JLabel();jLabel2.setText("用户名:");jLabel2.setBounds(new Rectangle(27, 59, 133, 29));jLabel2.setHorizontalAlignment(SwingConstants.CENTER);jLabel2.setPreferredSize(new Dimension(10, 15));jLabel1 = new JLabel();jLabel1.setText("登陆界面");jLabel1.setFont(new Font("\u534e\u6587\u5b8b\u4f53", Font.BOLD, 24));jLabel1.setBounds(new Rectangle(0, 0, 389, 32)); jLabel1.setForeground(new Color(255, 0, 54));jLabel1.setHorizontalAlignment(SwingConstants.CENTER); jContentPane = new JPanel();jContentPane.setLayout(null);jContentPane.add(jLabel1, null);jContentPane.add(jLabel2, null);jContentPane.add(jLabel, null);jContentPane.add(getJTextField1(), null); jContentPane.add(getJPasswordField(), null); jContentPane.add(getJButton1(), null);jContentPane.add(getJButton2(), null);}return jContentPane;}private JButton getJButton1() {if (jButton1 == null) {jButton1 = new JButton();jButton1.setBounds(new Rectangle(79, 168, 84, 33)); jButton1.setText("确定");jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) {test();}});}return jButton1;}public void test(){if(jTextField1.getText().equals("zhaoran")&&String.valueOf(jPassword Field.getPassword()).equals("1234")){String aa[]={"aa","bb"};Contents.main(aa);setVisible(false);}else{JOptionPane.showMessageDialog(Login.this, "输入错误~~请重新输入~");}}}3.2.2新建文件目录private class newMenuAction implements ActionListener {public void actionPerformed(ActionEvent arg0) {try {String Name = JOptionPane.showInputDialog("创建目录名称:");if (!Name.equals("")) {TreePath path = tree.getSelectionPath();MutableTreeNode node = (MutableTreeNode) path.getLastPathComponent();DefaultTreeModel model = (DefaultTreeModel) tree.getModel(); MutableTreeNode newNode = new DefaultMutableTreeNode(Name); tree.fireTreeExpanded(path);if (node.getAllowsChildren()) model.insertNodeInto(newNode, node, 0);else {JOptionPane.showMessageDialog(null, "不能给文件追加下级目录!"); return;}String fullPath = "";for (Object obj : path.getPath()) {String str = obj.toString();if (str.endsWith("\\"))str = str.substring(0, str.length() - 1);if (fullPath.equals(""))fullPath += str;elsefullPath += "\\" + str;}File parentDir = new File(fullPath);if (parentDir.isDirectory()) {File currentFile = new File(fullPath + "\\" + Name);currentFile.mkdir();}} else {JOptionPane.showMessageDialog(null, "文件名不能为空~"); }} catch (HeadlessException e) {e.printStackTrace();}}}3.2.3新建文件private class newFileAction implements ActionListener { public void actionPerformed(ActionEvent e) {TreePath tp = tree.getSelectionPath();String fullPath = "";for (Object obj : tp.getPath()) {String str = obj.toString();if (str.endsWith("\\"))str = str.substring(0, str.length() - 1);if (fullPath.equals(""))fullPath += str;elsefullPath += "\\" + str;}String FileName = JOptionPane.showInputDialog("创建文件名称:"); if (!FileName.equals(null)) {File f = new File(fullPath + "\\" + FileName);try {f.createNewFile();} catch (IOException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}} else {JOptionPane.showMessageDialog(null, "文件名不能为空~");}}}3.2.4删除功能private class MenuAction implements ActionListener {public void actionPerformed(ActionEvent e) {TreePath tp = tree.getSelectionPath();DefaultMutableTreeNode node = (DefaultMutableTreeNode) tp.getLastPathComponent();DefaultTreeModel dtm = (DefaultTreeModel) tree.getModel(); dtm.removeNodeFromParent(node);String fullPath = "";for (Object obj : tp.getPath()) {String str = obj.toString();if (str.endsWith("\\"))str = str.substring(0, str.length() - 1); if (fullPath.equals(""))fullPath += str;elsefullPath += "\\" + str;}File currentFile = new File(fullPath);if (currentFile.isFile())currentFile.delete();elsedeleteDir(currentFile);}}public static boolean deleteDir(File dir) { if (dir.isDirectory()) {String[] subDir = dir.list();for (String s : subDir) {deleteDir(new File(dir, s));}}return dir.delete();}3.2.5打开文件public class Open_File {public static void main(String[] args) {DemoWindow2 dw = new DemoWindow2("打开文件");Toolkit theKit = dw.getToolkit();Dimension wndSize = theKit.getScreenSize();dw.setBounds(wndSize.width/3,wndSize.height/3,wndSize.width/3,wndSize.height/3);dw.setVisible(true);}}class DemoWindow2 extends JFrame implements ActionListener, Runnable {private static final long serialVersionUID = 1L;JTextArea jta = new JTextArea();int v = ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED;int h = ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED;JScrollPane jsp = new JScrollPane(jta, v, h);JPanel jp1 = new JPanel();JButton jb = new JButton("打开文件");public DemoWindow2(String title) {super(title);jp1.add(jb);add(jsp);add(jp1, BorderLayout.SOUTH);jb.addActionListener(this);}public void actionPerformed(ActionEvent e) {new Thread(this).start();}public void run() {jta.setText("");JFileChooser jfc = newJFileChooser(System.getProperty("user.home"));jfc.setFileFilter(new TXTFilter());if (jfc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { File file = jfc.getSelectedFile();if (file != null) {try {FileInputStream fis = new FileInputStream(file); ProgressMonitorInputStream pmis = new ProgressMonitorInputStream( this, "读取进度", fis);ProgressMonitor pm = pmis.getProgressMonitor();pm.setMillisToDecideToPopup(10);pm.setMillisToPopup(0);byte[] readbyte = new byte[10];while (pmis.read(readbyte) > 0) {jta.append(new String(readbyte, "GB2312"));Thread.sleep(10);if (pm.isCanceled()) {jta.append("\n\n终止读取文件~");}}pmis.close();} catch (Exception e) {System.out.println(e.getMessage()); }}}}}class TXTFilter extends FileFilter { public boolean accept(File f) {if (f.isDirectory()) {return true;}String[] filePostfix = { "txt" };for (String str : filePostfix) {if (getExtension(f).equals(str)) return true;}return false;}public String getDescription() {return "TXT文件(*.txt)";}public static String getExtension(File f) { String ext = "";String s = f.getName();int i = stIndexOf('.');if (i > 0 && i < s.length() - 1) {ext = s.substring(i + 1).toLowerCase();}return ext;}}3.2.6查看文件属性public class Proper extends JFrame {private static final long serialVersionUID = 1L; private JPanel jContentPane = null;private String fullPath;private JTextArea jTextArea = null;private JScrollPane jScrollPane = null;public Proper(String fullPath) {super();this.fullPath=fullPath;initialize();}private void initialize() {this.setSize(332, 221);Dimension d=Toolkit.getDefaultToolkit().getScreenSize();Dimension fsize=this.getSize();this.setLocation((d.width-fsize.width)/2, (d.height-fsize.height)/2); this.setContentPane(getJContentPane());this.setTitle("查看文件属性");this.setVisible(true);}private JPanel getJContentPane() {if (jContentPane == null) {jContentPane = new JPanel();jContentPane.setLayout(null);jContentPane.add(getJScrollPane());}return jContentPane;}private JScrollPane getJScrollPane() {if (jScrollPane == null) {jScrollPane = new JScrollPane();jScrollPane.setBounds(new Rectangle(3, 1, 319, 185));jScrollPane.setViewportView(getJTextArea());}return jScrollPane;}private JTextArea getJTextArea() {if (jTextArea == null) {jTextArea = new JTextArea();jTextArea.setBounds(new Rectangle(3, 1, 319, 185));jTextArea.setEditable(false);File currentFile = new File(fullPath);StringBuffer sb = new StringBuffer();if (currentFile.isDirectory())sb.append("文件夹路径:" + currentFile.getAbsolutePath() + "\n");elsesb.append("文件路径:" + currentFile.getAbsolutePath() + "\n");sb.append("是否可读:" + currentFile.canRead() + "\n");sb.append("是否可写:" + currentFile.canWrite() + "\n");if (!currentFile.isDirectory())sb.append("文件长度:" + currentFile.length() / (1024 * 1024) + "M\n");SimpleDateFormat s = new SimpleDateFormat("yyyy年MM月dd日HH小时mm分钟ss秒");sb.append("文件上次修改时间:" + s.format(newDate(stModified()))+ "\n");sb.append("文件是否被隐藏:" + currentFile.isHidden() + "\n");jTextArea.setText(sb.toString()); }return jTextArea;}4软件运行过程4.1登陆记主目录界面,默认为C盘,4.2新建目录newfiles4.3新建文件file4.4删除4.5打开文件4.6查看文件属性5(设计小结操作系统课程设计,自己通过查找资料、复习课本、编程调试,写实验报告等环节,进一步掌握了以前学到的知识,并且还对操作系统应用有了更深入的认识与掌握。

操作系统课程设计二级文件系统

操作系统课程设计二级文件系统

#include<stdio.h>#include<string.h>#include<stdlib.h>//#include <time.h>int N=0; //记录用户数目struct MFILE{ //文件表char filename[19];char content[1000];//char filecha_time[18];int fileflag;};struct DIR{ //目录表char dirname[19];MFILE file[100];int filenum;int coflag;//char dircha_time[18];};struct USER{ //用户表char name[5];char password[5];DIR dir[100];int dirnum;} user[10];int check(char* name,char* password,int &userlo);void enter_dir(int userlo);void show_dir(int userlo);int open_dir(int userlo,char* ch1,int &filelo);void create_dir(int userlo,char* ch1);void delete_dir(int userlo,char* ch1);void re_dirname(int userlo,char* ch1,char* ch2);void enter_file(int userlo,int filelo);void show_file(int userlo,int filelo);void open_file(int userlo,char* ch1,int filelo);void close_file(int userlo,char* ch1,int filelo);void read_file(int userlo,char* ch1,int filelo);void create_file(int userlo,char* ch1,char* ch2,int filelo); void delete_file(int userlo,char* ch1,int filelo);void re_filename(int userlo,char* ch1,char* ch2,int filelo);void add_filestr(int userlo,char* ch1,char* ch2,int filelo);//char* nowtime();void main(){FILE *fp=fopen("user.txt","r+");while(!feof(fp)){ //用户文件读入到用户顺序表char name[5];char password[5];fscanf(fp,"%s%s",name,password);strcpy(user[N].name,name);strcpy(user[N].password,password);user[N].dirnum=0;N++;}while(1){char name[19];char password[19];printf("请登录:\n");printf("用户名->");scanf("%s",name);printf("密码->");scanf("%s",password);int userlo; //用户标识if(check(name,password,userlo)==1){enter_dir(userlo);}else printf("用户名或密码错误!\n");}}int check(char* name,char* password,int &userlo){for(int i=0;i<N;i++){if(strcmp(user[i].name,name)==0&&strcmp(user[i].password,password)==0) {userlo=i;printf("登陆成功!\n");printf("输入help获得帮助列表\n");return 1;}elsei++;}return 0;}void enter_dir(int userlo){biaohao:while(1){char action[19];char ch1[19];char ch2[19];printf("->");if(scanf("%s",action)&&strcmp(action,"help")==0){printf("*****************************************\n"); //printf("* help 帮助!*\n");printf("* dir 显示目录!*\n");printf("* open name 打开目录!*\n");printf("* create 目录名创建新目录!*\n");printf("* delete 目录名删除目录! *\n");printf("* rename 旧名称新名称为目录重命名!*\n");printf("* quit 退出登陆!*\n");printf("*****************************************\n"); }else if(strcmp(action,"dir")==0){show_dir(userlo);}else if(strcmp(action,"quit")==0){break;}else if(strcmp(action,"open")==0){scanf("%s",&ch1);int filelo;if(open_dir(userlo,ch1,filelo)==0){printf("目录打开成功!\n");printf("输入help获得帮助列表\n");open_dir(userlo,ch1,filelo);enter_file(userlo,filelo);}elsegoto biaohao;}else if(strcmp(action,"create")==0){scanf("%s",&ch1);create_dir(userlo,ch1);}else if(strcmp(action,"delete")==0){scanf("%s",&ch1);delete_dir(userlo,ch1);}else if(strcmp(action,"rename")==0){scanf("%s%s",&ch1,&ch2);re_dirname(userlo,ch1,ch2);}else{printf("%s不是系统内部命令!\n",action); }}}void show_dir(int userlo){if(user[userlo].dirnum==0){printf("目录为空!\n");return;}printf("目录名\n");for(int i=0;i<user[userlo].dirnum;i++){//printf("%-26s\n",user[userlo].dir[i].dirname);printf(user[userlo].dir[i].dirname);printf(" \n");}}int open_dir(int userlo,char* ch1,int &filelo){for(int i=0;i<user[userlo].dirnum;i++){if(strcmp(user[userlo].dir[i].dirname,ch1)==0){user[userlo].dir[i].coflag=1;filelo=i;return 0;// printf("目录打开成功!\n");//printf("输入help获得帮助列表\n");}}printf("该目录不存在!\n");return 1;}void create_dir(int userlo,char* ch1){for(int i=0;i<user[userlo].dirnum;i++){if(strcmp(user[userlo].dir[i].dirname,ch1)==0){printf("有同名用户存在!\n");return;}}strcpy(user[userlo].dir[user[userlo].dirnum].dirname,ch1);user[userlo].dir[user[userlo].dirnum].coflag=0;user[userlo].dir[user[userlo].dirnum].filenum=0;user[userlo].dirnum++;printf("目录创建成功!\n");}void delete_dir(int userlo,char* ch1){int dflag=-1;for(int i=0;i<user[userlo].dirnum;i++){if(strcmp(user[userlo].dir[i].dirname,ch1)==0){dflag=i;break;}}if(dflag==-1){printf("该目录不存在!\n");}else{for(int j=i;j<user[userlo].dirnum-1;j++){strcpy(user[userlo].dir[j].dirname,user[userlo].dir[j+1].dirname);for(int k=0;k<user[userlo].dir[j+1].filenum;k++){strcpy(user[userlo].dir[j].file[k].filename,user[userlo].dir[j+1].file[k].filename);strcpy(user[userlo].dir[j].file[k].content,user[userlo].dir[j+1].file[k].content);user[userlo].dir[j].file[k].fileflag=user[userlo].dir[j+1].file[k].fileflag;}user[userlo].dir[j].filenum=user[userlo].dir[j+1].filenum;user[userlo].dir[j].coflag=user[userlo].dir[j+1].coflag;}printf("删除成功!\n");user[userlo].dirnum--;}}void re_dirname(int userlo,char* ch1,char* ch2){for(int i=0;i<user[userlo].dirnum;i++){if(strcmp(user[userlo].dir[i].dirname,ch1)==0){for(int j=0;j<user[userlo].dirnum;j++){if(strcmp(user[userlo].dir[j].dirname,ch2)==0){printf("你更换的新目录名已经存在!");return;}}strcpy(user[userlo].dir[i].dirname,ch2);printf("重名名成功!\n");return;}}printf("无此目录!无法重命名!\n");}void enter_file(int userlo,int filelo){while(1){char action[19];char ch1[19];char ch2[19];printf("->");if(scanf("%s",action)&&strcmp(action,"help")==0){printf("---------------------———————————\n");//printf("* help 帮助!*\n");printf("- dir 显示文件列表!-\n");printf("- open 文件名打开文件!-\n");printf("- read 文件名读取文件!-\n");printf("- create 文件名文件内容创建新文件!-\n");printf("- delete 文件名删除文件! -\n");printf("- rename 旧文件名新文件名为文件重命名!-\n");printf("- addstr 文件名更新内容更新文件!-\n");printf("- ... 返回上一层!-\n");//printf("- quit 退出登陆!-\n");printf("----------------------———————————\n");}else if(strcmp(action,"dir")==0){show_file(userlo,filelo);}// else if(strcmp(action,"quit")==0)// {// break;// }else if(strcmp(action,"...")==0){break;}else if(strcmp(action,"open")==0){scanf("%s",&ch1);open_file(userlo,ch1,filelo);}else if(strcmp(action,"close")==0){scanf("%s",&ch1);close_file(userlo,ch1,filelo);}else if(strcmp(action,"read")==0){scanf("%s",&ch1);read_file(userlo,ch1,filelo);}else if(strcmp(action,"create")==0){//scanf("%s%s",&ch1,&ch2);scanf("%s\n",&ch1);scanf("%s",&ch2);create_file(userlo,ch1,ch2,filelo);}else if(strcmp(action,"delete")==0){scanf("%s",&ch1);delete_file(userlo,ch1,filelo);}else if(strcmp(action,"rename")==0){scanf("%s%s",&ch1,&ch2);re_filename(userlo,ch1,ch2,filelo);}else if(strcmp(action,"addstr")==0){scanf("%s%s",&ch1,&ch2);add_filestr(userlo,ch1,ch2,filelo);}else{printf("%s不是系统内部命令!\n",action);return;}}}void show_file(int userlo,int filelo){if(user[userlo].dir[filelo].filenum==0){printf("无文件!\n");return;} printf("文件名\n");for(int i=0;i<user[userlo].dir[filelo].filenum;i++){printf("%-26s",user[userlo].dir[filelo].file[i].filename);//printf("%-26s\n",user[userlo].dir[filelo].file[i].filecha_time);}}void open_file(int userlo,char* ch1,int filelo){for(int i=0;i<user[userlo].dir[filelo].filenum;i++){if(strcmp(user[userlo].dir[filelo].file[i].filename,ch1)==0){if(user[userlo].dir[filelo].file[i].fileflag==1){printf("该文件已经打开!\n");return;}else{user[userlo].dir[filelo].file[i].fileflag=1;printf("打开成功!\n");return;}}}printf("该文件不存在!\n");}void close_file(int userlo,char* ch1,int filelo){for(int i=0;i<user[userlo].dir[filelo].filenum;i++){if(strcmp(user[userlo].dir[filelo].file[i].filename,ch1)==0){ if(user[userlo].dir[filelo].file[i].fileflag==0){printf("该文件未打开!\n");return;}else{user[userlo].dir[filelo].file[i].fileflag=0;return;}}}printf("你要关闭的文件文件不存在!\n");}void read_file(int userlo,char* ch1,int filelo){for(int i=0;i<user[userlo].dir[filelo].filenum;i++){if(strcmp(user[userlo].dir[filelo].file[i].filename,ch1)==0){ if(user[userlo].dir[filelo].file[i].fileflag==0){printf("请先打开该文件,然后在读取!\n");return;}else{printf("%s\n",user[userlo].dir[filelo].file[i].content);return;}}}printf("该文件不存在!");return;}void create_file(int userlo,char* ch1,char* ch2,int filelo){for(int i=0;i<user[userlo].dir[filelo].filenum;i++){if(strcmp(user[userlo].dir[filelo].file[i].filename,ch1)==0){printf("有同名文件存在!\n");return;}}printf("输入内容\n");//strcpy(user[userlo].dir[filelo].file[user[userlo].dir[filelo].filenum].filecha_time,nowtime());strcpy(user[userlo].dir[filelo].file[user[userlo].dir[filelo].filenum].filename,ch1);strcpy(user[userlo].dir[filelo].file[user[userlo].dir[filelo].filenum].content,ch2);user[userlo].dir[filelo].file[user[userlo].dir[filelo].filenum].fileflag=0;user[userlo].dir[filelo].filenum++;printf("文件创建成功\n");}void delete_file(int userlo,char* ch1,int filelo){int mflag=-1;for(int i=0;i<user[userlo].dir[filelo].filenum;i++){if(strcmp(user[userlo].dir[filelo].file[i].filename,ch1)==0){mflag=i;break;}}if(mflag==-1){printf("该文件不存在!\n");}else{if(user[userlo].dir[filelo].file[mflag].fileflag==1){printf("删除前请先关闭文件");return;}else{for(int j=i;j<user[userlo].dir[filelo].filenum-1;j++){strcpy(user[userlo].dir[filelo].file[j].filename,user[userlo].dir[filelo].file[j+1].filename); //strcpy(user[userlo].dir[filelo].file[j].filecha_time,user[userlo].dir[filelo].file[j+1].filecha_time);strcpy(user[userlo].dir[filelo].file[j].content,user[userlo].dir[filelo].file[j+1].content);user[userlo].dir[filelo].file[j].fileflag=user[userlo].dir[filelo].file[j+1].fileflag;}printf("删除成功!\n");user[userlo].dir[filelo].filenum--;}}}void re_filename(int userlo,char* ch1,char* ch2,int filelo){for(int i=0;i<user[userlo].dir[filelo].filenum;i++){if(strcmp(user[userlo].dir[filelo].file[i].filename,ch1)==0){for(int j=0;j<user[userlo].dir[filelo].filenum;j++){if(strcmp(user[userlo].dir[filelo].file[j].filename,ch2)==0){printf("你要更换的新文件名已经存在!\n");return;}}strcpy(user[userlo].dir[filelo].file[i].filename,ch2);//strcpy(user[userlo].dir[filelo].file[i].filecha_time,nowtime());printf("重名名成功!\n");return;}}printf("无此文件!无法重命名!\n");}void add_filestr(int userlo,char* ch1,char* ch2,int filelo){for(int i=0;i<user[userlo].dir[filelo].filenum;i++){if(strcmp(user[userlo].dir[filelo].file[i].filename,ch1)==0){if(user[userlo].dir[filelo].file[i].fileflag==0){printf("请先打开文件再进行更新!\n");return;}else{strcat(user[userlo].dir[filelo].file[i].content,ch2);//strcpy(user[userlo].dir[filelo].file[i].filecha_time,nowtime());printf("更新成功!\n");return;}}}printf("无此文件!无法更新!\n");}//char* nowtime(){// time_t t = time(0);// char tmp[64];// strftime( tmp, sizeof(tmp), "%Y/%m/%d %X",localtime(&t) ); // return (char*)tmp;//}。

操作系统课程设计-模拟一个简单二级文件管理系统

操作系统课程设计-模拟一个简单二级文件管理系统

模拟一个简单二级文件管理系统设计目的:通过具体的文件存储空间的管理、文件的物理结构、目录结构和文件操作的实现,加深对文件系统内部功能和实现过程的理解。

设计内容:模拟一个简单二级文件管理系统一、实验内容描述1 实验目标本实验的目的是通过一个简单多用户文件系统的设计,加深理解文件系统的内部功能及内部实现.2 实验要求为DOS系统设计一个简单的二级文件系统.要求做到以下几点:①可以实现下列命令:login 用户登录dir 列文件目录create 创建文件delete 删除文件open 打开文件close 关闭文件read 读文件write 写文件②列目录时要列出文件名、物理地址、保护码和文件长度.③源文件可以进行读写保护.二、程序主要内容1设计思路程序中要求每个用户在登陆后才可对其拥有的文件进行操作,用户对于其他用户的文件无操作权.文件操作包括浏览、创建、删除、打开、关闭、阅读、写入、修改模式.其他操作包括新建用户、帮助、用户登入、用户登出、退出系统.在程序文件夹下有个名为“file”的系统根目录,此目录下包括:一个名为“mfd”的文件,记录所有注册过的帐号及密码;用户文件,以用户名作为文件名,内容为其拥有的文件名及属性;一个名为“keiji”的文件夹.“keiji”文件夹中包括:“file.p”指针文件,记录所有已用的物理地址;一些以物理地址为名的文件,内容为文件内容.2 数据结构file结构体系统文件数据结构:fpaddrint,文件的物理地址、flengthint,文件长度、fmodeint,文件模式0.只读;1.可写;2.可读写;3.保护、fname[]char,文件名;filemode结构体文件状态数据结构:isopenint,文件当前状态,0.关闭;1.打开、modeint,文件模式0.只读;1.可写;2.可读写;3.初始化;user结构体用户信息数据结构:uname[]char,用户名、upassword[]char,用户密码;userfile结构体用户文件数据结构:uname[]char,用户名、ufile[]file,用户拥有的文件数组.代码:#include <stdio.h>#include <stdlib.h>#include <conio.h>#include <time.h>#include <string.h>#define MaxUser 100 //定义最大MDF主目录文件#define MaxDisk 512*1024 //模拟最大磁盘空间#define commandAmount 12 //对文件操作的指令数//存储空间管理有关结构体和变量char disk[MaxDisk]; //模拟512K的磁盘存储空间typedef struct distTable //磁盘块结构体{int maxlength;int start;int useFlag;distTable *next;}diskNode;diskNode *diskHead;struct fileTable //文件块结构体{char fileName[10];int strat; //文件在磁盘存储空间的起始地址int length; //文件内容长度int maxlength; //文件的最大长度char fileKind[3]; //文件的属性——读写方式struct tm *timeinfo;bool openFlag; //判断是否有进程打开了该文件//fileTable *next;};//两级目录结构体typedef struct user_file_directory //用户文件目录文件UFD {//char fileName[10];fileTable *file;user_file_directory *next;}UFD;//UFD *headFile;typedef struct master_file_directory //主文件目录MFD{char userName[10];char password[10];UFD *user;}MFD;MFD userTable[MaxUser];int used=0; //定义MFD目录中用已有的用户数//文件管理void fileCreate(char fileName[],int length,char fileKind[]); //创建文件void fileWrite(char fileName[]); //写文件void fileCat(char fileName[]); //读文件void fileRen(char fileName[],char rename[]); //重命名文件void fileFine(char fileName[]); //查询文件void fileDir(char UserName[]); //显示某一用户的所有文件void fileClose(char fileName[]); //关闭已打开的文件void fileDel(char fileName[]); //删除文件void chmod(char fileName[],char kind[]); //修改文件的读写方式int requestDist(int &startPostion,int maxLength); //磁盘分配查询void initDisk(); //初始化磁盘void freeDisk(int startPostion); //磁盘空间释放void diskShow(); //显示磁盘使用情况//用户管理void userCreate();int login();int userID=-1; //用户登录的ID号,值为-1时表示没有用户登录int main(){char order[commandAmount][10];strcpy(order[0],"create");strcpy(order[1],"rm");strcpy(order[2],"cat");strcpy(order[3],"write");strcpy(order[4],"fine");strcpy(order[5],"chmod");strcpy(order[6],"ren");strcpy(order[7],"dir");strcpy(order[8],"close");strcpy(order[9],"return");strcpy(order[10],"exit");strcpy(order[11],"df");charcommand[50],command_str1[10],command_str2[10],command_str3[5],command_str4[3];int i,k,j;int length;initDisk(); //初始化磁盘for(i=0;i<MaxUser;i++) //初始化用户UFD目录文件的头指针{userTable[i].user=(UFD *)malloc(sizeof(UFD));userTable[i].user->next=NULL;}while(1){printf("********************************************\n");printf(" 1、Creat user\n");printf(" 2、login\n");printf("********************************************\n");printf("Please chooce the function key:>");int choice;scanf("%d",&choice);if(choice==1) userCreate();else if(choice==2) userID=login();else printf("您的输入有误,请重新选择\n");while(userID!=-1){fflush(stdin);printf("———————————————————————————————————————\n");printf(" create-创建格式:create a1 1000 rw,将创建名为a1,长度为1000字节可读可写的文件\n");printf(" rm-删除格式:rm a1,将删除名为a1的文件\n");printf(" cat-查看文件内容格式:cat a1,显示a1的内容\n");printf(" write-写入格式:write a1\n");printf(" fine-查询格式:fine a1 ,将显示文件a1的属性\n");printf(" chmod-修改格式:chmod a1 r,将文件a1的权限改为只读方式\n");printf(" ren-重命名格式:ren a1 b1 ,将a1改名为b1\n");printf(" dir-显示文件格式:dir aaa,将显示aaa用户的所有文件\n");printf(" df-显示磁盘空间使用情况格式:df\n");printf(" close-关闭文件格式:close a1,将关闭文件a1\n");printf(" return-退出用户,返回登录界面\n");printf(" exit-退出程序\n");printf("————————————————————————————————————————\n");printf("please imput your command:>");gets(command);int select;for(i=0;command[i]!=' '&&command[i]!='\0';i++) //command_str1字符串存储命令的操作类型command_str1[i]=command[i];k=i;command_str1[k]='\0';for(i=0;i<commandAmount;i++){if(!strcmp(command_str1,order[i])){select=i;break;}}if(i==commandAmount){printf("您输入的命令有误,请重新输入\n");continue;}for(i=k+1,k=0;command[i]!=' '&&command[i]!='\0';i++,k++) //commmand_str2字符串存储文件名或用户名command_str2[k]=command[i];command_str2[k]='\0';k=i;switch(select){case 0:for(i=k+1,k=0;command[i]!=' ';i++,k++)command_str3[k]=command[i];command_str3[k]='\0';k=i;j=1;length=0; //初始化文件长度for(i=strlen(command_str3)-1;i>=0;i--) //把字符串转换为十进制{length+=(command_str3[i]-48)*j;j*=10;}for(i=k+1,k=0;command[i]!=' '&&command[i]!='\0';i++,k++)command_str4[k]=command[i];command_str4[k]='\0';fileCreate(command_str2,length,command_str4);break;case 1:fileDel(command_str2);break;case 2:fileCat(command_str2);break;case 3:fileWrite(command_str2);break;case 4:fileFine(command_str2);break;case 5:for(i=k+1,k=0;command[i]!=' '&&command[i]!='\0';i++,k++)command_str3[k]=command[i];command_str3[k]='\0';chmod(command_str2,command_str3);break;case 6:for(i=k+1,k=0;command[i]!='\0';i++,k++)command_str3[k]=command[i];command_str3[k]='\0';fileRen(command_str2,command_str3);break;case 7:fileDir(command_str2);break;case 8:fileClose(command_str2);break;case 9:UFD *p;for(p=userTable[userID].user->next;p!=NULL;p=p->next) //退出用户之前关闭所有打的文件if(p->file->openFlag)p->file->openFlag=false;system("cls");userID=-1;break;case 10:exit(0);break;case 11:diskShow();break;}}}return 0;}void userCreate(){char c;char userName[10];int i;if(used<MaxUser){printf("请输入用户名:");for(i=0;c=getch();i++){if(c==13) break;elseuserName[i]=c;printf("%c",c);}userName[i]='\0';for(i=0;i<used;i++){if(!strcmp(userTable[i].userName,userName)){printf("\n");printf("该用户名已存在,创建用户失败\n");system("pause");return;}}strcpy(userTable[used].userName,userName);printf("\n");printf("请输入密码:");for(i=0;c=getch();i++){if(c==13) break;elseuserTable[used].password[i]=c;printf("*");}userTable[userID].password[i]='\0';printf("\n");printf("创建用户成功\n");used++;system("pause");}else{printf("创建用户失败,用户已达到上限\n");system("pause");}fflush(stdin);}int login(){char name[10],psw[10];char c;int i,times;printf("请输入用户名:");for(i=0;c=getch();i++){if(c==13) break;elsename[i]=c;printf("%c",c);}name[i]='\0';for(i=0;i<used;i++){if(!strcmp(userTable[i].userName,name))break;}if(i==used){printf("\n您输入的用户名不存在\n");system("pause");return -1;}for(times=0;times<3;times++){memset(psw,'\0',sizeof(psw));printf("\n请输入密码:");for(i=0;c=getch();i++){if(c==13) break;elsepsw[i]=c;printf("*");}printf("\n");for(i=0;i<used;i++){if(!strcmp(psw,userTable[i].password)){printf("用户登录成功\n");system("pause");break;}}if(i==used){printf("您输入的密码错误,您还有%d次输入机会\n",2-times);if(times==2) exit(0);}else break;}fflush(stdin);return i;}void initDisk(){diskHead=(diskNode *)malloc(sizeof(diskNode));diskHead->maxlength=MaxDisk;diskHead->useFlag=0;diskHead->start=0;diskHead->next=NULL;}int requestDist(int &startPostion,int maxLength){int flag=0; //标记是否分配成功diskNode *p,*q,*temp;p=diskHead;while(p){if(p->useFlag==0&&p->maxlength>maxLength){startPostion=p->start;q=(diskNode *)malloc(sizeof(diskNode));q->start=p->start;q->maxlength=maxLength;q->useFlag=1;q->next=NULL;diskHead->start=p->start+maxLength;diskHead->maxlength=p->maxlength-maxLength;flag=1;temp=p;if(diskHead->next==NULL) diskHead->next=q;else{while(temp->next) temp=temp->next;temp->next=q;}break;}p=p->next;}return flag;}void fileCreate(char fileName[],int length,char fileKind[]){//int i,j;time_t rawtime;int startPos;UFD *fileNode,*p;for(p=userTable[userID].user->next;p!=NULL;p=p->next){if(!strcmp(p->file->fileName,fileName)){printf("文件重名,创建文件失败\n");system("pause");return;}}if(requestDist(startPos,length)){fileNode=(UFD *)malloc(sizeof(UFD));fileNode->file=(fileTable *)malloc(sizeof(fileTable)); //这一步必不可少,因为fileNode里面的指针也需要申请地址,否则fileNode->file指向会出错strcpy(fileNode->file->fileName,fileName);strcpy(fileNode->file->fileKind,fileKind);fileNode->file->maxlength=length;fileNode->file->strat=startPos;fileNode->file->openFlag=false;time(&rawtime);fileNode->file->timeinfo=localtime(&rawtime);fileNode->next=NULL;if(userTable[userID].user->next==NULL)userTable[userID].user->next=fileNode;else{p=userTable[userID].user->next;while(p->next) p=p->next;p->next=fileNode;}printf("创建文件成功\n");system("pause");}{printf("磁盘空间已满或所创建文件超出磁盘空闲容量,磁盘空间分配失败\n");system("pause");}}void freeDisk(int startPostion){diskNode *p;for(p=diskHead;p!=NULL;p=p->next){if(p->start==startPostion)break;}p->useFlag=false;}void fileDel(char fileName[]){UFD *p,*q,*temp;q=userTable[userID].user;p=q->next;while(p){if(!strcmp(p->file->fileName,fileName)) break;else{p=p->next;q=q->next;}}if(p){if(p->file->openFlag!=true) //先判断是否有进程打开该文件{temp=p;q->next=p->next;freeDisk(temp->file->strat); //磁盘空间回收free(temp);printf("文件删除成功\n");system("pause");}elseprintf("该文件已被进程打开,删除失败\n");system("pause");}}else{printf("没有找到该文件,请检查输入的文件名是否正确\n");system("pause");}}void fileCat(char fileName[]){int startPos,length;int k=0;UFD *p,*q;q=userTable[userID].user;for(p=q->next;p!=NULL;p=p->next){if(!strcmp(p->file->fileName,fileName))break;}if(p){startPos=p->file->strat;length=p->file->length;p->file->openFlag=true; //文件打开标记printf("*****************************************************\n");for(int i=startPos;k<length;i++,k++){if(i%50==0) printf("\n"); //一行大于50个字符换行printf("%c",disk[i]);}printf("\n\n*****************************************************\n");printf("%s已被read进程打开,请用close命令将其关闭\n",p->file->fileName);system("pause");}else{printf("没有找到该文件,请检查输入的文件名是否正确\n");system("pause");}}void fileWrite(char fileName[]){UFD *p,*q;q=userTable[userID].user;int i,k,startPos;for(p=q->next;p!=NULL;p=p->next){if(!strcmp(p->file->fileName,fileName))break;}if(p){if(!strcmp(p->file->fileKind,"r")) //判断文件类型{printf("该文件是只读文件,写入失败\n");system("pause");return;}char str[500];printf("please input content:\n");gets(str);startPos=p->file->strat;p->file->openFlag=true; //文件打开标记p->file->length=strlen(str);if(p->file->length>p->file->maxlength){printf("写入字符串长度大于该文件的总长度,写入失败\n");system("pause");return;}for(i=startPos,k=0;k<(int)strlen(str);i++,k++)disk[i]=str[k];printf("文件写入成功,请用close命令将该文件关闭\n");system("pause");}else{printf("没有找到该文件,请检查输入的文件名是否正确\n");system("pause");}}void fileFine(char fileName[])UFD *p,*q;q=userTable[userID].user;for(p=q->next;p!=NULL;p=p->next){if(!strcmp(p->file->fileName,fileName))break;}if(p){printf("********************************************\n");printf("文件名:%s\n",p->file->fileName);printf("文件长度:%d\n",p->file->maxlength);printf("文件在存储空间的起始地址:%d\n",p->file->strat);printf("文件类型:%s\n",p->file->fileKind);printf("创建时间:%s\n",asctime(p->file->timeinfo));printf("********************************************\n");system("pause");}else{printf("没有找到该文件,请检查输入的文件名是否正确\n");system("pause");}}void chmod(char fileName[],char kind[]){UFD *p,*q;q=userTable[userID].user;for(p=q->next;p!=NULL;p=p->next){if(!strcmp(p->file->fileName,fileName))break;}if(p){strcpy(p->file->fileKind,kind);printf("修改文件类型成功\n");system("pause");}else{printf("没有找到该文件,请检查输入的文件名是否正确\n");system("pause");}}void fileRen(char fileName[],char name[]){UFD *p,*q;q=userTable[userID].user;for(p=q->next;p!=NULL;p=p->next){if(!strcmp(p->file->fileName,fileName))break;}if(p){while(q->next){if(!strcmp(q->next->file->fileName,name)){printf("您输入的文件名已存在,重命名失败\n");system("pause");return;}q=q->next;}strcpy(p->file->fileName,name);printf("重命名成功\n");system("pause");}else{printf("没有找到该文件,请检查输入的文件名是否正确\n");system("pause");}}void fileDir(char userName[]){UFD *p;int i,k;for(i=0;i<MaxUser;i++){if(!strcmp(userTable[i].userName,userName)){k=i;break;}}if(i==MaxUser){printf("没有找到该用户,请检查输入用户名是否正确\n");system("pause");return;}else{p=userTable[k].user->next;printf("************************************************************************* *******\n");printf("文件名文件长度文件在磁盘的起始地址文件类型创建时间\n");for(;p!=NULL;p=p->next)printf("%s %d %d %s %s",p->file->fileName, p->file->maxlength,p->file->strat,p->file->fileKind,asctime(p->file->timeinfo));printf("************************************************************************* *******\n");system("pause");}}void diskShow(){diskNode *p;int i=0,unusedDisk=0;printf("************************************************************************* **\n");printf(" 盘块号起始地址容量(bit) 是否已被使用\n");for(p=diskHead;p!=NULL;p=p->next,i++){if(p->useFlag==false) unusedDisk+=p->maxlength;printf(" %d %d %d %d \n",i,p->start,p->maxlength,p->useFlag);}printf("************************************************************************* **\n");printf("磁盘空间总容量:512*1024bit 已使用:%dbit 末使用:%dbit\n\n",MaxDisk-unusedDisk,unusedDisk);system("pause");}void fileClose(char fileName[]){UFD *p,*q;q=userTable[userID].user;for(p=q->next;p!=NULL;p=p->next){if(!strcmp(p->file->fileName,fileName))break;}if(p){p->file->openFlag=false;printf("%s文件已关闭\n",p->file->fileName);system("pause");}else{printf("没有找到该文件,请检查输入的文件名是否正确\n");system("pause");}}运行结果视图:(略)。

操作系统设计一个二级文件系统报告

操作系统设计一个二级文件系统报告

v .. . ..
. . . 资 料. .
操作系统(2014年秋季学期)
实 验 报 告
系别:计算机科学与技术 班级:信安12-1班 姓名:*** 学号:
实验名称:进程调度
NORTH CHINA UNIVERSITY OF TECHNOLOGY
2022-4-26 2/17
.
Word 资料
NORTH CHINA UNIVERSITY OF TECHNOLOGY
2022-4-26 4/17
.
Word 资料
NORTH CHINA UNIVERSITY OF TECHNOLOGY
.
Word 资料
NORTH CHINA UNIVERSITY OF TECHNOLOGY
.
Word 资料
NORTH CHINA UNIVERSITY OF TECHNOLOGY
}
五、编译过程截图
(下面是一个例子,换上你自己的图)
六、测试用例
(下面是一个例子,换上你自己的)
文字叙述一下测试过程的实例。

如先创建用户***、再产生文件****、再打开文件****,再写入内容********,再退出,再打开用户***,再将文件读出,读出的内容应该是*******。

等等。

六、实验结果
(下面是一个例子,换上你自己的图,给出根据测试用例的截图)。

操作系统课程设计文件管理二级文件系统

操作系统课程设计文件管理二级文件系统

本文档分别包含linux和windows两套程序目录一、课程设计的目的..........................二、课程设计的内容及要求...................内容..........................................要求..........................................三、实现原理.................................四、关键算法实现流程图 .....................流程图........................................关键算法......................................五、软件运行环境及限制 .....................六.结果输出及分析..........................七.心得体会.................................八.参考文献.................................一.课程设计的目的本课程设计是学习完《计算机操作系统》课程后,进行的一次全面的综合训练,通过课程设计,让我更好地掌握操作系统的原理及实现方法,加深对操作系统基础理论和重要算法的理解,加强了我的动手能力。

二.课程设计的内容及要求内容:为LINUX 设计一个简单的二级文件系统。

要求做到以下几点:1、可以实现下列几条命令(至少4条)。

Login 用户登录Dir 列文件目录Create 创建文件Delete 删除文件Open 打开文件Close 关闭文件Read 读文件Write 写文件2、列目录时要列出文件名、物理地址、保护码和文件长度。

3、源文件可以进行读写保护。

从课程设计的目的出发,通过设计工作的各个环节,达到以下教学要求:1、鼓励并优先个人独立选题设计,可从下面设计题目中选择,也可以自拟相关题目;如要合作,每组最多两名同学,且设计文档不能相同;2、可以在我们实验课最后布置的实验设计基础上进行完善和改进,但不能相同。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

《操作系统》课程设计
题目:设计一个简单二级文件系统
专业:计算机科学与技术
年级:文专计081—1
*名:***
学号:************
指导教师:***
时间:2010.7.12---2010.7.16
2010年7 月15日
一、设计内容
1、可以实现下列几条命令。

Format 格式化
Dir 列文件目录
Create 创建文件
Delete 删除文件
Deldir 删除目录
Open 打开文件
Close 关闭文件
Search 查询文件
Copy 拷贝文件
Cut 剪切文件
二、开发环境
Windows操作系统
Microsoft Visual C++
三、分析设计
(一)实验原理
通过程序模拟Linux文件系统,用一个二进制文件(FileSystem.disk)来模拟磁盘.设计一个多用户的二级文件系经统、实现一般的创建文件、目录,删除文件、目录,切换目录,打开、关闭文件、读写文件等操作。

文件系统,包含格式化,显示文件(目录),创建文件等几个简单命令的实现,而且能完成超级块的读写,节点的读写等过程. 本文件系统采用两级目录,其中第一级对应
于用户账号,第二级对应于用户账号下的文件。

另外,为了简单本文件系统未考虑文件共享、文件系统安全以及管道文件与设备文件等特殊内容。

1.程序执行流程图:
2.数据块的分配和回收
(二)程序结构
设计FileSystem类负责管理磁盘空间和磁盘内存I节点,负责对磁盘空间和磁盘数据进行优化管理。

并提代接口言方法供用户或程序调用。

(三)数据结构
int physic[100]; //文件地址缓冲区
int style=1; //文件的类型
char cur_dir[10]="root"; //当前目录
int search_i=0;
struct command
{
char com[10];
}cmd[20];
struct block
{
int n; //空闲的盘快的个数
int free[50]; //存放空闲盘快的地址
int a; //模拟盘快是否被占用
}memory[20449];
struct block_super
{
int n; //空闲的盘快的个数
int free[50]; //存放进入栈中的空闲块
int stack[50]; //存放下一组空闲盘快的地址
}super_block;
struct node //i结点信息
{
int file_style; //i结点文件类型
int file_length; //i结点文件长度
int file_address[100]; //i结点文件的物理地址 char file_message[100];
} i_node[640];
struct dir //目录项信息
{
char file_name[10]; //文件名
int i_num; //文件的结点号
char dir_name[10]; //文件所在的目录
} root[640];
四.运行示例及结果分析
菜单
删除目录文件查询
拷贝
剪切
五、程序实现
详细程序请参见源文件, 在此只列举3点
1、剪切文件
程序片段:void cut(char *tmp,char *newname) {
int i; //,j
char t[20];
_strtime(t);
for(i=0;i<640;i++)
if(strcmp(newname,root[i].file_name)==0)
{
break;
}
if(i==640)
{
printf("目录不存在,不能剪切!\n");
return;
}
for(i=0;i<640;i++)
{
if(strcmp(tmp,root[i].file_name)==0)
{
strcpy(root[i].dir_name,newname);
strcpy(i_node[root[i].i_num].change_t,t);//
printf("剪切成功!\n");//
return;
}
}
if(i==640)
printf("文件不存在,执行失败!\n");
}
2.无文件查询
程序片段:void search(char* filename){
int sign=0;
for(search_i=0;search_i<640;search_i++)
{
if(strcmp(root[search_i].file_name,filename)==0) //查询文件中所在目录信息和当前目录信息相同的数据
{
sign=1;
int k=root[search_i].i_num;
printf("%s\t",root[search_i].file_name); //文件名
printf(" %d\t",i_node[k].file_style); //文件的类型
printf(" %d\t\t",i_node[k].file_length); //文件的长度
printf("%s\n",root[search_i].dir_name); //文件所在的目录
}
}
if (sign==0)
{
printf("%s\n","未找到该文件!");
}
}
六、心得与体会
这次设计中遇到最难的问题就是拷贝功能的实现,由于之前没有接触过拷贝的原理,所以通过和同伴的讨论和试验,终于突发奇想的运用改变文件路径的源代码和创建文件相结合,终于实现了拷贝功能,能够将一个文件从一个目录拷贝到另一目录(即先移动过去然后再在原位置创建一个原文件),可是却无法拷贝到root根目录,为了解决这个问题,又在创建文件的程序里添加了一个判断是否为根目录的语句,可是后来又发现了真个系统存在重命名的问题。

设计在逐步深入,功能在逐渐健全,可是问题也就随之暴露的更多,逐步的解决问题,我想,这才是我们课程设计的意义吧。

主要解决了拷贝问题,剪切问题和无文件查询问题。

可是由于C++编程语言掌握的不是很好,程序编写的很普通,而且很多不足,例如:查询的编写过程中没有做出模糊查询,只做到了精确查询,而且是用了将全部磁盘块查询一遍的方法;拷贝中文件可以重名,无法提示用户文件重名;剪切无法做到从一子目录剪切到根目录。

这些问题还有待深入学习和探讨。

通过几天来的设计和学习,真的学习到了很多东西。

通过这次的操作系统的课程设计,在老师的细心指导和同学的积极讨论下,终于做出了模拟Linux二级文件系统,能够简单得实现目录的创建和删除,文件的建立和删除,文件的读写等这些基本操作,并且着重改编了拷贝、剪切和查询的功能,了解二级目录的有关内容,并且通过编写的模拟Lunix下的操作环境有了更进一步的了解。

最后,感谢老师的指导。

相关文档
最新文档