《进程控制开发》PPT课件.ppt
《《进程控制开发》PPT课件.ppt》由会员分享,可在线阅读,更多相关《《进程控制开发》PPT课件.ppt(68页珍藏版)》请在三一办公上搜索。
1、第7章 进程控制,Linux进程概述 Linux进程控制编程 Linux守护进程实验,进程的定义,进程的参考定义有如下几种:1、进程是程序的一次执行过程;2、进程=PCB+程序+数据;3、进程是一个可拥有资源的独立实体,同时又是一个可以独立调度的基本单位。,进程与程序的区别,动态性与静态进程由“创建”而产生,由“调度”而执行;由得不到资源而阻塞;由撤消而消亡。而程序是静态的,是完成某个特定功能的指令的有序序列,我们把程序看成是一个菜谱,而进程则是按照菜谱进行烹调的过程。结构特征从结构上看,每个进程是由程序段、数据段和进程控制块三部分组成,总称“进程映像”。程序无控制块。,进程与程序的区别,并发
2、性多个进程实体,同存于内存中,能在一段时间内同时执行;程序是不能并发执行的,只有建立了进程,才能并发执行。独立性。进程是系统进行资源分配和调度的一个独立单位;程序则不是。进程具有创建其它进程的功能,小结,进程的状态,进程通常有3种状态。执行态:该进程正在运行,即进程正在占用CPU。就绪态:进程已经具备执行的一切条件,正在等待分配CPU的处理时间片。等待态:进程不能使用CPU,若等待事件发生(等待的资源分配到)则可将其唤醒。,Linux进程状态,Linux进程状态及转换,fork(),TASK_RUNNING,就绪,TASK_INTERRUPTIBLE,浅度睡眠,TASK_UNINTERRUPT
3、IBLE,深度睡眠,TASK_STOPPED,暂停,TASK_ZOMBIE,僵死,占有,CPU,运行,do_exit(),schedule(),ptrace(),schedule(),时间片耗尽,等待资源到位,sleep_on(),schedule(),等待资源到位,interruptible_sleep_on(),schedule(),资源到位,wake_up_interruptible(),或收到信号,wake_up(),资源到位,wake_up(),收到信号,SIGCONT,wake_up(),TASK_RUNNING,进程控制块,进程控制块是进程存在的标志,每一进程都有一进程控制块,L
4、inux中用数据结构task_struct表示,并将指向每个进程控制块的指针保存在一个数组task中,数组下标的最大值为512,表明Linux允许的进程数最多可有 512个。Linux进程控制块也叫进程描述符(Process Descriptor),进程控制块信息分类,状态信息描述进程状态的变化。链接信息描述进程的父子关系。各种标识符用简单数字对进程进行标识。进程间通信信息描述多个进程在同一任务上协作工作。时间和定时器信息描述进程在生存周期内使用CPU时间的统计、计费等信息。调度信息描述进程优先级、调度策略等信息。文件系统信息对进程使用文件情况进行记录。虚拟内存信息描述每个进程拥有的地址空间。
5、处理器环境信息描述进程的执行环境(处理器的寄存器及堆栈等),struct task_struct volatile long state;struct thread_info*thread_info;atomic_t usage;unsigned long flags;unsigned long ptrace;int lock_depth;#if defined(CONFIG_SMP),Linux进程控制块数据结构,unsigned long long timestamp,last_ran;unsigned long long sched_time;int activated;unsigned
6、 long policy;cpumask_t cpus_allowed;unsigned int time_slice,first_time_slice;#ifdef CONFIG_SCHEDSTATSstruct sched_info sched_info;#endifstruct list_head tasks;struct list_head ptrace_children;struct list_head ptrace_list;struct mm_struct*mm,*active_mm;struct linux_binfmt*binfmt;,Linux进程控制块数据结构,long
7、exit_state;int exit_code,exit_signal;int pdeath_signal;unsigned long personality;unsigned did_exec:1;pid_t pid;pid_t tgid;struct task_struct*real_parent;struct task_struct*parent;struct list_head children;struct list_head sibling;struct task_struct*group_leader;,Linux进程控制块数据结构,struct pid pidsPIDTYPE
8、_MAX;struct completion*vfork_done;int _user*set_child_tid;int _user*clear_child_tid;unsigned long rt_priority;cputime_t utime,stime;unsigned long nvcsw,nivcsw;struct timespec start_time;unsigned long min_flt,maj_flt;cputime_t it_prof_expires,it_virt_expires;unsigned long long it_sched_expires;struct
9、 list_head cpu_timers3;uid_t uid,euid,suid,fsuid;gid_t gid,egid,sgid,fsgid;struct group_info*group_info;,Linux进程控制块数据结构,kernel_cap_t cap_effective,cap_inheritable,cap_permitted;unsigned keep_capabilities:1;struct user_struct*user;#ifdef CONFIG_KEYSstruct key*thread_keyring;unsigned char jit_keyring;
10、#endifint oomkilladj;char commTASK_COMM_LEN;int link_count,total_link_count;struct sysv_sem sysvsem;struct thread_struct thread;struct fs_struct*fs;,Linux进程控制块数据结构,struct files_struct*files;struct namespace*namespace;struct signal_struct*signal;struct sighand_struct*sighand;sigset_t blocked,real_blo
11、cked;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;struct audit_context*audit_context;seccomp_t seccomp;u32 parent_exec_id;u32 self_exec_id;,Linux进程控制块数据结构,spinlock_t alloc_lock;spinlock_t proc_l
12、ock;void*journal_info;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;wait_queue_t*io_wait;u64 rchar,wchar,syscr,syscw;,Linux进程控制块数据结构,#if defined(CONFIG_BSD_PROCESS
13、_ACCT)u64 acct_rss_mem1;u64 acct_vm_mem1;clock_t acct_stimexpd;#endif#ifdef CONFIG_NUMA struct mempolicy*mempolicy;short il_next;#endif#ifdef CONFIG_CPUSETSstruct cpuset*cpuset;nodemask_t mems_allowed;int cpuset_mems_generation;#endifatomic_t fs_excl;,Linux进程控制块数据结构,每个进程都有一个唯一的标识符PID,内核通过这个标识符来识别不同的
14、进程,用户程序通过PID对进程发号施令,PID是32位的无符号整数,它被顺序编号,1、2、.、32767每个进程都属于某个用户组。task_struct结构中还定义有用户标识符UID(User Identifier)和组标识符GID(Group Identifier)这两种标识符用于系统的安全控制,系统通过这两种标识符控制进程对系统中文件和设备的访问。,进程标识符PID,Linux下进程地址空间,Linux系统是一个多进程的系统,每个进程都是一个独立的运行单位,运行在独立的虚拟地址空间,即使一个进程发生异常,它也不会影响到系统中的其他进程。Linux下进程地址空间包含3个段,分别为“数据段”、
15、“代码段”和“堆栈段”。数据段:存放全局变量、常数以及动态分配的数据。数据段又可以再分成普通数据段(包括可读可写/只读数据段,存放静态初始化的全局变量或常量)、BSS数据段(存放未初始化的全局变量)以及堆(存放动态分配的数据)。代码段:存放程序代码。堆栈段:存放子程序的返回地址、参数以及程序的局部变量等。,Linux下进程地址空间,Linux下进程的运特模式,在Linux系统中,进程的执行模式划分为用户模式和内核模式用户模式:若进程执行的是用户程序、应用程序或者内核之外的系统程序,则处在该模式下,也称为用户态内核模式:用户程序执行中出现系统调用或者发生中断事件,就要运行核心程序,进程模式就转变
16、为内核模式,也称进入核心态内核模式下运行的进程可以执行机器的特权指令,访问受操作系统保护的资源,第7章 进程控制,Linux进程概述 Linux进程控制编程 Linux守护进程实验,创建进程,fork(),fork1.c.#include#include#include int main()pid_t pid;char*message;int n;printf(“fork program startingn”);pid=fork();,switch(pid)case-1:perror(“fork failed”);exit(1);case 0:message=“This is the chil
17、d”;n=5;break;default:message=“This is the parent”;n=3;break;for(;n 0;n-)puts(message);sleep(1);exit(0);,示例补1,$./fork1fork program startingThis is the parentThis is the childThis is the parentThis is the childThis is the parentThis is the child$This is the childThis is the child,fork()从已存在的进程中创建一个称为子
18、进程的新进程,而原进程称为父进程。fork()使用“写时复制”技术产生新进程。即只复制父进程的控制块、内核栈与页表并作必要修改即产生一个新/子进程。让子进程共享父进程的地址空间,包括代码段、进程堆栈、内存信息、打开的文件描述符、资源限制和控制终端等。当父、子进程中有一个要写入时才进行复制/增加一个拷贝,如果子进程产生后立即调用exec()函数,就可避免写时拷贝的额外开销。,父子两个进程运行同一个程序,与创建进程的初衷相左,因此需要用一种方式来区分它们,并使它们运行不同的程序,否则,这两个进程不可能做不同的事。什么方式?条件或分支指令exec()函数,进程结构,进程创建,代码,数据,mm_str
19、uct,task_struct,vm_area_struct,mm,vmmap,进程地址空间,pdg,页表,物理内存,父进程,写时复制;让子进程先运行,其可能会执行exec()读入其代码执行.,进程创建,mm_struct,vm_area_struct,vmmap,pdg,页表,fork(),进程结构,进程创建,代码,数据,mm_struct,task_struct,vm_area_struct,mm,vmmap,进程地址空间,pdg,页表,物理内存,task_struct,mm,父进程,子进程,vfork(),exec函数族(1),exec函数族提供了一个在进程中启动另一个程序运行的方法。它
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 进程控制开发 进程 控制 开发 PPT 课件
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-4859919.html