關於“atexit函數”,“Linux中的t權限”,“Linux中的FILE結構體”的介紹
一、關於atexit函數
atexit函數包含在頭文件:#include<stdlib.h>中 ,功 能: 註冊終止函數(即main執行結束後調用的函數) ,用 法: int atexit(void (*func)(void)),atexit()函數來註冊程序正常終止時要被調用的函數,在一個程序中最多可以用atexit()註冊32個處理函數,這些處理函數的調用順序與其註冊的順序相反,也即最先註冊的最後調用,最後註冊的最先調用。
如下所示:
由上述例子可知,最後註冊的fun3()函數先結束調用,而最先註冊的fun1()函數最後結束調用,所以印證了atexit註冊的函數順序與調用順序相反。由於atexit是在進程退出時調用函數的,所以main函數裏的printf先輸出了 fun has finished。
二、Linux中的t權限
t:Sticky Bit,簡稱爲SBIT權限,只針對目錄有效。
t :設置粘着位,一個文件可讀寫的用戶並一定想讓他有刪除此文件的權限,如果文件設置了t權限則只用屬主和root有刪除文件的權限,通過chmod +t filename 來設置t權限。
當用戶對此目錄具有 w, x 權限,亦具有寫入的權限時當用戶在該目錄下創建文件或目錄時,僅有自己與 root用戶纔有權限刪除該文件或目錄。
如果希望用戶能夠添加文件但同時不能刪除文件, 則可以對文件使用sticky bit位. 設置該位後, 就算用戶對目錄具有寫權限, 也不能刪除該文件。
struct file結構體其原型是:
struct file {
/*
* fu_list becomes invalid after file_free is called and queued via
* fu_rcuhead for RCU freeing
*/
union {
struct list_head fu_list;
struct rcu_head fu_rcuhead;
} f_u;
struct path f_path;
#define f_dentry f_path.dentry
#define f_vfsmnt f_path.mnt
const struct file_operations *f_op;
atomic_t f_count;
unsigned int f_flags;
mode_t f_mode;
loff_t f_pos;
struct fown_struct f_owner;
unsigned int f_uid, f_gid;
struct file_ra_state f_ra;
unsigned long f_version;
#ifdef CONFIG_SECURITY
void *f_security;
#endif
/* needed for tty driver, and maybe others */
void *private_data;
#ifdef CONFIG_EPOLL
/* Used by fs/eventpoll.c to link all the hooks to this file */
struct list_head f_ep_links;
spinlock_t f_ep_lock;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space *f_mapping;
};
文件結構體代表一個打開的文件,系統中的每個打開的文件在內核空間都有一個關聯的struct file。