1 Executing a Programing

  1. exec将加载新的进程,并替换掉现在的进程
  2. exev execvp execvpe
    1. exev
    2. execvp
    3. execvpe

1.1 Using execv and execvp

  1. execv,需要输入绝对路径
/*execv_ls-l.c*/
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>int main(int argc, char * argv[]){//argv array for: /bin/ls -lchar * ls_args[] = { "/bin/ls" , "-l", NULL};//                                  ^//       all argv arrays must be ___| //       NULL terminated       //execute the programexecv(   ls_args[0],     ls_args);//           ^              ^//           |              |// Name of program        argv array// is ls_args[0]          for ls_args//only get here on errorperror("execv");return 2;
}
  1. execvp,可以加载$path,所以不需要输入全路径
aviv@saddleback: demo $ cat execvp_ls-l.c
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>int main(int argc, char * argv[]){//argv array for: ls -lchar * ls_args[] = { "ls" , "-l", NULL};//                    ^ //  use the name ls//  rather than the//  path to /bin/lsexecvp(   ls_args[0],     ls_args);//only get here on errorperror("execv");return 2;
}

1.2 The argv[] argument to execv and execvp

char * ls_args[] = { "ls" , "-l", NULL};.-----.
ls_args ->  |  .--+--> "/bin/ls"|-----||  .--+--> "-l"|-----||  .--+--> NULL'-----'

2 Creating a new Process

1.exec是在当前进程里执行,fork是新创建了一个进程

2.1 fork()

1.所有的进程都产生于其他进程,最初的进程是init
2.fork方法返回两次,一次是父进程(返回子进程的id),一次是子进程(返回0)
3.子进程时父进程的镜像

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>int main(){pid_t c_pid;c_pid = fork(); //duplicate                                                                                                                                                if( c_pid == 0 ){//child: The return of fork() is zero                                                                                                                                    printf("Child: I'm the child: %d\n", c_pid);}else if (c_pid > 0){//parent: The return of fork() is the process of id of the child                                                                                                         printf("Parent: I'm the parent: %d\n", c_pid);}else{//error: The return of fork() is negative                                                                                                                                perror("fork failed");_exit(2); //exit failure, hard                                                                                                                                           }return 0; //success
}

2.2 Process identifiers or pid

1.每一个进程都有一个id,叫pid
2.pid占用2个字节,类型是pid_t
3.父进程fork返回子进程的id,子进程fork返回0,出错fork返回-1
4.pstree 查看进程的继承关系

2.3 Retrieving Process Identifiers: getpid() and getppid()

  1. fork机制,父知道子pid,子不知道父pid,使用getppid
  2. 父不知道自己的id,使用getpid
  3. shell执行进程,就是fork
  4. shell是进程的父进程,使用echo $$来查看当前shell的进程
//retrieve the current process id
pid_t getpid(void);//retrieve the parent's process id
pid_t getppid(void);
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>int main(){pid_t pid, ppid;//get the process'es pidpid = getpid();//get the parrent of this process'es pidppid = getppid();printf("My pid is: %d\n",pid);printf("My parent's pid is %d\n", ppid);return 0;
}

3 Waiting on a child with wait()

  1. wait()父进程调用,返回子进程状态的变化
  2. 当父进程调用wait()将会阻塞,直到子进程状态发生变化
  3. wait返回子子进程的pid,如果没有子进程,则返回-1
  4. wait需要整型指针参数,记录子进程退出的状态
