一步一步学linux操作系统: 10 进程数据结构_task_struct字段_运行统计、亲缘关系、进程权限、内存管理

task_struct 相关源码位置:
linux-4.13.16\include\linux\sched.h

运行统计信息

包含用户/内核态运行时间; 上/下文切换次数; 启动时间等;

u64        utime;//用户态消耗的CPU时间
u64        stime;//内核态消耗的CPU时间
unsigned long      nvcsw;//自愿(voluntary)上下文切换计数
unsigned long      nivcsw;//非自愿(involuntary)上下文切换计数
u64        start_time;//进程启动时间,不包含睡眠时间
u64        real_start_time;//进程启动时间,包含睡眠时间

在这里插入图片描述

进程亲缘关系

task_struct 里面关于进程亲缘关系的字段

struct task_struct __rcu *real_parent; /* real parent process */
struct task_struct __rcu *parent; /* recipient of SIGCHLD, wait4() reports */
struct list_head children;      /* list of my children */
struct list_head sibling;       /* linkage in my parent's children list */

在这里插入图片描述

  • parent 指向其父进程。当它终止时,必须向它的父进程发送信号。
  • children 表示链表的头部。链表中的所有元素都是它的子进程。
  • sibling 用于把当前进程插入到兄弟链表中。

real_parent 和 parent 通常一样; 但在 bash 中用 GDB 调试程序时, GDB 是 parent, bash 是 real_parent
图片来自极客时间趣谈linux操作系统

进程权限

task_struct 里面关于进程权限的字段

/* Objective and real subjective task credentials (COW): */
const struct cred __rcu         *real_cred;
/* Effective (overridable) subjective task credentials (COW): */
const struct cred __rcu         *cred;

在这里插入图片描述
real_cred 指针(谁能操作我这个进程); cred 指针(我这个进程能操作谁)

cred结构体

位置 /include/linux/cred.h

struct cred {
	atomic_t	usage;
#ifdef CONFIG_DEBUG_CREDENTIALS
	atomic_t	subscribers;	/* number of processes subscribed */
	void		*put_addr;
	unsigned	magic;
#define CRED_MAGIC	0x43736564
#define CRED_MAGIC_DEAD	0x44656144
#endif
	kuid_t		uid;		/* real UID of the task */
	kgid_t		gid;		/* real GID of the task */
	kuid_t		suid;		/* saved UID of the task */
	kgid_t		sgid;		/* saved GID of the task */
	kuid_t		euid;		/* effective UID of the task */
	kgid_t		egid;		/* effective GID of the task */
	kuid_t		fsuid;		/* UID for VFS ops */
	kgid_t		fsgid;		/* GID for VFS ops */
	unsigned	securebits;	/* SUID-less security management */
	kernel_cap_t	cap_inheritable; /* caps our children can inherit */
	kernel_cap_t	cap_permitted;	/* caps we're permitted */
	kernel_cap_t	cap_effective;	/* caps we can actually use */
	kernel_cap_t	cap_bset;	/* capability bounding set */
	kernel_cap_t	cap_ambient;	/* Ambient capability set */
#ifdef CONFIG_KEYS
	unsigned char	jit_keyring;	/* default keyring to attach requested
					 * keys to */
	struct key __rcu *session_keyring; /* keyring inherited over fork */
	struct key	*process_keyring; /* keyring private to this process */
	struct key	*thread_keyring; /* keyring private to this thread */
	struct key	*request_key_auth; /* assumed request_key authority */
#endif
#ifdef CONFIG_SECURITY
	void		*security;	/* subjective LSM security */
#endif
	struct user_struct *user;	/* real user ID subscription */
	struct user_namespace *user_ns; /* user_ns the caps and keyrings are relative to. */
	struct group_info *group_info;	/* supplementary groups for euid/fsgid */
	struct rcu_head	rcu;		/* RCU deletion hook */
} __randomize_layout;

在这里插入图片描述

__rcu

