进程是一个正在执行的程序。
进程是程序的一个运行实例。
进程能分配处理器并由处理器执行的实体。
如果从内核的角度看的话:进程是分配系统资源的单位。当一个程序被加载到内存之后并为他分配一个PCB(进程控制块),这时候就称为进程了。在linux中PCB就是一个名字叫做task_struct的结构体,我们叫他”进程描述符”。它里面有进程执行的所有信息,所以CPU对task_struct进行管理就相当于在对进程进行管理。
PCB叫做进程控制块,它用来维护进程相关的信息,每个进程都有一个PCB。在linux中这个PCB是一个叫做task_struct的结构体。
task_struct :
在linux中,每一个进程都有一个进程描述符,这个”进程描述符”是一个结构体名字叫做task_struct,在task_struct里面保存了许多关于进程控制的信息。
task_struct是Linux内核的一种数据结构,它会被装载到RAM里并包含进程的信息。每个进程都把它的信息放在task_struct这个数据结构里面,而task_struct包含以下内容:
标示符:描述本进程的唯一标示符,用来区别其他进程。
状态:任务状态,退出代码,退出信号等。
优先级:相对于其他进程的优先级。
程序计数器:程序中即将被执行的下一条指令的地址。
内存指针:包括程序代码和进程相关数据的指针,还有和其他进程共享的内存块的指针。
上下文数据:进程执行时处理器的寄存器中的数据。
I/O状态信息:包括显示的I/O请求,分配给进程的I/O设备和正在被进程使用的文件列表。
记账信息:可能包括处理器时间总和,使用的时钟总数,时间限制,记账号等。
文件系统数据成员:
(1) struct fs_struct *fs;
(2) struct files_struct *files;
(3) int link_count;
- struct task_struct {
- volatile long state;
- unsigned long flags;
- int sigpending;
- mm_segment_t addr_limit;
-
-
-
- volatile long need_resched;
- int lock_depth;
- long nice;
-
- unsigned long policy;
- struct mm_struct *mm;
- int processor;
-
- unsigned long cpus_runnable, cpus_allowed;
- struct list_head run_list;
- unsigned long sleep_time;
-
- struct task_struct *next_task, *prev_task;
- struct mm_struct *active_mm;
- struct list_head local_pages;
- unsigned int allocation_order, nr_local_pages;
- struct linux_binfmt *binfmt;
- int exit_code, exit_signal;
- int pdeath_signal;
- unsigned long personality;
-
- int did_exec:1;
- pid_t pid;
- pid_t pgrp;
- pid_t tty_old_pgrp;
- pid_t session;
- pid_t tgid;
- int leader;
- struct task_struct *p_opptr,*p_pptr,*p_cptr,*p_ysptr,*p_osptr;
- struct list_head thread_group;
- struct task_struct *pidhash_next;
- struct task_struct **pidhash_pprev;
- wait_queue_head_t wait_chldexit;
- struct completion *vfork_done;
- 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_value;
- struct timer_list real_timer;
- struct tms times;
- unsigned long start_time;
-
- long per_cpu_utime[NR_CPUS], per_cpu_stime[NR_CPUS];
-
-
-
-
-
- unsigned long min_flt, maj_flt, nswap, cmin_flt, cmaj_flt, cnswap;
- int swappable:1;
-
-
-
-
-
-
- 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;
- struct rlimit rlim[RLIM_NLIMITS];
- unsigned short used_math;
- char comm[16];
-
- int link_count, total_link_count;
-
- struct tty_struct *tty;
- unsigned int locks;
-
- struct sem_undo *semundo;
- struct sem_queue *semsleeping;
-
- struct thread_struct thread;
-
- struct fs_struct *fs;
-
- struct files_struct *files;
-
- spinlock_t sigmask_lock;
- 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;
- u32 parent_exec_id;
- u32 self_exec_id;
-
- spinlock_t alloc_lock;
- void *journal_info;
- };