/*get_exitstatus.c*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>#include <sys/types.h>
#include <sys/wait.h>int main(){pid_t c_pid, pid;int status;c_pid = fork(); //duplicateif( c_pid == 0 ){//childpid = getpid();printf("Child: %d: I'm the child\n", pid, c_pid);printf("Child: sleeping for 2-seconds, then exiting with status 12\n");//sleep for 2 secondssleep(2);//exit with statys 12exit(12);}else if (c_pid > 0){//parent//waiting for child to terminatepid = wait(&status);if ( WIFEXITED(status) ){printf("Parent: Child exited with status: %d\n", WEXITSTATUS(status));}}else{//error: The return of fork() is negativeperror("fork failed");_exit(2); //exit failure, hard}return 0; //success
}

4 Fork/Exec/Wait Cycle

14 exec/fork/wait cycles for Process Management相关推荐

  1. Process Management [LKD 03]

    今天开始读Linux Kernel Development这本书. 看了这本书的目录,覆盖比较广泛,和LDD相比多了一些东西,毕竟LDD侧重于device driver,而LKD侧重于kernel本身 ...

  2. 业务流程管理(Business Process Management)

    1.业务流程管理 编辑本义项 BPM 求助编辑百科名片 BPM是Business Process Management的英文字母缩写,大致有五个意思,即业务流程管理,是一套达成企业各种业务环节整合的全 ...

  3. python 多进程 调用模块内函数_进程创建fork()和multiprocessing模块Process类

    一.进程VS程序 一个人穿上军装去打仗就是战士,战士需要武器.战场等资源.一个人去种田就是农民,农民需要土地.锄头等资源. 编写完毕的代码,安静的呆在磁盘上,称之为程序.代码要运行就得加载到内存,正在 ...

  4. 线程基础:多任务处理(14)——Fork/Join框架(要点1)

    =============接上文 (<线程基础:多任务处理(13)--Fork/Join框架(解决排序问题)>) 1.工作过程概要 在开篇前,首先回答一个上篇文章中的一个问题.在上篇文章给 ...

  5. 第14周翻译Stairway to Transaction Log Management in SQL Server, Level 5: Managing the Log in Full Rec...

    来源:Stairway to Transaction Log Management in SQL Server, Level 5: Managing the Log in Full Recovery ...

  6. Process management of windows

    igfxem.exe进程是正常的进程.是intel家的核显驱动类的进程.核显即"核芯显卡",是指GPU部分它是与CPU建立在同一内核芯片上,两者完全融合的芯片."核芯显卡 ...

  7. posix多线程有感--线程高级编程(线程和fork,exec)

    当多线程进程调用fork创建子进程时,Pthreads指定只有那个调用fork的线程在子进程内存在(表示子进程中只有调用线程这个线程).尽管当从fork调用返回时,只有调用线程在子进程中存在,所有其他 ...

  8. 【Linux系统编程学习】Linux进程控制原语(fork、exec函数族、wait)

    此为牛客Linux C++和黑马Linux系统编程课程笔记. 1. fork函数 1.1 fork创建单个子进程 #include<unistd.h> pid_t fork(void); ...

  9. c++中的fork函数_linux c语言 fork() 和 exec 函数的简介和用法

    linux c语言 fork() 和 exec 函数的简介和用法 假如我们在编写1个c程序时想调用1个shell脚本或者执行1段 bash shell命令, 应该如何实现呢? 其实在<stdli ...

最新文章

  1. MYSQL 数据库迁移 ***
  2. oracle 新建路径,Linux环境安装Oracle11g(三)——用户、路径创建及配置环境变量
  3. html传输php连接mysql数据库_解析HTML、JS与PHP之间的数据传输
  4. 解决ionic切换路由后,不继承tab路由,没有返回按钮问题
  5. 【数字逻辑设计】核心知识归纳总结
  6. 微信小程序scroll-view去掉滚动条
  7. 递归大总结之台阶问题
  8. 豆瓣评分8.0以上数据分析R、MySQL、Python等书籍,45本包邮送到家
  9. vscode插件(安装路径),压缩js文件解压,方便查看
  10. 2020 11月8周记
  11. 字符串与vector和list
  12. java支付宝提现_关于Java调用微信、支付宝的支付、提现
  13. 国产手机荣获国际大奖,在美国被排队购买,不是华为你可能没用过
  14. 数据分析之Excel的基本功能(下)
  15. mysql rollup函数_Mysql,Oracle使用rollup函数完成行列统计
  16. android7.1 jack-admin 报错
  17. 2008北京奥运会足球赛程(男足)
  18. ARM芯片、内核、架构、指令集的联系与区别
  19. 五、angularjs在进入界面前加载数据
  20. 【python3 高级编程】.一文搞懂多线程

热门文章

  1. 深入理解C语言系列之C语言语法陷阱(考题常设置的陷阱点、必须避免的错误和缺陷类型)
  2. android emmc生产日期,碎碎念android eMMC【转】
  3. linux网卡绑定和漂移,LINUX修改、增加IP的方法,一张网卡绑定多个IP/漂移IP【转】...
  4. Python语言常用的49个基本概念及含义
  5. Linux 线程控制
  6. 波士顿学院计算机科学专业,波士顿学院专业有哪些?
  7. xpath修复html错误,【python】xpath解析html文件报错:lxml.etree.XPathEvalError: Invalid expression...
  8. 双目摄像头 三维坐标 python_uNetXST:将多个车载摄像头转换为鸟瞰图语义分割图像...
  9. gdb当前哪一行_GDB原理之ptrace实现原理
  10. python 小甲鱼——面向对象笔记