命名空间是用来对资源的全局管理的,例如进程的PID。
1、命名空间的实现
内核使用nsproxy数据结构来将所有命名空间提供的对象关联起来,
struct nsproxy {
atomic_t count;
struct uts_namespace *uts_ns; ///运行内核的名称 版本 体系结构
struct ipc_namespace *ipc_ns; //保存进程间通信的信息
struct mnt_namespace *mnt_ns; //已经装载的文件系统映射
struct pid_namespace *pid_ns_for_children;
struct net net_ns; //网络命名空间
};
在创建新进程的时候,使用fork可以建立新的命名空间,有如下几种标识:
#define CLONE_NEWUTS 0x04000000 / New utsname group? /
#define CLONE_NEWIPC 0x08000000 / New ipcs /
#define CLONE_NEWUSER 0x10000000 / New user namespace /
#define CLONE_NEWPID 0x20000000 / New pid namespace /
#define CLONE_NEWNET 0x40000000 / New network namespace /
#define CLONE_IO 0x80000000 / Clone io context */
系统初始化的时候,产生全局的命名空间,定义如下:
struct nsproxy init_nsproxy = {
.count = ATOMIC_INIT(1),
.uts_ns = &init_uts_ns,
#if defined(CONFIG_POSIX_MQUEUE) || defined(CONFIG_SYSVIPC)
.ipc_ns = &init_ipc_ns,
#endif
.mnt_ns = NULL,
.pid_ns_for_children = &init_pid_ns,
#ifdef CONFIG_NET
.net_ns = &init_net,
#endif
};
Linux命名空间
最新推荐文章于 2023-05-30 16:51:42 发布