一周总结(六)

前言

本次总结是关于操作系统的进程的基础概念

进程概念

  1. A process is a program in execution (an active entity, i.e. it is a running program )
  2. Basic unit of work on a computer, a job, a task.
  3. A container of instructions with some resources:– e.g. CPU time (CPU carries out the instructions), memory, files, I/O devices to accomplish its task
  4. jobs(作业)=user program(用户程序)=tasks(任务)=process(进程)
  5. 内存中的进程
    • 代码段(text sectioin)
    • Program counter(PC)
    • Registers
    • Data section(global data)
    • Stack
    • Heap 动态分配内存
    • OS资源 (打开的文件,sockets,安全证书)
  6. 进程状态
    • New(新) 进程正在创建
    • Running(运行) 指令正在执行
    • Ready(就绪) 进程正在等待被分配到cpu
    • Waiting(等待、blocked阻塞) 进程正在等待某个事件发生
    • Terminated(终止)进程已经完成了运行
  7. 进程转换

    • 基本状态转换

      1. 就绪->运行: 当处理机空闲时,调度程序必将处理机分配给一个就绪的进程
      2. 运行->等待: 当运行态的进程需要等待某一事件(如等待I/O完成)发生后,才能继续运行,则该进程放弃处理机,进入等待态
      3. 等待->就绪: 当等待态的进程等待的事件发生,则转为就绪态
      4. 运行->就绪: 当运行态的进程由于时间片用完,则被调度为就绪态
    • 等待->运行,就绪->等待,一般不可能发生

new ready running waiting terminated
new admitted
ready scheduler dispatch
running interrupt I/O or event wait exit
waiting I/O or event completion
terminated
  1. 进程状态与处理机
    • 任意时刻,最多只有n个进程处于运行态。(n为处理机个数)
    • 就绪态按照一定算法排成就绪队列
    • 等待态按照一定算法排除等待队列
  2. 进程与程序的区别
    • 进程是动态的,程序是静态的
    • 进程是暂时的,程序是永久的
    • 进程包括程序、数据、PCB
    • 一个程序可以对应多个进程(通过多次执行),一个进程可以包括多个程序(d多次调用)
  3. Process Control Block(PCB 进程控制块)
    • 进程状态,PC,寄存器,调度信息,内存信息,资源信息,文件管理,IO状态信息

进程调度

  1. 调度队列
    • Job queue 全部进程
    • Ready queue 就绪进程(驻留在内存中)
    • Device queue 设备队列(等待IO的设备)
  2. 调度分类(按调度频率)
    • Long-term scheduler(job scheduler)长程调度、作业调度,时间片为秒或分钟如多道程序。现代操作系统很少使用
    • Short-term scheduler(CPU scheduler)短程(CPU)调度,时间片为毫秒
    • Medium-term scheduler 中程调度
  3. 调度分类(按密集)
    • I/O-bound (I/O型进程)
    • CPU-bound (CPU型进程),更多时间做computation
  4. Context Switch (上下文切换)
    • CPU在切换进程时需要保存旧进程的状态,并通过上下文获取加载新进程的状态,并加载新进程。
    • 上下文在PCB中
    • 上下文切换是开销,时间依赖于硬件

进程操作

  1. 父进程创建子进程,由此构建进程树
  2. 进程由process identifier(pid)确定
  3. 资源共享
    • 父子共享所有资源
    • 子进程共享父进程的资源子集
    • 父子无共享
  4. 运行
    • 父子同时运行
    • 父等待子结束
  5. 地址空间(Address space)
    • 子复制父
    • 子有另一个程序
  6. 进程创建(creation)
    • linux fork: 返回值为pid,父进程中pid为子进程的进程号,子进程中为0;两个进程具有完全一样的用户级上下文
    • exec system call 在fork之后调用来用新程序替换进程内存空间
  7. 进程终止(Termination)
    • 终止事件
      • 正常结束
      • 异常结束
      • 外界干预
    • Android 进程等级,Android终止最低级的进程来释放资源
      1. Foreground
      2. Visible
      3. Service
      4. Background
      5. Empty

进程间通信

  1. Independent process、 Cooperating process
  2. 进程通信的优点
    • 信息共享
    • 计算加速
    • 模块化
    • 便利性
  3. Interprocess Communication(IPC)进程间通信

    IPC使得进程间通信和同步不需要使用相同的地址空间
    • Shared memory: 进程将消息发送到一个共同的地址内存中
    • Message passing:发送到内核的消息队列中
  4. IPC通信类型
    • 直接通信
    • 间接通信
  5. IPC通信机制
    • 信号(signal)
    • 共享存储区(shared memory)
    • 管道(pipe)
    • 消息(message)
    • 套接字(socket)
    • 文件锁(file lock)
    • 邮件槽
    • 剪贴板(clipboard)
  6. IPC-Shared Memory
    • 用户进程控制而非操作系统
  7. Produce-Consumer Problem(生产者-消费者问题)

    • unbounded-buffer(无限缓冲区)
    • bounded-buffer(有限缓冲区)
    • 预定义变量

      1
      2
      3
      4
      5
      6
      7
      #define BUFFER_SIZE 10
      typedef struct {
      ...
      } item;
      item buffer[BUFFER_SIZE];
      int in = 0;
      int out = 0;

      只能使用BUFFER_SIZE-1个item

    • 生产者进程

      1
      2
      3
      4
      5
      6
      7
      8
      item nextProduced;
      where (1){
      produce an item in nextProduced;
      while (((in+1) % BUFFER_SIZE)==out)
      ;// do nothing
      buffer[in] = nextProduced;
      in = (in + 1) % BUFFER_SIZE;
      }
    • 消费者进程

      1
      2
      3
      4
      5
      6
      7
      8
      item nextConsumed;
      while (1){
      while (in == out)
      ;//do nothing
      nextConsumed = buffer[out];
      out = (out + 1) % BUFFER_SIZE;
      consume the item in nextConsumed;
      }
  8. IPC-Message Passing

    • send
    • receive
    • message size is either fixed or variable
    • 通信过程
      1. 建立连接(communication link)
        • 物理方法:shared memory; Hardware bus; Network
        • 化学方法:直接/间接 同步/异步 automatic/explicit buffering
      2. 交换信息(send/receive)
    • 直接通信
      • send(P,message)
      • receive(Q,message)
      • communication link
        • 自动建立
        • 一条link只连接两个通信进程
        • 每一对进程中只存在一条link
        • link一般是双向的,但也可能是单向的
    • 间接通信 (mailboxex or ports)
      • 每一个邮件槽有一个id,只有共享邮件槽的进程才可以通信
      • communication link
        • 当仅当进程共享邮件槽时建立
        • 一条link可以连接若干条连接
        • 每一对进程可以共享若干links
        • link可以单向或多向
      • 操作流程:创建mailbox(port);发送和接收消息;销毁mailbox
    • Synchronization
      • Blocking(阻塞 synchronous)
        • Blocking send:sender is blocked until message is received
        • Blockint receive: receiver is blocked until message is available
      • Non-blockint(非阻塞 asynchronous)
        • Non-blocking send: sender send message and continue
        • Non-blocking receive: receiver receive message or null

Client-Server System Communication

  1. sockets(详见网络)
  2. Remote Procedure Calls(远过程调用)