• Linux 操作系统内核之进程


    Linux 操作系统内核



    进程

    进程有很多解释的版本,比如说是程序的执行过程,其实每个人的理解不一样,简单的来说,进程就是cpu分配资源的基本单元。

    ps:线程是计算机调度的基本单元(面试经常问的:进程和线程区别)


    一、进程描述符

    进程描述符主要是说明进程的相关信息,可以看linux 内核源码2.6.3以上的版本,
    struct task_struct根据描述(include/linux/sched.h)

    struct task_struct {
    	volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
    	struct thread_info *thread_info;
    	atomic_t usage;
    	unsigned long flags;	/* per process flags, defined below */
    	unsigned long ptrace;
    
    	int lock_depth;		/* Lock depth */
    
    	int prio, static_prio;
    	struct list_head run_list;
    	prio_array_t *array;
    
    	unsigned long sleep_avg;
    	long interactive_credit;
    	unsigned long long timestamp;
    	int activated;
    
    	unsigned long policy;
    	cpumask_t cpus_allowed;
    	unsigned int time_slice, first_time_slice;
    
    	struct list_head tasks;
    	struct list_head ptrace_children;
    	struct list_head ptrace_list;
    
    	struct mm_struct *mm, *active_mm;
    
    /* task state */
    	struct linux_binfmt *binfmt;
    	int exit_code, exit_signal;
    	int pdeath_signal;  /*  The signal sent when the parent dies  */
    	/* ??? */
    	unsigned long personality;
    	int did_exec:1;
    	pid_t pid;
    	pid_t __pgrp;		/* Accessed via process_group() */
    	pid_t tty_old_pgrp;
    	pid_t session;
    	pid_t tgid;
    	/* boolean value for session group leader */
    	int leader;
    	/* 
    	 * pointers to (original) parent process, youngest child, younger sibling,
    	 * older sibling, respectively.  (p->father can be replaced with 
    	 * p->parent->pid)
    	 */
    	struct task_struct *real_parent; /* real parent process (when being debugged) */
    	struct task_struct *parent;	/* parent process */
    	struct list_head children;	/* list of my children */
    	struct list_head sibling;	/* linkage in my parent's children list */
    	struct task_struct *group_leader;	/* threadgroup leader */
    
    	/* PID/PID hash table linkage. */
    	struct pid_link pids[PIDTYPE_MAX];
    
    	wait_queue_head_t wait_chldexit;	/* for wait4() */
    	struct completion *vfork_done;		/* for vfork() */
    	int __user *set_child_tid;		/* CLONE_CHILD_SETTID */
    	int __user *clear_child_tid;		/* CLONE_CHILD_CLEARTID */
    
    	unsigned long rt_priority;
    	unsigned long it_real_value, it_prof_value, it_virt_value;
    	unsigned long it_real_incr, it_prof_incr, it_virt_incr;
    	struct timer_list real_timer;
    	struct list_head posix_timers; /* POSIX.1b Interval Timers */
    	unsigned long utime, stime, cutime, cstime;
    	unsigned long nvcsw, nivcsw, cnvcsw, cnivcsw; /* context switch counts */
    	u64 start_time;
    /* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
    	unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
    /* process credentials */
    	uid_t uid,euid,suid,fsuid;
    	gid_t gid,egid,sgid,fsgid;
    	int ngroups;
    	gid_t	groups[NGROUPS];
    	kernel_cap_t   cap_effective, cap_inheritable, cap_permitted;
    	int keep_capabilities:1;
    	struct user_struct *user;
    /* limits */
    	struct rlimit rlim[RLIM_NLIMITS];
    	unsigned short used_math;
    	char comm[16];
    /* file system info */
    	int link_count, total_link_count;
    	struct tty_struct *tty; /* NULL if no tty */
    /* ipc stuff */
    	struct sysv_sem sysvsem;
    /* CPU-specific state of this task */
    	struct thread_struct thread;
    /* filesystem information */
    	struct fs_struct *fs;
    /* open file information */
    	struct files_struct *files;
    /* namespace */
    	struct namespace *namespace;
    /* signal handlers */
    	struct signal_struct *signal;
    	struct sighand_struct *sighand;
    
    	sigset_t blocked, real_blocked;
    	struct sigpending pending;
    
    	unsigned long sas_ss_sp;
    	size_t sas_ss_size;
    	int (*notifier)(void *priv);
    	void *notifier_data;
    	sigset_t *notifier_mask;
    	
    	void *security;
    
    /* Thread group tracking */
       	u32 parent_exec_id;
       	u32 self_exec_id;
    /* Protection of (de-)allocation: mm, files, fs, tty */
    	spinlock_t alloc_lock;
    /* Protection of proc_dentry: nesting proc_lock, dcache_lock, write_lock_irq(&tasklist_lock); */
    	spinlock_t proc_lock;
    /* context-switch lock */
    	spinlock_t switch_lock;
    
    /* journalling filesystem info */
    	void *journal_info;
    
    /* VM state */
    	struct reclaim_state *reclaim_state;
    
    	struct dentry *proc_dentry;
    	struct backing_dev_info *backing_dev_info;
    
    	struct io_context *io_context;
    
    	unsigned long ptrace_message;
    	siginfo_t *last_siginfo; /* For ptrace use.  */
    };
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136

    内核中是使用双向链表将 task_struct 串起来,
    双向链表
    进程描述符任务队列

    但是对于每个链表都要初始化,插入删除的话,非常浪费时间,linux内核封装了list_head, 使用list_head构造双向链表

    struct list_head {
    	struct list_head *next, *prev;
    };
    
    • 1
    • 2
    • 3

    同时添加了处理函数:

    list_add(n, p): 把n指向的元素指向p指向的元素之后
    list_add_tail(n, p):把n指向的元素指向p指向的元素之前
    list_del(p):删除p所指向的元素
    list_empty(p):检查p链表是否为空
    …

    然后通过esp寄存器快速获取到当前运行进程的task_struct结构
    在这里插入图片描述

    static inline struct task_struct * get_current(void) { 
              struct task_struct *current; __asm__(
            "andl %%esp,%0; ":"=r" (current) : "0" (~8191UL)
    ); 
    return current;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    二、进程状态

    1.进程描述符中有字段state表示了进程的状态

    volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
    
    • 1

    2.有7种状态:

    TASK_RUNNING : 可执行状态
    TASK_INTETRRUPTIBLE : 可中断的等待状态
    TASK_UNINTERRUPTIBLE : 不可中断的等待状态
    TASK_STOPPED : 暂停状态
    TASK_TRACED : 跟踪状态
    EXIT_ZOMBLE : 僵死状态
    EXIT_DEAD : 僵死撤销状态
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    总结

    本文大概讲了进程描述符和进程状态,后面会说进程的关系和转换。
    参考书籍:
    《linux内核设计和实现》
    《深入理解linux内核》

  • 相关阅读:
    java计算机毕业设计ssm+jsp计算机视频学习网站
    merge语法的使用
    屏幕时代的数字化信息管理:从传统到纯软件的转变
    springcloud05——Zookeeper实现支付微服务
    交换部分 VLAN
    别再夹灰了!这份阿里巴巴 Java 架构六大专题面试宝典值得你刷一刷
    perl下mysql同步监控
    Debiased Contrastive Learning of Unsupervised Sentence Representations 论文阅读
    云原生--kubectl命令汇总
    Python学习第2天-安装pycharm
  • 原文地址:https://blog.csdn.net/qq_34770510/article/details/126005157