zoukankan      html  css  js  c++  java
  • Linux的命名空间

    1. 为什么提供命名空间

    命名空间是一种轻量级的虚拟化手段。

    传统的虚拟化软件,是虚拟化多个不同的操作系统,对共享资源的限制很大。

    通过提供命名空间,可以让进程与进程之间,用户与用户之间彼此看不到对方。

    命名空间,相当于容器。

    命名空间,本质上建立了系统的不同视图。

    chroot是一种简单的命名空间,仅限于将进程限制在文件系统的某一部分。

    2. 创建命名空间的方式

    1). fork/clone创建新进程时,可以设置选项,使新进程与父进程共享命名空间,还是新进程创建一个独立的命名空间。

    2). unshare系统调用,可以将进程的某些部分从父进程分离,其中也包括命名空间。

    3. 实现:

       1: struct task_struct {
       2: ......
       3: /* namespaces */
       4:     struct nsproxy *nsproxy;
       5: ......
       6: }
       1: /*
       2:  * A structure to contain pointers to all per-process
       3:  * namespaces - fs (mount), uts, network, sysvipc, etc.
       4:  *
       5:  * 'count' is the number of tasks holding a reference.
       6:  * The count for each namespace, then, will be the number
       7:  * of nsproxies pointing to it, not the number of tasks.
       8:  *
       9:  * The nsproxy is shared by tasks which share all namespaces.
      10:  * As soon as a single namespace is cloned or unshared, the
      11:  * nsproxy is copied.
      12:  */
      13: struct nsproxy {
      14:     atomic_t count;
      15:     struct uts_namespace *uts_ns;
      16:     struct ipc_namespace *ipc_ns;
      17:     struct mnt_namespace *mnt_ns;
      18:     struct pid_namespace *pid_ns;
      19:     struct net          *net_ns;
      20: };

    每个进程都有一个指针指向nsproxy结构体,多个进程可能共享一个nsproxy结构体,比如父子进程。

    一个nsproxy代表一整套的命名空间实现,其中包含了几个子系统的命名空间:

    UTS(UNIX Timesharing System),包含了运行内核的名称,版本,底层体系结构的信息;

    IPC,包含了所有与进程间通信有关的信息;

    MNT,包含了文件系统的视图;

    PID,就是进程ID;

    USER,就是用户;

    NET,与网络相关。

    各个子系统对于命名空间的实现与应用都各不相同。

  • 相关阅读:
    线程池参数设置技巧
    线程池的七个参数
    bug篇——Windows启动redis报错Creating Server TCP listening socket 127.0.0.1:6379: bind: No error
    总结篇——git本地仓库上传更新到github
    实例篇——springboot自定义拦截器
    总结篇——从零搭建maven多模块springboot+mybatis项目
    bug篇——MySQL的时区问题
    工具类篇——时间处理Calendar类
    安装篇——nginx安装ssl模块转发https请求
    总结篇——nginx代理服务器
  • 原文地址:https://www.cnblogs.com/long123king/p/3535462.html
Copyright © 2011-2022 走看看