RCU(Read-Copy Update),是 Linux 中比较重要的一种同步机制。顾名思义就是“读,拷贝更新”,再直白点是“随意读,但更新数据的时候,需要先复制一份副本,在副本上完成修改,再一次性地替换旧数据”。这是 Linux 内核实现的一种针对“读多写少”的共享数据的同步机制。

四个id组
  • uid 和 gid
    注释是 real user/group id
    一般情况下,谁启动的进程,就是谁的 ID
    权限审核的时候,往往不比较这两个
  • euid 和 egid
    注释是 effective user/group id
    按照哪个用户审核权限
    操作消息队列, 共享内存、信号量等对象
  • fsuid 和 fsgid
    注释是 VFS filesystem user/group id
    对文件操作会审核的权限。
  • suid 和 sgid
    saved uid 和 save gid
    由于suid和sgid保存了原始的用户id,可以随时使用 setuid,通过设置 uid 或者 suid 来改变权限。

一般说来,fsuid、euid,和 uid 是一样的,fsgid、egid,和 gid 也是一样的

特殊的情况

图片来自极客时间趣谈linux操作系统

  1. 用户 A 想玩B 安装的游戏
    游戏这个程序文件的权限为 rwxr–r–。A 是没有权限运行这个程序的,要用户 B 给用户 A 权限才行
  2. 开了权限后,A可以运行游戏了,但不能保存
    当游戏运行起来之后,游戏进程的 uid、euid、fsuid 都是用户 A
    保存这个文件的权限 rw-r–r--,只给用户 B 开了写入权限,,所以A写不进去
  3. 通过 chmod u+s program 命令,设置 set-user-ID 的标识位,把游戏的权限变成 rwsr-xr-x
    用户 A 再启动这个游戏的时候,进程 uid 还是用户 A,由于有set-user-id 标识,euid 和 fsuid 就不是用户 A而是文件的所有者的 ID,也就是euid 和 fsuid 都改成用户 B 了

capabilities 机制

除了以用户和用户组控制权限,Linux 还有另一个机制就是 capabilities,用位图表示权限
capability 机制, 以细粒度赋予普通用户部分高权限

位置: \include\uapi\linux\capability.h

#define CAP_CHOWN            0
#define CAP_KILL             5
#define CAP_NET_BIND_SERVICE 10
#define CAP_NET_RAW          13
#define CAP_SYS_MODULE       16
#define CAP_SYS_RAWIO        17
#define CAP_SYS_BOOT         22
#define CAP_SYS_TIME         25
#define CAP_AUDIT_READ          37
#define CAP_LAST_CAP         CAP_AUDIT_READ

在这里插入图片描述

  • cap_permitted 表示进程能够使用的权限。但是真正起作用的是 cap_effective。
  • cap_effective 实际起作用的权限, cap_permitted 范围可大于 cap_effective
  • cap_inheritable 表示权限可被继承, 在 exec 执行时继承调用者的inheritable权限集合, 并加入到 permitted 集合,但在非 root 用户下执行 exec 时,通常不会保留 inheritable 集合
  • cap_bset 系统中所有进程允许保留的权限,如果这个集合中不存在某个权限,那么系统中的所有进程都没有这个权限
  • cap_ambient 当执行 exec 的时候,cap_ambient 会被添加到 cap_permitted 中,同时设置到 cap_effective 中,解决非 root 用户进程使用 exec 执行一个程序的时候,如何保留权限的问题

内存管理

数据结构 mm_struct

struct mm_struct                *mm;
struct mm_struct                *active_mm;

在这里插入图片描述

文件与文件系统

每个进程有一个文件系统的数据结构,还有一个打开文件的数据结构

/* Filesystem information: */
struct fs_struct                *fs;
/* Open file information: */
struct files_struct             *files;

在这里插入图片描述

task_struct 数据结构

图片来自极客时间趣谈linux操作系统

参考资料:

趣谈Linux操作系统(极客时间)链接:
http://gk.link/a/10iXZ
欢迎大家来一起交流学习

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值