DS_TD_Linux进程管理_0105_第1页
DS_TD_Linux进程管理_0105_第2页
DS_TD_Linux进程管理_0105_第3页
DS_TD_Linux进程管理_0105_第4页
DS_TD_Linux进程管理_0105_第5页
已阅读5页,还剩63页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、诚迈科技,诚迈科技,移动软件研发专家移动软件研发专家ArcherMind Technology (Nanjing) Co., Ltd.- A reliable software development service provider Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind

2、MarketingARCHERMINDMobile Software Expert 进程概述1 进程与线程2进程生命周期3进程间通信4目目 录录 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert进程进程vLinux是一个多任务多用户操作系统是一个多任务多用户操作系统v任务任务(task) 进程进程(process)系统资源(系统资源(cpu,时间,存储等,时间,存储等)分配的基本单位分配的基本单位使用使用CPU运行的基本调

3、度单位运行的基本调度单位进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert进程描述符进程描述符1进程状态进程状态2进程组织进程组织3 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert进程描述符进程描述符:对进程进行全面描述的数据结构

4、。:对进程进行全面描述的数据结构。进程描述符进程描述符struct task_struct volatile long state; /* -1 unrunnable, 0 runnable, 0 stopped */void *stack;atomic_t usage;unsigned int flags;/* per process flags, defined below */unsigned int ptrace;struct list_head tasks;struct list_head ptrace_children;struct list_head ptrace_list;pi

5、d_t pid;pid_t tgid;struct task_struct *parent; /* parent process */struct list_head children;/* list of my children */struct list_head sibling;/* linkage in my parents children list */进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software

6、Expert时间和时间和定定时器信时器信息息 描述进程在生存周期内使用描述进程在生存周期内使用CPU时间的统时间的统计、计费等信息。计、计费等信息。进程间进程间通信信息通信信息 描述多个进程在同一任务上协作工作。描述多个进程在同一任务上协作工作。各种各种标识符标识符用简单数字对进程进行标识。用简单数字对进程进行标识。链接信息链接信息描述进程的父子关系。描述进程的父子关系。状态信息状态信息描述进程动态的变化。描述进程动态的变化。进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind Mark

7、etingARCHERMINDMobile Software Expert调度信息调度信息描述进程优先级、调度策略等信息。描述进程优先级、调度策略等信息。文件系统文件系统信息信息对进程使用文件情况进行记录。对进程使用文件情况进行记录。虚拟内存虚拟内存信息信息描述每个进程拥有的地址空间。描述每个进程拥有的地址空间。处理器处理器环境信息环境信息 描述进程的执行环境(处理器的寄存器及描述进程的执行环境(处理器的寄存器及堆栈等)。堆栈等)。进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind M

8、arketingARCHERMINDMobile Software Expertunion thread_union struct thread_info thread_info;unsigned long stackTHREAD_SIZE/sizeof(long);#define THREAD_SIZE 8192struct thread_info struct task_struct *task;内核栈与进程描述符内核栈与进程描述符thread_infostructureprocessdescriptorstacksp进程描述符进程描述符 Email : Web : Project : G

9、eneral Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert此页作废此页作废中断向量表中断处理程序IRQINTRdo_IRQrequest_irqhandle_IRQ_eventhandler1特定的执行代码中特定的执行代码中raise_softirq进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Ex

10、pert标识进程描述符标识进程描述符static inline struct thread_info *current_thread_info(void) register unsigned long sp asm (sp); return (struct thread_info *)(sp & (THREAD_SIZE - 1);static inline struct task_struct *get_current(void) return current_thread_info()-task;#define current (get_current()current宏获取当前宏获取当前

11、正在运行的进程描正在运行的进程描述符的指针述符的指针内核可以方便而快速内核可以方便而快速地找到进程描述符地找到进程描述符进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert分配进程描述符分配进程描述符#define alloc_thread_info(tsk) (struct thread_info *)_get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER)#endi

12、f#define free_thread_info(info) free_pages(unsigned long)info, THREAD_SIZE_ORDER);# define alloc_task_struct() kmem_cache_alloc(task_struct_cachep, GFP_KERNEL)# define free_task_struct(tsk) kmem_cache_free(task_struct_cachep, (tsk)v分配分配 task_structv分配分配 thread_info进程描述符进程描述符 Email : Web : Project :

13、General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv链表链表v亲属关系亲属关系v队列队列运行队列运行队列等待队列等待队列vPID 哈希表哈希表进程组织进程组织进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertstruct list_head tasks;vLinux的所有进程组成了

14、一个的所有进程组成了一个双向链表双向链表: 进程链表进程链表进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertvlist_head链表结构管理,内核提供链表操作链表结构管理,内核提供链表操作的多种函数和宏。的多种函数和宏。struct list_head struct list_head *next, *prev;list entry;list add;list del;list move;进程描述符进

15、程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv程序创建的进程具有父子兄弟亲属关系。程序创建的进程具有父子兄弟亲属关系。进程间亲属关系进程间亲属关系struct task_struct *real_parent; struct task_struct *parent;struct list_head children;struct list_head sibling;P0P1P2P3P4parentchildr

16、ensiblingsibling进程描述符进程描述符children Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv等待队列表示一组睡眠的进程。等待队列表示一组睡眠的进程。v等待队列结构:等待队列结构:等待队列等待队列进程描述符进程描述符struct _wait_queue unsigned int flags;#define WQ_FLAG_EXCLUSIVE0 x01void *private;wait_queue

17、_func_t func;struct list_head task_list;typedef struct _wait_queue wait_queue_t; Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software ExpertvLinux中使用进程中使用进程PID(Process ID)来标识)来标识一个进程,每个进程都有自己独一无二的一个进程,每个进程都有自己独一无二的ID。vPID 散列表:散列表: 进程标识进程标识pid_t pid

18、;pid_t tgid;进程描述符进程描述符static struct hlist_head *pid_hash; Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software ExpertvLinux中的进程状态(中的进程状态(state):):进程状态进程状态#define TASK_RUNNING0#define TASK_INTERRUPTIBLE1#define TASK_UNINTERRUPTIBLE2#define _TASK_STO

19、PPED4#define _TASK_TRACED8/* in tsk-exit_state */#define EXIT_ZOMBIE16#define EXIT_DEAD32进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert暂停状态暂停状态 暂时停止接受某种处暂时停止接受某种处理(挂起)。理(挂起)。僵死状态僵死状态 执行接受但尚未消执行接受但尚未消亡,尚未释放进程资亡,尚未释放进程资源。源。可中断

20、的等待状态可中断的等待状态 等待资源有效时唤等待资源有效时唤醒,可被其他进程通醒,可被其他进程通过信号或中断唤醒。过信号或中断唤醒。不可中断的等待状态不可中断的等待状态 等待资源有效,不等待资源有效,不能由其他进程通过信能由其他进程通过信号和时钟中断唤醒。号和时钟中断唤醒。可运行状态可运行状态 正在运行或准备运行正在运行或准备运行的进程。的进程。进程状态进程状态 进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software E

21、xpert进程状态及转换进程状态及转换 TASK_RUNNING就绪就绪TASK_INTERRUPTIBLE浅度睡眠浅度睡眠TASK_UNINTERRUPTIBLE深度睡眠深度睡眠TASK_STOPPED暂停暂停TASK_ZOMBIE僵死僵死占有占有 CPU执行执行do_exit()schedule()ptrace()schedule()等待资源到位等待资源到位sleep_on()schedule()等待资源到位等待资源到位interruptible_sleep_on()schedule()资源到位或收到信号资源到位或收到信号wake_up_interruptible()资源到位资源到位wak

22、e_up()收到信号收到信号wake_up()schedule()schedule()进程描述符进程描述符 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv等待队列的相关函数:等待队列的相关函数:进程等待某一特定事件进程等待某一特定事件:sleep_on/ interruptible_sleep_on wait_for_completion/ wait_for_completion_interruptible唤醒进程进入

23、唤醒进程进入TASK_RUNNING状态状态:wake_up/ wake_up_interruptible 进程描述符进程描述符等待队列等待队列 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert 进程描述符进程描述符sleep_on_common(wait_queue_head_t *q, int state, long timeout) unsigned long flags; wait_queue_t wait; in

24、it_waitqueue_entry(&wait, current); _set_current_state(state); spin_lock_irqsave(&q-lock, flags); _add_wait_queue(q, &wait); spin_unlock(&q-lock); timeout = schedule_timeout(timeout); spin_lock_irq(&q-lock); _remove_wait_queue(q, &wait); spin_unlock_irqrestore(&q-lock, flags); return timeout; Email

25、: Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert进程描述符进程描述符do_wait_for_common(struct completion *x, long timeout, int state) if (!x-done) DECLARE_WAITQUEUE(wait, current); wait.flags |= WQ_FLAG_EXCLUSIVE; _add_wait_queue_tail(&x-wait, &wait);_s

26、et_current_state(state); spin_unlock_irq(&x-wait.lock); timeout = schedule_timeout(timeout); spin_lock_irq(&x-wait.lock); _remove_wait_queue(&x-wait, &wait); Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert进程与线程进程与线程进程与线程进程与线程1 内核线程内核线程2

27、 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert进程进程可执行文件(可执行文件( 程序)程序)系统堆栈空间系统堆栈空间进程描述符进程描述符用户空间用户空间线程线程可执行文件(可执行文件( 程序)程序)系统堆栈空间系统堆栈空间进程描述符进程描述符用户空间用户空间进程进程 线程线程进程与线程进程与线程 Email : Web : Project : General Usage Version: 1.0 Author: Arc

28、herMind MarketingARCHERMINDMobile Software ExpertvLinux使用使用轻量级进程轻量级进程(lightweight process) :轻量级进程可以轻量级进程可以同其它进程共享资源,例如内存地同其它进程共享资源,例如内存地址空间、打开的文件描述符、信号处理等等。址空间、打开的文件描述符、信号处理等等。通过将轻量级进程与每个线程相关联,线程之间可通过将轻量级进程与每个线程相关联,线程之间可共享内存地址空间等资源,并且可以由内核可以独共享内存地址空间等资源,并且可以由内核可以独立调度。立调度。轻量级进程对多线程应用提供了支持。轻量级进程对多线程应用

29、提供了支持。轻量级进程轻量级进程进程与线程进程与线程 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software ExpertvLinuxLinux线程线程:生存在进程中,并使用进程资源;生存在进程中,并使用进程资源;拥有拥有task_struct结构,拥有进程号结构,拥有进程号;独立调度;独立调度;拥有它自己独立的控制流(前提是只要它的父进程还拥有它自己独立的控制流(前提是只要它的父进程还存在);存在);仅仅复制可以使它自己调度的必要的资源;仅

30、仅复制可以使它自己调度的必要的资源;可能会同其它与之同等独立的线程分享进程资源;可能会同其它与之同等独立的线程分享进程资源;1.是轻量级的,因为大部分的开支已经在创建时完成了是轻量级的,因为大部分的开支已经在创建时完成了。 LinuxLinux线程线程进程与线程进程与线程 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert用户线程用户线程用户态用户态由用户线程创建由用户线程创建由内核线程创建由内核线程创建内核线程内核线程内核

31、态内核态拥有拥有task_structtask_struct结构结构; ;可调度,可抢占。可调度,可抢占。LinuxLinux线程线程进程与线程进程与线程 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert内核线程内核线程events/kworker 处理内核软硬件事件处理内核软硬件事件 kblockd 管理磁盘块读写管理磁盘块读写 kswapd执行内存回收执行内存回收kjournald Ext3 文件系统的日志管理文件系统

32、的日志管理 ksoftirqd 处理软中断处理软中断 kapmd处理电源管理相关处理电源管理相关进程与线程进程与线程 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv特点:特点:循环构成,从创建时永远执行下去;被唤醒执行后,自循环构成,从创建时永远执行下去;被唤醒执行后,自行休眠。行休眠。内核线程内核线程进程与线程进程与线程static int ksoftirqd(void * _bind_cpu) set_curren

33、t_state(TASK_INTERRUPTIBLE); while (!kthread_should_stop() schedule(); _set_current_state(TASK_RUNNING); while (local_softirq_pending() do_softirq(); set_current_state(TASK_INTERRUPTIBLE); _set_current_state(TASK_RUNNING); Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind Marketi

34、ngARCHERMINDMobile Software Expert特殊的进程特殊的进程Init0 通常称为通常称为swapper进程,是所有进程的祖先进程,是所有进程的祖先完成初始化进程描述符的各个对应域、页全局完成初始化进程描述符的各个对应域、页全局目录等工作,最后执行目录等工作,最后执行cpu_idle()函数,成为函数,成为idle进程。进程。Init1 由由init0创建,创建,PID为为1。通常称为。通常称为init进程,进程,为常规内核任务初始化一些内核线程。最后为常规内核任务初始化一些内核线程。最后作为一个从不终止的进程,创建和监控其他作为一个从不终止的进程,创建和监控其他进程

35、的活动。进程的活动。进程与线程进程与线程 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert进程的创建进程的创建1进程的执行进程的执行2进程的撤销进程的撤销3进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert进程相关的系统调用

36、进程相关的系统调用fork/vfork/clone函数函数创建进程创建进程/ /线程线程 读取可执行文件,读取可执行文件,并载入进程地址空间并载入进程地址空间执行执行 提供初级的进程同提供初级的进程同步措施,使一个进程步措施,使一个进程等待,直到另一个进等待,直到另一个进程结束为止程结束为止wait函数函数exit函数函数终止一个进程的运行终止一个进程的运行exec函数函数进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile S

37、oftware Expertvfork -创建子进程创建子进程vvfork- “写时复制写时复制”前的创建进程方式,减少系前的创建进程方式,减少系统开销。统开销。vclone -创建线程创建线程fork/vfork/clone进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertkernel_thread()vfork()fork()clone()sys_vfork()sys_fork()sys_c

38、lone()do_fork()copy_processuser_spacekernelwake_up_new_task进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software ExpertCLONE_VM: 子进程共享父进程内存描述符和所有页表(地址空间)子进程共享父进程内存描述符和所有页表(地址空间)CLONE_FS: 子进程共享父进程所在文件系统信息子进程共享父进程所在文件系统信息CLONE_FILES: 子进

39、程共享父进程打开的文件子进程共享父进程打开的文件CLONE_SIGHAND:子进程共享父进程信号处理:子进程共享父进程信号处理CLONE_VFORK:父进程被挂起,直至子进程释放虚拟内:父进程被挂起,直至子进程释放虚拟内 存资源存资源long do_fork(unsigned long clone_flags, unsigned long stack_start, struct pt_regs *regs, unsigned long stack_size, int _user *parent_tidptr, int _user *child_tidptr)do_fork进程的生命周期进程的生

40、命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertasmlinkage int sys_fork(struct pt_regs *regs)return do_fork(SIGCHLD, regs-ARM_sp, regs, 0, NULL, NULL);asmlinkage int sys_clone(unsigned long clone_flags, unsigned long newsp, int _user

41、 *parent_tidptr, int tls_val, int _user *child_tidptr, struct pt_regs *regs) return do_fork(clone_flags, newsp, regs, 0, parent_tidptr, child_tidptr);(一般常用(一般常用CLONE_VM| CLONE_FS| CLONE_FILES| CLONE_SIGHAND)进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind Marketin

42、gARCHERMINDMobile Software Expertasmlinkage int sys_vfork(struct pt_regs *regs) return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs-ARM_sp, regs, 0, NULL, NULL);pid_t kernel_thread(int (*fn)(void *), void *arg, unsigned long flags) return do_fork(flags|CLONE_VM|CLONE_UNTRACED, 0, ®s, 0, NULL, N

43、ULL); 进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertdup_task_structinit task_structsched_forkcopy_fscopy_filecopy_mmcopy_signalcopy_processcopy_process进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Aut

44、hor: ArcherMind MarketingARCHERMINDMobile Software Expertfork VS vfork VS clone(1)fork函数函数在创建新的子进程时候会复制所在创建新的子进程时候会复制所有父进程所拥有的资源,是父进有父进程所拥有的资源,是父进程的一个拷贝。程的一个拷贝。vfork函数函数父子进程共享地址空间。子进程父子进程共享地址空间。子进程对虚拟内存空间的修改实际上是对虚拟内存空间的修改实际上是对父进程虚拟内存空间的修改。对父进程虚拟内存空间的修改。 clone函数函数由用户通过参由用户通过参clone_flags 的设置的设置来决定父进程哪

45、些资源共享,哪来决定父进程哪些资源共享,哪些资源拷贝。些资源拷贝。 进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv由由fork创建的新进程称为创建的新进程称为子进程子进程。vfork函数的特点:函数的特点:“调用一次,返回两次调用一次,返回两次” 子进程的返回值是子进程的返回值是0 父进程的返回值是子进程的进程父进程的返回值是子进程的进程IDIDfork pid_t fork(void);

46、进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert原来的进程原来的进程fork()()原来的进程原来的进程继续执行继续执行新进程新进程返回一个新的返回一个新的pid返回返回0进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile S

47、oftware Expert 调用调用fork之后之后发发生了分叉,变成两生了分叉,变成两个不同的控制流个不同的控制流进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv运行结果:This is the child process 6363This is the parent process 6362This is the child process 6363This is the parent

48、 process 6362This is the child process 6363This is the parent process 6362This is the child process 6363This is the child process 6363This is the child process 6363进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv传统的进程直接把所

49、有的资源复制给子进程,传统的进程直接把所有的资源复制给子进程,实现简单并且效率低下。如果新进程执行新的实现简单并且效率低下。如果新进程执行新的映像时候,前面的拷贝就没有什么作用了。映像时候,前面的拷贝就没有什么作用了。v写时复制写时复制是一种推迟甚至免除复制的技术:是一种推迟甚至免除复制的技术:子进程复制父进程的页表,以只读的方式共享。子进程复制父进程的页表,以只读的方式共享。当两者之一去写物理页时候,内核才将这个页的内当两者之一去写物理页时候,内核才将这个页的内容拷贝到新的物理页,并把这个新的物理页分配给容拷贝到新的物理页,并把这个新的物理页分配给写进程。写进程。写时复制写时复制进程的生命周

50、期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert子进程返回子进程返回0,父进程返回子进程的,父进程返回子进程的pid号。号。错误返回错误返回-1。vvfork()的实现类似()的实现类似fork函数,在性能上有函数,在性能上有所提高。所提高。vvfork()创建新进程时,父子进程共享虚拟()创建新进程时,父子进程共享虚拟内存空间。内存空间。vfork pid_t vfork(void);进程的生命周期进程

51、的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert#include #include int glob=6;int main()int var = 88;pid_t pid;printf(in beginning: glob=%dt var=%dn,glob,var);if (pid=vfork() = 0)glob+;var+;printf(in child process: glob=%dt var=%dn,

52、glob,var);_exit(0);else if(pid 0)perror(vfork failed!n);exit(1); else printf(in parent process:glob=%dt var=%dn,glob,var);return 0; 进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv运行结果:in beginning: glob=6 var=88in child

53、process: glob=7 var=89in parent process:glob=7 var=89进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expert返回创建进程的返回创建进程的PID号,错误返回号,错误返回-1。fn: 执行的过程;执行的过程;stack:使用的堆栈。:使用的堆栈。v创建轻量级进程(线程)的系统调用,有选择的创建轻量级进程(线程)的系统调用,有选择的设置父子进程之间需共享

54、的资源(设置父子进程之间需共享的资源(flag)。)。clone int clone(int (*fn)(void *), void *child_stack, int flags, void *arg)进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertint variable,fd;int do_something()variable = 42;printf(in child processn

55、);close(fd); _exit(0);return 0;int main(int argc, char *argv)void *child_stack;char tempch;variable = 9;fd = open(/test.txt, O_RDONLY);child_stack = (void*)malloc(16384);printf(The variable is %dn,variable);clone(do_something,child_stack+10000,CLONE_VM|CLONE_FILES,NULL);sleep(3);printf(The variable

56、is now %dn,variable);if (read(fd,&tempch,1) 1)perror(File Read Error);exit(1);进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertv运行结果:The variable is 9in child processThe variable is now 42File Read Error: Bad file descripto

57、r进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMINDMobile Software Expertfork VS vfork VS clone(2)fork函数函数不对父子进程的执行次序进行任何限制不对父子进程的执行次序进行任何限制,fork返回后,子进程和父进程都从调返回后,子进程和父进程都从调用用fork函数的下一条语句开始执行。函数的下一条语句开始执行。vfork函数函数子进程先运行,父进程挂起,直到子进子进程先运行,父进程挂起,直到

58、子进程调用了程调用了exec或或_ _exit之后,父子进程的之后,父子进程的执行次序才不再有限制执行次序才不再有限制。clone函数函数由标志由标志CLONE_VFORK来决定子进程来决定子进程在执行时父进程是阻塞还是运行,若没在执行时父进程是阻塞还是运行,若没有设置该标志,则父子进程同时运行,有设置该标志,则父子进程同时运行,设置了该标志,则父进程挂起,直到子设置了该标志,则父进程挂起,直到子进程结束为止。进程结束为止。进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind M

59、arketingARCHERMINDMobile Software Expertv用用fork创建子进程后,调用创建子进程后,调用exec函数函数,进程完,进程完全由新程序替代,使父进程和子进程执行不同全由新程序替代,使父进程和子进程执行不同的任务。的任务。子进程的地址空间被填入可执行文件的内容,其进子进程的地址空间被填入可执行文件的内容,其进程功能开始有别于父进程。程功能开始有别于父进程。exec函数执行成功就会进入新进程执行不再返回。函数执行成功就会进入新进程执行不再返回。exec调用失败才返回调用失败才返回-1-1,继续在克隆来的地址空间,继续在克隆来的地址空间中从调用点向下执行中从调用点向下执行vexec并不创建新进程,是用另外一个程序关系并不创建新进程,是用另外一个程序关系替代了当前进程的正文、数据、堆和栈。替代了当前进程的正文、数据、堆和栈。exec 进程的生命周期进程的生命周期 Email : Web : Project : General Usage Version: 1.0 Author: ArcherMind MarketingARCHERMIND

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论