实验9-proc文件系统的实现

本文详细介绍了在Linux 0.11内核中实现proc文件系统的过程,包括创建虚拟文件节点,如psinfo和hdinfo,以及修改内核源码以支持内存文件类型的读取。探讨了文件视图在Linux系统中的应用,以及如何通过统一的文件视图接口访问各种设备。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

实验九

实验目的

  • 掌握虚拟文件系统的实现原理
  • 实践文件、目录、文件系统等概念

实验内容

在Linux 0.11上实现procfs(proc文件系统)内的psinfo结点。当读取此结点的内容时,可得到系统当前所有进程的状态信息。例如,用cat命令显示/proc/psinfo的内容,可得到:

$ cat /proc/psinfo
pid    state    father    counter    start_time
0    1    -1    0    0
1    1    0    28    1
4    1    1    1    73
3    1    1    27    63
6    0    4    12    817
$ cat /proc/hdinfo
total_blocks:    62000;
free_blocks:    39037;
used_blocks:    22963;
...

procfs及其结点要在内核启动时自动创建。

相关功能实现在fs/proc.c文件内。

实验报告

文件视图的分析

Linux0.11系统中很重要的一个就是将外设均虚拟化为文件访问,通过统一的文件视图接口来实现所有外设的访问,包括硬盘的读写,键盘及打印机等的使用,均使用统一的文件视图接口来完成。这样如果有新的设备添加进来,那么访问的新外设的方法也是统一的。这些可以通过下面对文件视图的代码分析清楚地了解。

1、实验分析及实现代码

对设备进行读写均通过open函数来完成,包括对显示的输出。系统提供了一个sys_open()函数来完成。其代码如下:

int sys_open(const char * filename,int flag,int mode)
{
    struct m_inode * inode;
    struct file * f;
    int i,fd;

    mode &= 0777 & ~current->umask;
    for(fd=0 ; fd<NR_OPEN ; fd++)
        if (!current->filp[fd])
            break;
    if (fd>=NR_OPEN)
        return -EINVAL;
    current->close_on_exec &= ~(1<<fd);
    f=0+file_table;
    for (i=0 ; i<NR_FILE ; i++,f++)
        if (!f->f_count) break;
    if (i>=NR_FILE)
        return -EINVAL;
    (current->filp[fd]=f)->f_count++;
    if ((i=open_namei(filename,flag,mode,&inode))<0) {
        current->filp[fd]=NULL;
        f->f_count=0;
        return i;
    }
/* ttys are somewhat special (ttyxx major==4, tty major==5) */
//如果是字符设备,则对tty设备进行处理,
    if (S_ISCHR(inode->i_mode)) {
        if (MAJOR(inode->i_zone[0])==4) {
            if (current->leader && current->tty<0) {
                current->tty = MINOR(inode->i_zone[0]);
                tty_table[current->tty].pgrp = current->pgrp;
            }
        } else if (MAJOR(inode->i_zone[0])==5)
            if (current->tty<0) {
                iput(inode);
                current->filp[fd]=NULL;
                f->f_count=0;
                return -EPERM;
            }
    }
/* Likewise with block-devices: check for floppy_change */
//如果是块设备,则交由块设备处理函数进行相应处理
    if (S_ISBLK(inode->i_mode))
        check_disk_change(inode->i_zone[0]);
    f->f_mode = inode->i_mode;
    f->f_flags = flag;
    f->f_count = 1;
    f->f_inode = inode;
    f->f_pos = 0;
    return (fd);
}

对所有设备进行访问之前均需要进行open操作,比如,在系统初始化时对终端设备的操作(void) open("/dev/tty0",O_RDWR,0); //打开终端 所以对内存文件的操作也一样如此进行,只要程序在打开文件时可以找到相应的INODE节点就可以进行文件打开操作。所以对此次实验而言,只要可以正确创建inode节点,就可以正确打开文件。所以要对创建节点的函数进行修正已支持新的内存文件类型。由于内存文件中在系统启动时自动添加的,所以还要在系统初始化硬盘后对内存文件的目录节点以及实现了的内存文件进行相应的添加,这个可以通过在根节点下添加一个proc子节点来实现,并且在新的子节点下新建两个已实现的内存文件:psinfo及hdinfo以显示进程和硬盘的信息。但打开后对文件的读操作(内存文件不能进行写操作)却要进行相应修正,因为我们的文件不是存在于硬盘上,而是在内存中生成的,所以要相应修改sys_read操作。当然,在读取inode节点时要对节点的类型进行判断并正确处理,所以也要对namei函数进行相应修正,添加我们的proc文件类型。所以要对stat.h文件中的文件类型进行添加以保证可以顺利读取内存类型文件。这样当系统打开一个内存文件时,会转到内存文件的处理函数中进行相应处理。

