歡迎您光臨本站 註冊首頁

進程式控制制塊(一)

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0
今天學習了關於進程式控制制塊的部分知識,只是由於覺得比較抽象,便在網上收集了一些信息和實例,以加深理解. 一、定義 進程式控制制塊(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;

struct task_struct *next_task, *prev_task;
//內核會對每一個進程做點什麼事情的時候,常常需要將其連成一個隊列,這2個指針用於這個目的
struct mm_struct *active_mm;
struct list_head local_pages;
unsigned int allocation_order, nr_local_pages;

/* 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;

/* PID hash table linkage. */
struct task_struct *pidhash_next;
struct task_struct **pidhash_pprev; //pid是隨機分配的,我們常常使用kill pid想進程發送信號(大部分人認為是殺死進程,其實這是個發送信號的指令,默認的參數為殺死.如果想暫停某進程,只需kill STOP 進程的PID),這裡可以看到根據pid尋找進程的操作是經常被使用的,而pid又是隨機分配,於是這裡邊用這2個指針指向一個雜湊數組,數組是按照雜湊的演算法,以pid為關鍵字建立,方便根據pid來尋找task_struct

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;
};

#define TASK_RUNNING 0 //不是表示正在運行,而是表示可以被調

#define TASK_INTERRUPTIBLE 1
#define TASK_UNINTERRUPTIBLE 2
#define TASK_ZOMBIE 4
#define TASK_STOPPED 8
//對應於task_struct中的state,進程運行狀態


//對應task_struct的flag
#define PF_ALIGNWARN 0x00000001
/* Print alignment warning msgs */
/* Not implemented yet, only for 486*/
#define PF_STARTING 0x00000002
/* being created */
#define PF_EXITING 0x00000004
/* getting shut down */
#define PF_FORKNOEXEC 0x00000040
/* forked but didn't exec */
#define PF_SUPERPRIV 0x00000100
/* used super-user privileges */
#define PF_DUMPCORE 0x00000200

/* dumped core */
#define PF_SIGNALED 0x00000400
/* killed by a signal */
#define PF_MEMALLOC 0x00000800
/* Allocating memory */
#define PF_MEMDIE 0x00001000
/* Killed for out-of-memory */
#define PF_FREE_PAGES 0x00002000
/* per process page freeing */
#define PF_NOIO 0x00004000
/* avoid generating further I/O */
#define PF_FSTRANS 0x00008000
/* inside a filesystem transaction */

#define PF_USEDFPU 0x00100000 /* task used FPU this quantum (SMP) */

//進程的個性化信息
enum {
MMAP_PAGE_ZERO = 0x0100000,
ADDR_LIMIT_32BIT = 0x0800000,
SHORT_INODE = 0x1000000,
WHOLE_SECONDS = 0x2000000,
STICKY_TIMEOUTS = 0x4000000,
ADDR_LIMIT_3GB = 0x8000000,
};

/*
* Personality types.
*
* These go in the low byte. Avoid using the top bit, it will
* conflict with error returns.
*/
enum {
PER_LINUX = 0x0000,
PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS |
WHOLE_SECONDS | SHORT_INODE,
PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,


PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
PER_BSD = 0x0006,
PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
PER_LINUX32 = 0x0008,
PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
PER_RISCOS = 0x000c,
PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
PER_HPUX = 0x000f,
PER_OSF4 = 0x0010, /* OSF/1 v4 */
PER_MASK = 0x00ff,
};

//進程資源的限制,對應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 */

本文出自 「只是博客而已」 博客,請務必保留此出處http://forrestcao.blog.51cto.com/2186939/396374


[火星人 ] 進程式控制制塊(一)已經有493次圍觀

http://coctec.com/docs/linux/show-post-49451.html