Linux 0.12

1. PCB

Linux的PCB叫task_struct,在linux-0.12/include/linux/sched.h中定义,

struct task_struct {
/* these are hardcoded - don't touch */
    long state;    /* -1 unrunnable, 0 runnable, >0 stopped */
    long counter;
    long priority;
    long signal;
    struct sigaction sigaction[32];
    long blocked;    /* bitmap of masked signals */
    /* various fields */
    int exit_code;
    unsigned long start_code,end_code,end_data,brk,start_stack;
    long pid,pgrp,session,leader;
    int    groups[NGROUPS];
    /* 
     * pointers to parent process, youngest child, younger sibling,
     * older sibling, respectively.  (p->father can be replaced with 
     * p->p_pptr->pid)
     */
    struct task_struct    *p_pptr, *p_cptr, *p_ysptr, *p_osptr;
    unsigned short uid,euid,suid;
    unsigned short gid,egid,sgid;
    unsigned long timeout,alarm;
    long utime,stime,cutime,cstime,start_time;
    struct rlimit rlim[RLIM_NLIMITS]; 
    unsigned int flags;    /* per process flags, defined below */
    unsigned short used_math;
    /* file system info */
    int tty;        /* -1 if no tty, so it must be signed */
    unsigned short umask;
    struct m_inode * pwd;
    struct m_inode * root;
    struct m_inode * executable;
    struct m_inode * library;
    unsigned long close_on_exec;
    struct file * filp[NR_OPEN];
    /* ldt for this task 0 - zero 1 - cs 2 - ds&ss */
    struct desc_struct ldt[3];
    /* tss for this task */
    struct tss_struct tss;
};

2. 进程状态

long state;,进程状态,在linux-0.12/include/linux/sched.h中定义,

#define TASK_RUNNING            0
#define TASK_INTERRUPTIBLE        1
#define TASK_UNINTERRUPTIBLE    2
#define TASK_ZOMBIE                3
#define TASK_STOPPED            4

各个状态含义如下:

2.1 进程状态转换

Linux 0.12进程状态转换图如下(图片来源于赵炯《Linux内核完全剖析——基于0.12内核》):

image-20230413224130398

2.2 进程控制

根据上图,状态在何种情况会发生,状态转移的话,完成了哪些操作?

3. 进程指针间关系

Linux 0.12进程控制块有4个指针成员变量来表示进程间的关系,

struct task_struct    *p_pptr, *p_cptr, *p_ysptr, *p_osptr;

进程指针间的关系图如下(图片来源于赵炯《Linux内核完全剖析——基于0.12内核》):

image-20230413224314416

3.1 进程组织

Linux 0.12进程组织采用数组存储task_struct指针,指针指向PCB,各进程间PCB又互相关联(逻辑上形成一颗进程树),相关代码如下:

struct task_struct * task[NR_TASKS] = {&(init_task.task), };

union task_union {
    struct task_struct task;
    char stack[PAGE_SIZE];
};

static union task_union init_task = {INIT_TASK,};
  • NR_TASKSsched.h中定义,值为64。
  • INIT_TASK系统中第一个进程,定义如下:

#define INIT_TASK \
/* state etc */    { 0,15,15, \
/* signals */    0,{{},},0, \
/* ec,brk... */    0,0,0,0,0,0, \
/* pid etc.. */    0,0,0,0, \
/* suppl grps*/ {NOGROUP,}, \
/* proc links*/ &init_task.task,0,0,0, \
/* uid etc */    0,0,0,0,0,0, \
/* timeout */    0,0,0,0,0,0,0, \
/* rlimits */   { {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff},  \
          {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff}, \
          {0x7fffffff, 0x7fffffff}, {0x7fffffff, 0x7fffffff}}, \
/* flags */    0, \
/* math */    0, \
/* fs info */    -1,0022,NULL,NULL,NULL,NULL,0, \
/* filp */    {NULL,}, \
    { \
        {0,0}, \
/* ldt */    {0x9f,0xc0fa00}, \
        {0x9f,0xc0f200}, \
    }, \
/*tss*/    {0,PAGE_SIZE+(long)&init_task,0x10,0,0,0,0,(long)&pg_dir,\
     0,0,0,0,0,0,0,0, \
     0,0,0x17,0x17,0x17,0x17,0x17,0x17, \
     _LDT(0),0x80000000, \
        {} \
    }, \
}

3. 内存使用

内存使用情况,

unsigned long start_code,end_code,end_data,brk,start_stack;

4. 文件相关

本文系Spark & Shine原创,转载需注明出处本文最近一次修改时间 2023-04-13 23:58

results matching ""

    No results matching ""