\\include\sys\stat.h
#define S_IFMT  00170000
#define S_IFREG  0100000
#define S_IFBLK  0060000
#define S_IFDIR  0040000
/*添加的内存文件类型*/
#define S_IFPROC 0030000
/*添加完毕*/
#define S_IFCHR  0020000
#define S_IFIFO  0010000
#define S_ISUID  0004000
#define S_ISGID  0002000
#define S_ISVTX  0001000

#define S_ISREG(m)    (((m) & S_IFMT) == S_IFREG)
#define S_ISDIR(m)    (((m) & S_IFMT) == S_IFDIR)
/*添加的内存文件类型判断*/
#define S_ISPROC(m)     (((m) & S_IFMT) == S_IFPROC)
/*添加完毕*/
#define S_ISCHR(m)    (((m) & S_IFMT) == S_IFCHR)
#define S_ISBLK(m)    (((m) & S_IFMT) == S_IFBLK)
#define S_ISFIFO(m)    (((m) & S_IFMT) == S_IFIFO)


\\fs\read_write.c
/*修改读文件函数,添加内存文件的读取流程*/
int sys_read(unsigned int fd,char * buf,int count)
{
    struct file * file;
    struct m_inode * inode;

    if (fd>=NR_OPEN || count<0 || !(file=current->filp[fd]))
        return -EINVAL;
    if (!count)
        return 0;
    verify_area(buf,count);
    inode = file->f_inode;
    if (inode->i_pipe)
        return (file->f_mode&1)?read_pipe(inode,buf,count):-EIO;
    if (S_ISCHR(inode->i_mode))
        return rw_char(READ,inode->i_zone[0],buf,count,&file->f_pos);
    if (S_ISBLK(inode->i_mode))
        return block_read(inode->i_zone[0],&file->f_pos,buf,count);
    if (S_ISDIR(inode->i_mode) || S_ISREG(inode->i_mode)) {
        if (count+file->f_pos > inode->i_size)
            count = inode->i_size - file->f_pos;
        if (count<=0)
            return 0;
        return file_read(inode,file,buf,count);
    }
    /*如果是内存文件,则调用内存文件读取函数*/
    if(S_ISPROC(inode->i_mode))
        return proc_read(inode->i_zone[0],&file->f_pos,buf,count);
    /*添加完毕*/
    printk("(Read)inode->i_mode=%06o\n\r",inode->i_mode);
    return -EINVAL;
}

\\fs\proc_dev.c
/*添加的对内存文件进行处理的函数*/
/*
 * linux/fs/proc_dev.c
 * 2015-10-22 melon add for procs system
*/

#include <errno.h>

#include <linux/sched.h>
#include <linux/kernel.h>
 #include <asm/segment.h>
 #include <stdarg.h>
 #include <stddef.h>

/*
 #include <stddef.h>
#include <linux/kernel.h>
*/
extern int vsprintf(char * buf, const char * fmt, va_list args);

/*static char psbuf[2048];*/
/*
* sprintk for print the info for psinfo
*/
 int sprintk(char * buf, const char *fmt, ...)
{
    va_list args;
    int i;

    va_start(args, fmt);
    i=vsprintf(buf,fmt,args);
    va_end(args);
    return i;
}

 /*
* psinfo  to show the info of process 
*/
int psinfo(unsigned long * pos, char * buf, int count)
{
    struct task_struct ** p;
    int output_count=0;
    char * psbuf=NULL;
    int chars=0;
    int offset=*pos;

    if((psbuf=(char *)malloc(sizeof(char *)*1024))==NULL)
    {
        printk("psinfo - malloc error!\n");
        return -EINVAL;
    }

    chars=sprintk(psbuf,"pid\tstate\tfather\tcounter\tstart_time\n");

    for(p = &LAST_TASK ; p >= &FIRST_TASK ; --p)
    {
        if(*p)
        {
            chars+=sprintk(psbuf+chars,"%d\t%d\t%d\t%d\t%d\n",(*p)->pid,(*p)->state,(*p)->father,(*p)->counter,(*p)->start_time);
        }
    }

    *(psbuf+chars)='\0';

    while(count>0)
    {
        if(offset>chars)
        {
            break;
        }

        put_fs_byte(*(psbuf+offset),buf++);
        offset++;
        output_count++;
        count--;

    }

    (*pos)+=output_count;

    free(psbuf);
    return output_count;
}

