linux内核分析笔记----虚拟文件系统(上)

Linux内核源代码情景分析笔记 Linux内核源代码情景分析笔记好吧,首先我承认我要是读者的话,这篇文章我看着也头疼,因为写的太长太泛(其主要部分集中在内存管理,进程管理,文件系统)!原本是想按自己理解的精简精简的,按照操作系统中两个核心的抽象概念“进程”和“文件”开始介绍的,可以说操作系统引入这两个概念是系统的核心,其中进程的管理可以说是对内存和cpu的抽象管理,当然基础内存管理是必须的。而文件可以说是对设备的一层抽象(不仅仅是 阅读详情

      虚拟文件系统,也不知道大家听过没有,反正我是听过了!我们知道在计算机行业,很多东西都不是一定有个官方说:朋友,我最大,你们做的东西,都要是这个样子,否则是非法的。事实上,很多东西都是靠的一种实力,通过实力来慢慢在人们心中成为既定事实。这个事实同样是没有官方的。好了,问题来了,没有官方,就没有标准,没有标准就没有统一,没有统一那就是三国时代,混战当道也!

      怎么办?特别是百花争鸣的文件系统,这时linux的内核开发者们想到了VFS(虚拟文件系统)。VFS使得用户可以直接使用open(),read()和write()这样的系统调用而不用关注具体文件系统和实际物理介质。也许你感觉不是很新奇啊,告诉你新奇的事情:在老式操作系统上(比如DOS),任何对非本地文件系统的访问都必须依靠特殊工具才能完成。这种实现的方式是内核在它的底层文件系统接口上建立了一个抽象层。该抽象层是linux能够支持各种文件系统,即便是它们在功能和行为上存在很大差别。为了支持文件系统,VFS提供了一个通用文件系统模型,该模型囊括了我们所能想到的文件系统的常用功能和行为。这个VFS抽象层之所以能衔接各种各样的文件系统,是因为它定义了所有文件系统都支持的基本抽象接口和数据结构,同时实际系统也将自身的诸如“如何打开文件”,“目录是什么”等概念在形式上与VFS的定义保持一致。因为实际文件系统的代码在统一的接口和数据结构隐藏了具体的实现细节,所以在VFS层和内核的其他部分看来,所有文件系统都是相同的,它们都支持像文件和目录这样的概念,同时也支持像创建和删除文件这样的操作。

      实际文件系统通过编程提供VFS所期望的抽象接口和数据结构,这样,内核就可以毫不费力地和任何文件系统协同工作。那么接下的问题,它们直接的关系如何呢,看下边的例子:

write(f,&buf,len);

      该代码不用说,应该明白。这个用户调用首先被一个通用系统调用sys_write()处理,sys_write()函数要找到f所在的文件系统实际给出的是哪个写操作,然后再执行该操作。实际文件系统的写方法是文件系统实现的一部分,数据最终通过该操作写入介质。下图给出流程:

       image

      下面我就先从整体上对unix(linux)文件系统做个概述,然后在具体下去。Unix使用了四种和文件系统相关的传统抽象概念,如下:

1.文件:就是一个有序字节串。
2.目录项:文件是放在目录中,目录又可以层层嵌套,形成文件路径。路径中的每一项就叫做目录项。目录是文件,这个文件列出了该目录下的所有文件.
3.索引节点:一个文件其实是由两部分组成:相关信息和文件本身。这里的相关信息指的是访问控制权限,大小,拥有者,创建时间等。文件相关信息也叫
   做元数据,被存储在一个单独的数据结构中,这个结构就叫做索引点(index node,简写inode)。
4.安装点(挂载点):文件系统被安装在一个特定的安装点上,该安装点在全局层次结构中被称为命名空间,所有的已安装文件系统都作为根文件树的树叶出
   现在系统中。
5.超级块:是一种包含文件系统信息的数据结构,里边是文件系统的控制信息。

      对应于上图,我们知道VFS是介于用户文件和文件系统之间的一个概念,所以如果文件系统想要穿透VFS供用户空间使用,就必须经过封装,提供一个符合这些概念的界面。上述每个元素都对应一个对象,该对象有属性结构体,描述了该对象的属性。有操作结构体,包含了自身所支持的操作,下面详细介绍:

      1.超级块对象:代表一个已安装的文件系统。由数据结构super_block结构体表示,定义在linux/fs.h中。如下所示:

struct super_block {
        struct list_head        s_list;            /* list of all superblocks */
        dev_t                   s_dev;             /* identifier */
        unsigned long           s_blocksize;       /* block size in bytes */
        unsigned long           s_old_blocksize;   /* old block size in bytes */
        unsigned char           s_blocksize_bits;  /* block size in bits */
        unsigned char           s_dirt;            /* dirty flag */
        unsigned long long      s_maxbytes;        /* max file size */
        struct file_system_type s_type;            /* filesystem type */
        struct super_operations s_op;              /* superblock methods */
        struct dquot_operations *dq_op;            /* quota methods */
        struct quotactl_ops     *s_qcop;           /* quota control methods */
        struct export_operations *s_export_op;     /* export methods */
        unsigned long            s_flags;          /* mount flags */
        unsigned long            s_magic;          /* filesystem's magic number */
        struct dentry            *s_root;          /* directory mount point */
        struct rw_semaphore      s_umount;         /* unmount semaphore */
        struct semaphore         s_lock;           /* superblock semaphore */
        int                      s_count;          /* superblock ref count */
        int                      s_syncing;        /* filesystem syncing flag */
        int                      s_need_sync_fs;   /* not-yet-synced flag */
        atomic_t                 s_active;         /* active reference count */
        void                     *s_security;      /* security module */
        struct list_head         s_dirty;          /* list of dirty inodes */
        struct list_head         s_io;             /* list of writebacks */
        struct hlist_head        s_anon;           /* anonymous dentries */
        struct list_head         s_files;          /* list of assigned files */
        struct block_device      *s_bdev;          /* associated block device */
        struct list_head         s_instances;      /* instances of this fs */
        struct quota_info        s_dquot;          /* quota-specific options */
        char                     s_id[32];         /* text name */
        void                     *s_fs_info;       /* filesystem-specific info */
        struct semaphore         s_vfs_rename_sem; /* rename semaphore */
};

      创建,管理和销毁超级块对象的代码位于文件fs/super.c中,超级块对象通过alloc_super()函数创建并初始化。在文件系统安装时,内核会调用该函数以便从磁盘读取文件系统超级块,并且将其信息填充到内存中的超级块对象中。其中最重要的一个是s_op,指向超级块的操作函数表,由super_operations结构体表示,定义在linux/fs.h中,如下:

struct super_operations {
        struct inode *(*alloc_inode) (struct super_block *sb);
        void (*destroy_inode) (struct inode *);
        void (*read_inode) (struct inode *);
        void (*dirty_inode) (struct inode *);
        void (*write_inode) (struct inode *, int);
        void (*put_inode) (struct inode *);
        void (*drop_inode) (struct inode *);
        void (*delete_inode) (struct inode *);
        void (*put_super) (struct super_block *);
        void (*write_super) (struct super_block *);
        int (*sync_fs) (struct super_block *, int);
        void (*write_super_lockfs) (struct super_block *);
        void (*unlockfs) (struct super_block *);
        int (*statfs) (struct super_block *, struct statfs *);
        int (*remount_fs) (struct super_block *, int *, char *);
        void (*clear_inode) (struct inode *);
        void (*umount_begin) (struct super_block *);
        int (*show_options) (struct seq_file *, struct vfsmount *);
};

      当文件系统需要对其超级块执行操作时,首先要在超级块对象中寻找需要的操作方法。比如一个文件系统要写自己的超级块,需要调用:sb->s_op->write_super(sb)这里的sb是指向文件系统超级块的指针,沿着该指针进入超级块操作函数表,并从表中取得希望得到的write_super()函数,该函数执行写入超级块的实际操作。

      2.索引节点对象:由inode结构体表示,定义在linux/fs.h中,如下:

struct inode {
        struct hlist_node       i_hash;              /* hash list */
        struct list_head        i_list;              /* list of inodes */
        struct list_head        i_dentry;            /* list of dentries */
        unsigned long           i_ino;               /* inode number */
        atomic_t                i_count;             /* reference counter */
        umode_t                 i_mode;              /* access permissions */
        unsigned int            i_nlink;             /* number of hard links */
        uid_t                   i_uid;               /* user id of owner */
        gid_t                   i_gid;               /* group id of owner */
        kdev_t                  i_rdev;              /* real device node */
        loff_t                  i_size;              /* file size in bytes */
        struct timespec         i_atime;             /* last access time */
        struct timespec         i_mtime;             /* last modify time */
        struct timespec         i_ctime;             /* last change time */
        unsigned int            i_blkbits;           /* block size in bits */
        unsigned long           i_blksize;           /* block size in bytes */
        unsigned long           i_version;           /* version number */
        unsigned long           i_blocks;            /* file size in blocks */
        unsigned short          i_bytes;             /* bytes consumed */
        spinlock_t              i_lock;              /* spinlock */
        struct rw_semaphore     i_alloc_sem;         /* nests inside of i_sem */
        struct semaphore        i_sem;               /* inode semaphore */
        struct inode_operations *i_op;               /* inode ops table */
        struct file_operations  *i_fop;              /* default inode ops */
        struct super_block      *i_sb;               /* associated superblock */
        struct file_lock        *i_flock;            /* file lock list */
        struct address_space    *i_mapping;          /* associated mapping */
        struct address_space    i_data;              /* mapping for device */
        struct dquot            *i_dquot[MAXQUOTAS]; /* disk quotas for inode */
        struct list_head        i_devices;           /* list of block devices */
        struct pipe_inode_info  *i_pipe;             /* pipe information */
        struct block_device     *i_bdev;             /* block device driver */
        unsigned long           i_dnotify_mask;      /* directory notify mask */
        struct dnotify_struct   *i_dnotify;          /* dnotify */
        unsigned long           i_state;             /* state flags */
        unsigned long           dirtied_when;        /* first dirtying time */
        unsigned int            i_flags;             /* filesystem flags */
        unsigned char           i_sock;              /* is this a socket? */
        atomic_t                i_writecount;        /* count of writers */
        void                    *i_security;         /* security module */
        __u32                   i_generation;        /* inode version number */
        union {
                void            *generic_ip;         /* filesystem-specific info */
        } u;
};

      有时,某些文件系统可能并不能完整的包含索引节点结构体要求的所有信息。举个例子,有的文件系统可能并不记录文件的创建时间,这时,该文件系统就可以在实现中选择任意合适的办法来解决这个问题,它可以在i_ctime中存储0,或者让i_ctime等于i_mtime,甚至任何其他值。索引节点对象中的inode_operations项存放了操作函数列表,定义在linux/fs.h中,如下:

struct inode_operations {
        int (*create) (struct inode *, struct dentry *,int);
        struct dentry * (*lookup) (struct inode *, struct dentry *);
        int (*link) (struct dentry *, struct inode *, struct dentry *);
        int (*unlink) (struct inode *, struct dentry *);
        int (*symlink) (struct inode *, struct dentry *, const char *);
        int (*mkdir) (struct inode *, struct dentry *, int);
        int (*rmdir) (struct inode *, struct dentry *);
        int (*mknod) (struct inode *, struct dentry *, int, dev_t);
        int (*rename) (struct inode *, struct dentry *, struct inode *, struct dentry *);
        int (*readlink) (struct dentry *, char *, int);
        int (*follow_link) (struct dentry *, struct nameidata *);
        int (*put_link) (struct dentry *, struct nameidata *);
        void (*truncate) (struct inode *);
        int (*permission) (struct inode *, int);
        int (*setattr) (struct dentry *, struct iattr *);
        int (*getattr) (struct vfsmount *, struct dentry *, struct kstat *);
        int (*setxattr) (struct dentry *, const char *,const void *, size_t, int);
        ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);
        ssize_t (*listxattr) (struct dentry *, char *, size_t);
        int (*removexattr) (struct dentry *, const char *);
};

      同样,操作调用时,用以下方式:i->i_op->truncate(i). 

      由于版面原因,我不得不分两次说了,下次继续后面有关虚拟文件系统的剩余部分.

