[Linux]进程(十二)--task_struct结构体

本文详细解析了 Linux 内核中的 task_struct 结构体,包括其成员变量及其作用,如进程状态、调度信息、文件系统信息等。还介绍了如何查看 thread_info 结构体的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

struct task_struct {
	volatile long state; /* -1 不可运行, 0 可运行, >0 表示停止 */
	void *stack;
	atomic_t usage;
	unsigned long flags; /* 每进程标志 */
	unsigned long ptrace;
	int lock_depth; /* 大内核锁深度 */
	int prio, static_prio, normal_prio;
	struct list_head run_list;
	const struct sched_class *sched_class;
	struct sched_entity se;
	unsigned short ioprio;
	unsigned long policy;
	cpumask_t cpus_allowed;
	unsigned int time_slice;
#if defined(CONFIG_SCHEDSTATS) || defined(CONFIG_TASK_DELAY_ACCT)
	struct sched_info sched_info;
#endif
	struct list_head tasks;
	/*
	* ptrace_list/ptrace_children forms the list of my children
	* that were stolen by a ptracer.
	*/
	struct list_head ptrace_children;
	struct list_head ptrace_list;
	struct mm_struct *mm, *active_mm;
/* 进程状态 */
	struct linux_binfmt *binfmt;
	long exit_state;
	int exit_code, exit_signal;
	int pdeath_signal; /* 在父进程终止的时候发送的信号 */
	unsigned int personality;
	unsigned did_exec:1;
	pid_t pid;
	pid_t tgid;
	struct task_struct *real_parent; /* 真正的父进程(被调试的情况下) */
	struct task_struct *parent; /* 父进程 ,parent和real_parent的区别:real_parent是亲爹,调fork的那个,parent呢是干爹,大部分情况下亲爹干爹是一个人,ps看到的是干爹,什么时候亲爹干爹不一样的,比如有一种情况,比如亲爹死了,但是呢又得有一个父进程,比如1号进程就会被当成父进程。但进程不是1号fork出来的。*/
	struct list_head children; /* 子进程链表 */
	struct list_head sibling; /* 连接到父进程的子进程的链表 */
	struct task_struct *group_leader; /* 线程组组长 */
	/* PID与PID散列表的联系*/
	struct pid_link pids[PIDTYPE_MAX];
	struct list_head thread_group;
	struct completion *vfork_done; /* 用于vfork() */
	int __user *set_child_tid; /* CLONE_CHILD_SETTID */
	int __user *clear_child_tid; /* CLONE_CHILD_CLEARTID */
	unsigned long rt_priority;
	cputime_t utime, stime, utimescaled, stimescaled;;
	unsigned long nvcsw, nivcsw; /* 上下文切换计数 */
	struct timespec start_time; /* 单调时间 */
	struct timespec real_start_time; /* 启动以来的时间 */
	unsigned long min_flt, maj_flt;
	cputime_t it_prof_expires, it_virt_expires;
	unsigned long long it_sched_expires;
	struct list_head cpu_timers[3];
/* 进程身份凭据 */
	uid_t uid,euid,suid,fsuid;
	gid_t gid,egid,sgid,fsgid;
	struct group_info *group_info;
	kernel_cap_t cap_effective, cap_inheritable, cap_permitted;
	unsigned keep_capabilities:1;
	struct user_struct *user;
	char comm[TASK_COMM_LEN]; /* 去除路径后的可执行文件名称*/
/* 文件系统信息 */
	int link_count, total_link_count;
/* ipc stuff */
	struct sysv_sem sysvsem;
/* 当前进程特定的cpu信息 */
	struct thread_struct thread;
/* filesystem information */
	struct fs_struct *fs;
/* open file information */
	struct files_struct *files;
/* namespace */
	struct nsproxy *nsproxy;
/* signal handlers */
	struct signal_struct *signal;
	struct sighand_struct *sighand;
	sigset_t blocked, real_blocked;
	sigset_t saved_sigmask; /* To be restored with TIF_RESTORE_SIGMASK */
	struct sigpending pending;
	unsigned long sas_ss_sp;
	size_t sas_ss_size;
	int (*notifier)(void *priv);
	void *notifier_data;
	sigset_t *notifier_mask;
#ifdef CONFIG_SECURITY
v	oid *security;
#endif
/* 线程组跟踪 */
	u32 parent_exec_id;
	u32 self_exec_id;
/* 日志文件系统信息 */
	void *journal_info;
/* 虚拟内存状态 */
	struct reclaim_state *reclaim_state;
	struct backing_dev_info *backing_dev_info;
	struct io_context *io_context;
	unsigned long ptrace_message;
	siginfo_t *last_siginfo; /* For ptrace use. */
...
};
atomic_t usage; 有几个进程正在使用此结构

task_struct结构体里的 rt_priority 成员代表的是实时进程优先级, 普通进程此值应该为0,实时进程才有此值。
PS –p –t 命令可以看出各个进程或者线程的实时优先级。

Irq都是实时优先级,一般实时优先级也比较高.


如何查看thread_info结构体的信息
#define task_thread_info(task)    ((struct thread_info *)(task)->stack)
V.V (struct thread_info*)(0xE9A1C000 & ~(8192-1))


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值