linux檔案系統與nginx模組系統異曲同工之妙

來源:互聯網
上載者:User

linux檔案系統,通過file_operation結構體,操作inode。file_operation結構體的所有域都是指標結構,每種檔案系統都要“實現”這些方法,供系統調用。對於系統來講,不知道也不用理會現在是需要操作哪種檔案系統,它只需調用介面方法,file_operation的函數指標自然會通過初始化過程,調用對應的檔案系統的相應方法。

這樣看起來,file_operation似乎就是多個介面方法的集合。真正的檔案系統只需要實現響應的方法,並且與函數指標對應起來,就可以在初始化之後使用了。

下面是file_operation的結構體代碼,其中如果不需要或者不能夠使用的函數指標,可以設成NULL。

struct file_operations {struct module *owner;loff_t (*llseek) (struct file *, loff_t, int);ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);int (*readdir) (struct file *, void *, filldir_t);unsigned int (*poll) (struct file *, struct poll_table_struct *);int (*ioctl) (struct inode *, struct file *, unsigned int, unsigned long);long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);long (*compat_ioctl) (struct file *, unsigned int, unsigned long);int (*mmap) (struct file *, struct vm_area_struct *);int (*open) (struct inode *, struct file *);int (*flush) (struct file *, fl_owner_t id);int (*release) (struct inode *, struct file *);int (*fsync) (struct file *, struct dentry *, int datasync);int (*aio_fsync) (struct kiocb *, int datasync);int (*fasync) (int, struct file *, int);int (*lock) (struct file *, int, struct file_lock *);ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);int (*check_flags)(int);int (*flock) (struct file *, int, struct file_lock *);ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);int (*setlease)(struct file *, long, struct file_lock **);};

與之類似的還有inode_operation等結構體。

之後看看nginx的模組系統,為了降低模組和nginx之間的耦合性,並且提高系統的可塑性。nginx也使用了這種物件導向的“介面類”的方法,來實現模組系統。

typedef struct {    ngx_str_t              *name;    void                 *(*create_conf)(ngx_cycle_t *cycle);    char                 *(*init_conf)(ngx_cycle_t *cycle, void *conf);    ngx_event_actions_t     actions;} ngx_event_module_t;typedef struct {    ngx_int_t  (*add)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);    ngx_int_t  (*del)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);    ngx_int_t  (*enable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);    ngx_int_t  (*disable)(ngx_event_t *ev, ngx_int_t event, ngx_uint_t flags);    ngx_int_t  (*add_conn)(ngx_connection_t *c);    ngx_int_t  (*del_conn)(ngx_connection_t *c, ngx_uint_t flags);    ngx_int_t  (*process_changes)(ngx_cycle_t *cycle, ngx_uint_t nowait);    ngx_int_t  (*process_events)(ngx_cycle_t *cycle, ngx_msec_t timer,                   ngx_uint_t flags);    ngx_int_t  (*init)(ngx_cycle_t *cycle, ngx_msec_t timer);    void       (*done)(ngx_cycle_t *cycle);} ngx_event_actions_t;

看,他們是多麼的相似。只要實現了event_action結構體內包含的介面方法,就可以實現自己的模組功能。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.