数据结构课设文件系统模拟器 内附详细课程设计文档,以及可运行代码,数据结构文件系统模拟器 立即下载

相关推荐

【FPGA】高效捕捉信号边沿的硬件实现技巧

本文深入探讨了FPGA设计中高效捕捉信号边沿的硬件实现技巧。核心在于通过“打两拍”同步器处理亚稳态风险,并详细解析了上升沿、下降沿及双边沿检测的基础原理与Verilog实现。文章进一步介绍了脉冲展宽、数字滤波等高级优化方案,并结合UART起始位检测与按键消抖等实战场景,提供了可靠的代码范例与调试心得,帮助工程师构建稳定可靠的边沿检测模块。

weixin_29165465的博客 225

内核学习之-编译内核-centos7

linux提供文件系统抽象层接口即VFS,定义了文件系统的基本操作和数据结构 1:基本的数据结构如下: struct dentry *dentry;/*include/dcache.h 目录项*/ struct inode *inode;/*include/fs.h 索引节点*/ struct super_block;/*include/fs.h 超级块*/ 2:超级块的描...

hypatia2015的专栏 387

Moveit +Gazebo:搭建单臂机械臂仿真平台

Moveit +Gazebo:搭建单臂机械臂仿真平台

magic_invaid的博客 5290

