一、atexit函数
atexit()注册的函数类型应为不接受任何参数的void函数,exit调用这些注册函数的顺序与它们 登记时候的顺序相反。同一个函数如若登记多次,则也会被调用多次。
#include <stdio.h>
#include < stdlib.h> void exit_fn1(void) { printf("Exit function #1 called\n"); } void exit_fn2(void) { printf("Exit function #2 called\n"); } int main(void) { /* post exit function #1 */ atexit(exit_fn1); /* post exit function #2 */ atexit(exit_fn2); return 0; } 输出: Exit function #2 called Exit function #1 called
二、FILE结构体成员
#ifndef _FILE_DEFINEDstruct _iobuf {
char *_ptr; //文件输入的下一个位置
int _cnt; //当前缓冲区的相对位置
char *_base; //指基础位置(即是文件的其始位置)
int _flag; //文件标志
int _file; //文件的有效性验证
int _charbuf; //检查缓冲区状况,如果无缓冲区则不读取
int _bufsiz; //???这个什么意思
char *_tmpfname; //临时文件名
}; typedef struct _iobuf FILE; #define _FILE_DEFINED #endif
三、Linux下的 Struct_file结构体
struct file {
union {
struct list_head fu_list; 文件对象链表指针linux/include/linux/list.h
struct rcu_head fu_rcuhead; RCU(Read-Copy Update)是Linux 2.6内核中新的锁机制
} f_u;
struct path f_path; 包含dentry和mnt两个成员,用于确定文件路径
#define f_dentry f_path.dentry f_path的成员之一,当前文件的dentry结构
#define f_vfsmnt f_path.mnt 表示当前文件所在文件系统的挂载根目录
const struct file_operations *f_op; 与该文件相关联的操作函数
atomic_t f_count; 文件的引用计数(有多少进程打开该文件)
unsigned int f_flags; 对应于open时指定的flag
mode_t f_mode; 读写模式:open的mod_t mode参数
off_t f_pos; 该文件在当前进程中的文件偏移量
struct fown_struct f_owner; 该结构的作用是通过信号进行I/O时间通知的数据。
unsigned int f_uid, f_gid; 文件所有者id,所有者组id
struct file_ra_state f_ra; 在linux/include/linux/fs.h中定义,文件预读相关
unsigned long f_version;
#ifdef CONFIG_SECURITY
void *f_security;
#endif
void *private_data;
#ifdef CONFIG_EPOLL
struct list_head f_ep_links;
spinlock_t f_ep_lock;
#endif
struct address_space *f_mapping;
};
四、t权限
在Linux中,如果对一个目录具有写权限,那么我们就可以在这个目录下创建删除文件。
现在我们要创建这样一个目录,我们可以在这个目录里创建文件,但是其他用户不能删除这个文件。
在这里就要用到一个特殊的权限:粘着位
在linux中有三种特殊权限,分别为:
SetUID = 4:在所有者设置的权限
SetGID = 2:在所属组设置的权限
粘着位 = 1:在其他人的位置设置的 (使用 t 来表示)
(SetUID 、SetGID 参见地址:http://blog.csdn.net/lyhdream/article/details/9063207)
对于授予了粘着位权限的目录来说:每个用户都能在这个目录里面创建文件(前提是这个目录的权限为777),但是只能删除自己创建的文件。
粘着为授予的方法:
一、chomd o+t
二、chomd 1777