linux环境编程第五章,Linux系统编程(第五章) 进程.pdf

第五章 进程及相关第五章 进程及相关 目标 目标 本章旨在向学员介绍Linux系统 下进程的概念及相关的系统调用 使用 本章旨在向学员介绍Linux系统 下进程的概念及相关的系统调用 使用 1 掌握进程的特点及应用 掌握进程的特点及应用 2 掌握进程相关的系统调用函 数的使用 掌握进程相关的系统调用函 数的使用 时间 时间 6 学时 教学方法 讲授 学时 教学方法 讲授PPT 实例练习 实例练习 5 1 什么是进程 什么是进程 概念概念 每个运行着的程序实例就构成一个进程 就好象 linux系统下ls命令 实现实现 执行cat a txt wc l 从操作上看是实现统计 a txt文本的行数的功能 从程序的角度 就是两个 进程间通过管道进行通信的例子 特点特点 linux系统允许多用户访问系统 每个用户可以同时运 行许多程序 或者同时运行同一个程序的许多个实例 组成组成 进程由程序代码 数据 变量 打开的文件和环境组 成 linux系统会在进程之间共享程序代码和系统函数 库 5 1 什么是进程 什么是进程 两个用户同时调用grep命令的效果 grep troi nextgen doc trek txtnextgen doc neil 文件文件 函数库函数库 数据 s kirk 代码 PID 101 数据 s troi 代码 PID 102 grep程序代码程序代码 C程序函数库程序函数库 grep kirk trek txt rick 5 2 进程进程 进程id 每个进程都有唯一的进程ID号 通过进程ID可以查找到相应的进 程及了解进程的当前状态 include include pid t getpid void 返回进程返回进程ID pid t getppid void 返回父进程返回父进程ID uid t getuid void 返回实用户返回实用户ID uid t geteuid void 返回有效用户返回有效用户ID gid t getgid void 返回真实组返回真实组ID gid t getegid void 返回有效组返回有效组ID 5 2 进程编程进程编程 fork系统调用 功能为创建进程 include include pid t fork void fork fork 最初的进程最初的进程最初的进程最初的进程 原进程原进程原进程原进程 继续执行继续执行继续执行继续执行 新进程新进程新进程新进程 返回新PID 返回 0 返回新PID 返回 0 include pid t pid pid fork 5 2 进程编程进程编程 实验 fork系统调用 include include include int main pid t pid char message int n printf fork program staring n pid fork switch pid case 1 perror fork failed exit 1 case 0 message This is the child n 5 break default message This is the parent n 3 break for n 0 n puts message sleep 1 exit 0 5 2 进程编程进程编程 fork 与vfork 的区别 vfork 使用中父子进程共享虚拟内存空间 fork 则不是 vfork保证子进程先运行 而fork的父子进程运行顺序是不定 的 它取决于内核的调度算法 5 2 进程编程进程编程 实验 vfork 与fork 的区别1 include include include int main pid t pid pid vfork if pid 0 printf vfork failed n else if pid 0 sleep 1 prntf hello I am child n else printf hello I am parent n exit 0 5 2 进程编程进程编程 实验 vfork 与fork 的区别2 include include include int main int i 0 pid t pid pid vfork if pid 0 printf vfork failed n else if pid 0 sleep 1 i printf hello I am child d n i else printf hello I am parent d n i exit 0 5 2 进程编程进程编程 wait系统调用 功能为当子进程运行时 会挂起调用进程 wait经常会在fork之后被父进程调用 include include pid t wait int stat loc WIFEXITED stat val 如果子进程正常结束 取非零值 WEXITSTATUS stat val 如果WIFEXITED非零 返回子进程的退出码 WIFSIGNALED stat val 如果子进程因为一个未捕获的信号终止 取非零值 WTERMSIG stat val 如果WIFSIGNALED非零 返回一个信号代码 WIFSTOPPED stat val 如果子进程意外终止 取一个非零值 WSTOPSIG stat val 如果WIFSTOPPED非零 返回一个信号代码 5 2 进程编程进程编程 例程 wait系统调用1 include int main pid t pid pid t childpid if pid fork 0 sleep 1 printf hello I am child n else childpid wait NULL printf hello I am parent child has finished PID d n childpid exit 0 5 2 进程编程进程编程 例程 wait系统调用2 include int main pid t pid pid t child pid int status int n printf fork program staring n if pid fork 0 printf I am child n sleep 10 if pid 0 child pid wait printf Child has finished PID d n child pid if WIFEXITED status printf Child exited with code d n WEXITSTATUS status else printf Child terminated abnormaily n exit 0 5 2 进程编程进程编程 exec系统调用 功能为装载新的程序 并转换到调用进程的内 存控件 execl execlp和execle的参数个数是可变的 execv和execvp的第二个参数是一个字符串数组 include int execl const char path const char arg0 char 0 int execlp const char file const char arg0 char 0 int execle const char path const char arg0 char 0 char const envp int execv const char path char const argv int execvp const char file char const argv int execv const char path char const argv char const envp 5 2 进程编程进程编程 所有的exec系统调用都是用execve调用实现的 execve execveexecve execl execvpexecv execleexeclp 5 2 进程编程进程编程 实验 execlp系统调用 include include int main printf Running ps with execlp n execlp ps ps ax 0 printf Done n exit 0 5 2 进程编程进程编程 实验 execl系统调用 include include int main int argc char argv char filename if argc 2 fprintf stderr usage useupper file n exit 1 filename argv 1 if freopen filename r stdin fprintf stderr could not redirect stdin from file s n filename exit 2 execl upper upper 0 perror could not exec upper exit 3 upper c include include int main int ch while ch getchar EOF putchar toupper ch exit 0 5 2 进程编程进程编程 实验 exec和fork include main pid t pid switch pid fork case 1 fatal fork failed break case 0 execl bin ls ls l char 0 fatal exec failed break default wait int 0 printf is completed n exit 0 int fatal char s perror s exit 1 5 2 进程编程进程编程 实验 fork与文件 include include main int fd pid t pid char buf 10 if fd open filedata O RDONLY 1 fatal open failed read fd buf 10 printpos Before fork fd switch pid fork case 1 fatal fork failed break case 0 printpos Child before read fd read fd buf 10 printpos Child after read fd break default wait int 0 printpos Parent after wait fd int printpos const char string int filedes off t pos if pos lseek filedes 0 SEEK CUR 1 fatal lseek failed printf s ld n string pos 5 2 进程编程进程编程 进程终止 main函数调用return 调用exit 调用 exit 调用abort 被一个信号终止 5 2 进程编程进程编程 exit函数与 exit函数 exit函数作为c标准库的一部分 使程序正常终止并且返回父进程 状态 include int exit int status include int exit status 5 2 进程编程进程编程 abort函数 异常终止一个程序 include void abort void include include int main void abort exit EXIT SUCCESS 5 2 进程编程进程编程 kill函数 一个进程可以用kill函数杀死另一个进程 pid指定了要杀死的进程 sig是要发送的信号 include include int kill pid t pid int sig 5 2 进程编程进程编程 实验 kill函数 include include include include include int main void pid t child int status retval if child fork 0 perror fork exit EXIT FAILURE if child 0 sleep 1000 exit EXIT SUCCESS else if waitpid child if retval puts kill failed n perror kill waitpid child else printf d killed n child exit EXIT SUCCESS 5 2 进程编程进程编程 僵死进程与提前退出 在exit和wait正常使用的情况下 每个子进程都会被父进程所等待 但有两种情况除外 1 子进程在其父进程没有调用wait的情况下退出 2 父进程退出 而它的一个或多个子进程还在运行 5 2 进程编程进程编程 实验 僵尸进程 include include include int main pid t pid char message int n printf fork program staring n pid fork switch pid case 1 perror fork failed exit 1 case 0 message This is the child n 3 break default message This is the parent n 5 break for n 0 n puts message sleep 1 exit 0 Copyright 2008 版权所有 东软集团

展开阅读全文

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值