struct成员的一种初始化方法

struct成员的一种初始化方法  转自: http://blog.chinaunix.net/u2/82646/showart_1358435.html 我: 在VC++6.0中不能使用这种方法, 看来VC的编译器没太按标准C99用点号。linux kernel 中的冒号是gcc的扩展。 int main() {         Str s = {.a = 1, .b

cay22的专栏 2533

23.网工入门篇--------介绍一下园区网典型组网架构及案例实践

园区网典型组网架构主要分为小型、中型、大型三种类型,以下是详细介绍及相关案例实践:

qq_31450765的博客 965

帆软FineBI考试题

Flint 补录表,有批次删除功能。在分析主题内添加数据时能够选择批量添加数据。插件安装过程中有报错提示需要下载 Fine I for Excel 运行环境进行修复,修复完毕后重新安装插件。图例显示支持自定义设置,且图例标题支持隐藏显示。finebi中的指标如果想进行维度区间/真的自定义分组,不能直接拖拽到组建区域中需要先转化为维度方可进行指标分组。excel报表不支持分页。

Davina_yu的博客 1601

理解linux虚拟文件系统VFS - 数据结构

file_system_type 1406 struct file_system_type { 1407 const char *name; 1408 int fs_flags; 1409 int (*get_sb) (struct file_system_type *, int, 1410 const

kickxxx的专栏 5027

华中科技大学头歌 交通灯系统设计 Lojisim 详细实验报告,实验图,仅供参考

本实验也是参考了许多大神的做法,也是小白一个!有完整实验代码,实验报告,答辩PPT(ppt当时做得仓促,有点拉胯)~ 需要请评论或私QQ:3096524043(有偿~~) 一、数码管驱动 由真值表生成电路 二、四位无符号比较器设计 三、8位无符号比较器设计 四、一位2路选择器设计 五、八位2路选择器设计 六、BCD双向计数器状态机设计 七、BCD双向计数器输出函数设计 ...

FindXY的博客 1万+

[文件系统]文件系统学习笔记(四)---常用数据结构

1,基本数据结构: file_system_type  每种文件系统对应一个文件系统类型结构,注册之后形成单链表,链表表头为file_systems(全局变量). Superblock  反应文件系统整体的控制信息,超级块以多种方式存在。(磁盘超级快、内存超级块、VFS超级块) Inode  反应了某个文件系统对象的一般元信息(metadata)。(磁盘inode、内存inode、VF

知了112的专栏 2961

sdc时钟约束2——多驱动时钟

sdc 多驱动时钟 master_clock

qq_40411444的博客 5529

【气候极端指数】MATLAB计算各种气候极端指数

简介各类气候极端指数,及其MATLAB计算代码

WW、forever的博客 4917

文件系统挂载详细分析(《图解Linux内核》虚拟文件系统篇笔记二)

上次分析到,系统初始化时会创建出一个vfsmount结构体mnt,并把创建出来的这个mnt以及他的小兄弟mnt->mnt_root包装成一个path结构体root并赋值给current->fs->root,然后在后续文件查找时,会先初始化一个nameidata结构体nd,将nd->root初始化为current->fs->root。那么我就来看一下mount垒墙的时候,是怎么创建vfsmount结构体的mnt的,以及文件查找的时候是怎么穿越一堵堵墙去查找的。我们再看看文件查找的时候是怎么穿过一度堵墙,在。

qq_34116044的博客 437

逆强化学习:从专家示范中学习策略

逆强化学习(Inverse Reinforcement Learning,简称IRL)是强化学习(Reinforcement Learning,简称RL)的一个子领域,它的目标是从专家的行为中学习策略。在传统的强化学习中,智能体通过与环境交互,根据奖励信号来学习一个策略。而在逆强化学习中,我们没有直接的奖励信号,而是通过观察专家的行为来推断出奖励函数,从而学习到一个与专家类似的策略。逆强化学习作为强化学习的一个重要子领域,在许多实际应用场景中都取得了显著的成功。

AI天才研究院 1451

linux内核分析笔记----虚拟文件系统(下)

接着上次的来,我今天讲虚拟文件系统剩下的一点知识.       3.目录项对象.目录项的概念上节已经说了,我就不多说.目录项中也可包括安装点.在路径/mnt/cdrom/foo中,/,mnt,cdrom都属于目录项对象。目录项由dentry结构体表示,定义在文件linux/dcache.h中,描述如下:struct dentry { atomic_t

数据挖掘及搜索专栏 1469

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

文件系统挂载详细分析(《图解Linux内核》虚拟文件系统篇笔记三)

上次说到文件查找和文件系统挂载的关系:文件系统挂载的时候创建了一个叫mnt的vfsmount结构体并穿越一堵堵墙在最后一堵墙后面垒上了自己的墙,而文件查找要在最后一堵墙上查找,而文件查找时穿越一堵堵墙的方式就是通过挂载时候建立的parent关系。方法是用来处理一层目录的,或者说是根据nd当前查找的结果查找下一级目录的),nd->path.dentry指向的是/目录(根目录),nd->last代表的是a,即在/下面查找a,那么假如没有在/下面找到a这个dentry,就会创建一个(

qq_34116044的博客 464

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

linux特定文件系统分析验证,linux内核分析笔记----虚拟文件系统

接着上次的来,我今天讲虚拟文件系统剩下的一点知识.3.目录项对象.目录项的概念上节已经说了,我就不多说.目录项中也可包括安装点.在路径/mnt/cdrom/foo中,/,mnt,cdrom都属于目录项对象。目录项由dentry结构体表示,定义在文件linux/dcache.h中,描述如下:由于目录项并非真正保存在磁盘上,所有目录项没有对应的磁盘数据结构,VFS根据字符串形式的路径名现场创建它,目录...

weixin_39854326的博客 162

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

linux内核 结构体 图,linux内核分析笔记----虚拟文件系统(上),结构体英文描述

虚拟文件系统,也不知道大家听过没有,反正我是听过了!我们知道在计算机行业,很多东西都不是一定有个官方说:朋友,我最大,你们做的东西,都要是这个样子,否则是非法的。事实上,很多东西都是靠的一种实力,通过实力来慢慢在人们心中成为既定事实。这个事实同样是没有官方的。好了,问题来了,没有官方,就没有标准,没有标准就没有统一,没有统一那就是三国时代,混战当道也!怎么办?特别是百花争鸣的文件系统,这时linu...

weixin_32059869的博客 120

《深入分析Linux内核源代码》读书、私藏笔记大放送

秉承着"不懂操作系统原理的程序员不是合格的程序员"的至理名言,鄙人又是买陈莉君老师的“Linux教学视频”,又是研读其力作《深入分析Linux内核源代码》,先将总结笔记放送给大家,希望对大家研习Linux源码提供更多的帮助,分章总结,从硬件起源开始->CPU中断->进程调度->内存管理->进程通信->文件系统->Socket网络通信,以源码结合注释的方式,对《深...

weixin_30393907的博客 211

Linux内核分析期中总结

Linux内核分析期中总结 每周读书笔记链接汇总 第三周 http://www.cnblogs.com/lxq20135309/p/5284952.html 第四周 http://www.cnblogs.com/lxq20135309/p/5296846.html 第五周 http://www.cnblogs.com/lxq20135309/p/5335502.html 第六周 http://ww...

weixin_30758821的博客 83

Linux内核——虚拟文件系统

因为要为不同类型的文件系统定义统一的接口层,所以 VFS 定义了一系列的规范,真实的文件系统必现按照 VFS 的规范来编写程序。是 VFS 中最为重要的一个结构,用于描述一个文件的meta(元)信息,其包含的是诸如文件的大小、拥有者、创建时间、磁盘位置等和文件相关的信息,所有文件都有一个对应的。Linux为了支持不同的文件系统而创造了虚拟文件系统,虚拟文件系统更像一个规范(或者说接口),真实的文件系统需要实现虚拟文件系统的规范(接口)才能接入到Linux内核中。成功读取超级块信息后,会把根目录的。

m0_74282605的博客 1193

Linux内核学习:EXT4 文件系统在 Linux 内核系统中的读写过程

目录 1 概述 2 虚拟文件系统 与 Ext4 文件系统 2.1 sys_write( ) 代码跟踪 2.2 sys_write( ) 过程分析 2.3 sys_write( ) 的核心部分 vfs_write( ) 2.4 ext4_file_write( ) 2.4.1 ext4文件系统的extent 2.4.2ext4_file_write( ) 2.5 generic...

Henry_Fordham——一个姓雪的程序员小哥哥 8188

Linux 内核中的虚拟文件系统:从抽象到实现

Linux 内核中的虚拟文件系统(VFS)是一个核心抽象层,它为不同的文件系统提供了统一的接口。VFS 通过超级块、索引节点、目录项、文件等组件,实现了文件系统的统一管理和操作。工作也要流程化,虚拟文件系统就像是系统中的抽象层,它确保了不同文件系统可以以统一的方式进行操作。在实际应用中,我们需要选择合适的文件系统,优化挂载参数,合理使用文件操作,监控文件系统状态,以及在开发自定义文件系统时遵循 VFS 的设计规范。

jiang_style的博客 1013

Linux内核启动之根文件系统挂载

一、基本概念介绍 1.1 rootfs 什么是根文件系统?理论上说一个嵌入式设备如果内核能运行起来,且不需要用户进程的话(估计这种情况很少),是不需要文件系统的。文件系统简单的说就是一种目录结构,由于linux操作系统的设备在系统中是以文件的形式存在,将这些文件分类管理以及提供和内核交互的接口,就形成了一定的目录结构也就是文件系统。文件系统是为用户反映系统的一种形式,为用户提供一个检测控制系统的...

Graceful_scenery的博客 3697

深入理解Linux内核中的虚拟文件系统(VFS)

在这篇教程中,我们深入探讨了Linux内核中的虚拟文件系统(VFS)。VFS作为抽象层,为不同类型的文件系统提供统一接口,使上层应用可以使用相同API访问文件。我们介绍了VFS的基本概念、关键数据结构(如`super_block`、`inode`、`dentry`和`file`)、VFS的回调函数与操作接口(如`file_operations`、`inode_operations`和`super_operations`),以及一个文件系统在VFS中的注册与使用流程。希望这些内容能帮助你更好地理解和应用VFS

Linux内核研究者 3675
上一篇: linux内核分析笔记----内存管理
下一篇: linux内核分析笔记----虚拟文件系统(下)
wustdatamining
博客等级 码龄15年 12粉丝 81原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值