int hdinfo(unsigned long * pos, char * buf, int count)
{
    struct super_block * sb; 
    struct buffer_head * bh;
    int total_blocks;
    int total_inodes;
    int used_blocks=0;
    int free_blocks=0;
    int i,j,z;
    char * p=NULL;

    int chars=0;
    int offset=*pos;
    int output_count=0;
    char * hdbuf=NULL;

    sb=get_super(current->root->i_dev);
    total_blocks = sb->s_nzones;
    total_inodes=sb->s_ninodes;
    for(i=0;i<sb->s_zmap_blocks;i++)
    {
        bh=sb->s_zmap[i];
        p=(char*)bh->b_data;
        for(j=0;j<1024;j++)
        {
            for(z=1;z<=8;z++)
            {
                if((used_blocks+free_blocks)>=total_blocks)
                    break;
                if( *(p+j) & z)
                    used_blocks++;
                else
                    free_blocks++;
            }
        }
    }

    hdbuf=(char*)malloc(sizeof(char*)*512);
    chars=sprintk(hdbuf,"s_imap_blocks:%d\ns_zmap_blocks:%d\n",sb->s_imap_blocks,sb->s_zmap_blocks);
    chars+=sprintk(hdbuf+chars,"total_blocks:%d\nfree_blcoks:%d\nused_blocks:%d\ntotal_indoes:%d\n",total_blocks,free_blocks,used_blocks,total_inodes);
    //*(hduf+chars)='\n';

    while(count>0)
    {
        if(offset>chars)
            break;

        put_fs_byte(*(hdbuf+offset),buf++);
        offset++;
        output_count++;
        count--;
    }

    (*pos)+=output_count;
    free(hdbuf);
    return output_count;

}

int proc_read(int dev, unsigned long * pos, char * buf, int count)
 {
     if(dev==0)
     {
         return psinfo(pos,buf,count);
     }
     if(dev==1)
     {
         return hdinfo(pos,buf,count);
     }

     return 0;
 }

\\添加了新的文件后要相应修改Makefile文件将新文件添加到内核

OBJS=    open.o read_write.o inode.o file_table.o buffer.o super.o \
    block_dev.o char_dev.o file_dev.o stat.o exec.o pipe.o namei.o \
    bitmap.o fcntl.o ioctl.o truncate.o proc_dev.o

### Dependencies:
bitmap.o: bitmap.c ../include/string.h ../include/linux/sched.h \
  ../include/linux/head.h ../include/linux/fs.h ../include/sys/types.h \
  ../include/linux/mm.h ../include/signal.h ../include/linux/kernel.h
block_dev.o: block_dev.c ../include/errno.h ../include/linux/sched.h \
  ../include/linux/head.h ../include/linux/fs.h ../include/sys/types.h \
  ../include/linux/mm.h ../include/signal.h ../include/linux/kernel.h \
  ../include/asm/segment.h ../include/asm/system.h
proc_dev.o: proc_dev.c ../include/errno.h ../include/linux/sched.h \
  ../include/linux/head.h ../include/linux/fs.h ../include/sys/types.h \
  ../include/linux/mm.h ../include/signal.h ../include/linux/kernel.h \
  ../include/asm/segment.h ../include/asm/system.h ../include/stdarg.h


\\在系统初始化时创建新的节点以及两个内存文件
\\init\main.c
\*由于要在main文件中使用创建节点的函数,所以在这里也要添加对mkdir以及mknod的引用*\
_syscall2(int,mkdir,const char*, name,mode_t,mode)
_syscall3(int,mknod,const char*,filename,mode_t,mode,dev_t,dev)


\\init\main.c\main

    setup((void *) &drive_info); //初始化磁盘工作
    /*在根节点下创建一个新的子节点,并新建两个内存文件*/
    mkdir("proc",0755);
    mknod("/proc/psinfo",S_IFPROC|0444,0);
    mknod("/proc/hdinfo",S_IFPROC|0444,1);
    /*melon - 2015-10-22*/
    (void) open("/dev/tty0",O_RDWR,0);  //打开终端

至此就完成此次实验。

2、实验截图
在这里插入图片描述

回答问题

完成实验后,在实验报告中回答如下问题:

  • 如果要求你在psinfo之外再实现另一个结点,具体内容自选,那么你会实现一个给出什么信息的结点?为什么?

  • 如果再实现另一个结点,我想会选内存使用的信息情况,因为内存是整个系统中最重要的资源,系统的运行要靠内存的支持,能及时了解内存的使用情况是有意义的。

  • 一次read()未必能读出所有的数据,需要继续read(),直到把数据读空为止。而数次read()之间,进程的状态可能会发生变化。你认为后几次read()传给用户的数据,应该是变化后的,还是变化前的? 如果是变化后的,那么用户得到的数据衔接部分是否会有混乱?如何防止混乱? 如果是变化前的,那么该在什么样的情况下更新psinfo的内容?

  • 按照目前的实现方法,如果在几次read之间进程状态发生变化,后几次传递给用户的是变化后的信息,那么可能会导致信息混乱。如果要防止混乱发生,可以考虑将取到的进程信息保存在硬盘的一个文件中,读取时只要该文件存在,就从文件中直接读取,而不是每次read都重新生成进程信息,而一次读动作完成后(返回0)就将硬盘上的文件删除。这样下次再时再次生成文件。这样可以保证传递的数据是一致的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值