今天學習了關於進程式控制制塊的部分知識,只是由於覺得比較抽象,便在網上收集了一些信息和實例,以加深理解. 一、定義 進程式控制制塊(PCB):系統為了管理進程設置的一個專門的數據結構,用它來記錄進程的外部特徵,描述進程的運動變化過程.系統利用PCB來控制和管理進程,PCB是系統感知進程存在的唯一標誌.進程與PCB是一一對應的. 二、PCB存放的信息 在不同的操作系統中對進程的控制和管理機制不同,PCB中的信息多少也不一樣,通常PCB應包含如下一些信息. 1、進程標識符 name每個進程都必須有一個唯一的標識符,可以是字元串,也可以是一個數字. 2、進程當前狀態 status說明進程當前所處的狀態.為了管理的方便,系統設計時會將相同的狀態的進程組成一個隊列,如就緒進程隊列,等待進程則要根據等待的事件組成多個等待隊列,如等待印表機隊列、等待磁碟I/O完成隊列等等. 3、進程相應的程序和數據地址,以便把PCB與其程序和數據聯繫起來. 4、進程資源清單.列出所擁有的除CPU外的資源記錄,如擁有的I/O設備,打開的文件列表等. 5、進程優先順序 priority:進程的優先順序反映進程的緊迫程度,通常由用戶指定和系統設置. 6、CPU現場保護區 cpustatus:當進程因某種原因不能繼續佔用CPU時(如等待印表機),釋放CPU ,這時就要將CPU的各種狀態信息保護起來,為將來再次得到處理機恢復CPU的各種狀態,繼續運行. 7、進程同步與通信機制 用於實現進程間互斥、同步和通信所需的信號 量等. 8、進程所在隊列PCB的鏈接字 根據進程所處的現行狀態,進程相應的PCB參加到不同隊列中.PCB鏈接字指出該進程所在隊列中下一個進程PCB的首地址. 9、與進程有關的其他信息. 如進程記賬信息,進程佔用CPU的時間等. 三、linux下的task_struc進程式控制制塊t數據結構 在linux中具體實現是 task_struct數據結構,它記錄了一下幾個類型的信息: 1.狀態信息,例如這個進程處於可執行狀態,休眠,掛起等. 2.性質,由於unix有很多變種,進程有自己獨特的性質. 3.資源,資源的鏈接比如內存,還有資源的限制和許可權等. 4.組織,例如按照家族關係建立起來的樹(父進程,子進程等). 下面是這一個數據結構 struct task_struct { /* * offsets of these are hardcoded elsewhere - touch with care */ volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */ //進程當前的狀態 unsigned long flags; /* per process flags, defined below */ //反應進程狀態的信息,但不是運行狀態,定義見下 int sigpending; //進程收到了信號,但尚未處理 mm_segment_t addr_limit; /* thread address space: //虛存地址上限 0-0xBFFFFFFF for user-thead 0-0xFFFFFFFF for kernel-thread */ struct exec_domain *exec_domain; volatile long need_resched; //與進程調度有關表示用戶從系統空間按返回用戶空間要執行的一次調度 unsigned long ptrace;
int lock_depth; /* Lock depth */
/* * offset 32 begins here on 32-bit platforms. We keep * all fields in a single cacheline that are needed for * the goodness() loop in schedule(). */ long counter; //與進程調度相關 long nice; unsigned long policy; //實用於本進程的調度政策 struct mm_struct *mm; int processor; /* * cpus_runnable is ~0 if the process is not running on any * CPU. It's (1 << cpu) if it's running on a CPU. This mask * is updated under the runqueue lock. * * To determine whether a process might run on a CPU, this * mask is AND-ed with cpus_allowed. */ unsigned long cpus_runnable, cpus_allowed; /* * (only the 'next' pointer fits into the cacheline, but * that's just fine.) */ struct list_head run_list; unsigned long sleep_time;
/* 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; unsigned task_dumpable:1; pid_t pid; //進程號 pid_t pgrp; 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->p_pptr->pid) */ struct task_struct *p_opptr, *p_pptr, *p_cptr, *p_ysptr, *p_osptr; //用於族譜信息的,例如p_opptr指向父進程 struct list_head thread_group;
wait_queue_head_t wait_chldexit; /* for wait4() */ struct completion *vfork_done; /* for vfork() */ 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 tms times; //運行時間的總匯 unsigned long start_time; long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS]; //在多個處理器上運行於系統空間和用戶空間的時間 /* 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;//發生頁面異常的次數和換入換出的次數 int swappable:1; /* 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 */ unsigned int locks; /* How many file locks are being held */ /* ipc stuff */ struct sem_undo *semundo; struct sem_queue *semsleeping; /* 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 */ spinlock_t sigmask_lock; /* Protects signal and blocked */ struct signal_struct *sig;
sigset_t 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;
/* Thread group tracking */ u32 parent_exec_id; u32 self_exec_id; /* Protection of (de-)allocation: mm, files, fs, tty */ spinlock_t alloc_lock;
/* journalling filesystem info */ void *journal_info; };
//進程資源的限制,對應task_struct中的struct rlimit rlim[RLIM_NLIMITS],RLIM_NLIMITS的值是11,代表11項資源,分別是 #define RLIMIT_CPU 0 /* CPU time in ms */ #define RLIMIT_FSIZE 1 /* Maximum filesize */ #define RLIMIT_DATA 2 /* max data size */ #define RLIMIT_STACK 3 /* max stack size */ #define RLIMIT_CORE 4 /* max core file size */ #define RLIMIT_RSS 5 /* max resident set size */ #define RLIMIT_NPROC 6 /* max number of processes */ #define RLIMIT_NOFILE 7 /* max number of open files */ #define RLIMIT_MEMLOCK 8 /* max locked-in-memory address space */ #define RLIMIT_AS 9 /* address space limit */ #define RLIMIT_LOCKS 10 /* maximum file locks held */