操作系统第四章笔记---进程

本文内容整理自西安交通大学软件学院田丽华老师的课件,仅供学习使用,请勿转载

操作系统系列笔记汇总:操作系统笔记及思维导图汇总附复习建议_Qlz的博客-CSDN博客

文章目录

Process Concept

Allow multiple programs to loaded into memory and to be executed concurrently.
现在的操作系统多为并发执行,具有许多新的特征。引入并发执行的目的是为了提高资源利用率

Introduce

  • The basic characteristics of OS is concurrence and sharing, several program sharing the resource of computer and running crossways.
    • resource competition
    • cooperate
    • communication

Sequential environment (顺序环境)

Only one program executed at a time, and this program had complete control of system and had access to all the system’s resources.

image-20210929235359707

特征

  • 顺序性:按照程序结构所指定的次序(可能有分支或循环)
  • 封闭性:独占全部资源,计算机的状态只由于该程序的控制逻辑所决定
  • 可再现性:初始条件相同则结果相同。如:可通过空指令控制时间关系。

Concurrent environment (并发环境)

并发环境: 一定时间内,物理机器上有两个或两个以上的程序同处于开始运行但尚未结束的状态,并且次序不是事先确定的。

image-20210929235510760

特征

  • 间断(异步)性:“走走停停”,一个程序可能走到中途停下来,失去原有的时序关系;
  • 失去封闭性:共享资源,受其他程序的控制逻辑的影响。如:一个程序写到存储器中的数据可能被另一个程序修改,失去原有的不变特征。
  • 失去可再现性:失去封闭性 ->失去可再现性;外界环境在程序的两次执行期间发生变化,失去原有的可重复特征。

多道程序设计对OS的要求

  • 如何描述并发程序的执行:引入进程及其状态
  • 如何实现并发程序运行:进程控制与调度
  • 如何处理资源的竞争与程序间的合作:并发控制与通信
  • 如何解决死锁:死锁策略

Process

  • 为了描述程序在并发执行时对系统资源的共享,我们需要一个描述程序执行时动态特征的概念,这就是进程
  • Process – a program in execution; 进程 - 在执行中的程序;
  • 一个具有一定独立功能的程序在一个数据集合上的一次动态执行过程。
  • A program is a passive entity; a process is a active entity.
  • 引入多进程,提高了对硬件资源的利用率,但又带来额外的空间和时间开销,增加了OS 的复杂性;
  • A process includes: 一个进程包括
    • Program code
    • program counter 程序计数器
    • stack 栈
    • data section 数据部分

进程与程序

  • 进程是动态的,程序是静态的:程序是有序代码的集合;进程是程序的执行。通常进程不可在计算机之间迁移;而程序通常对应着文件、静态和可以复制。
  • 进程是暂时的,程序是永久的:进程是一个状态变化的过程,程序可长久保存。
  • 进程与程序的组成不同:进程的组成包括程序、数据和进程控制块(即进程状态信息)。
  • 进程与程序的对应关系:通过多次执行,一个程序可对应多个进程;通过调用关系,一个进程可包括多个程序。

进程的特征

  • 结构特征:进程实体=程序段+相关的数据段+PCB。
  • 动态性:进程的实质是进程实体的一次执行过程,因 此动态性是进程的最基本的特征。
  • 并发性: 多个进程实体同存在于内存中,且能在一段时间内同时运行。是最重要的特征。
  • 独立性:指进程实体是一个能独立运行、独立分配资源和独立接受调度的基本单位。
  • 异步性: 进程按各自独立的、不可预知的速度向前推进。

进程的类型

  • 在系统中同时有多个进程存在,但归纳起来有两大类:
    • 系统进程
      • 系统进程起着资源管理和控制的作用。
      • 或者:执行操作系统核心代码的进程。
    • 用户进程
      • 执行用户程序的进程。

系统进程与用户进程的区别

  • 资源的使用:系统进程被分配一个初始的资源集合,这些资源可以为它独占,也能以最高优先权的资格使用。用户进程通过系统服务请求的手段竞争使用系统资源;
  • I/O操作:用户进程不能直接做I/O操作,而系统进程可以做显式的、直接的I/O操作。
  • CPU工作状态:系统进程在管态下活动,而用户进程则在用户态(目态)下活动。

进程描述

  • 进程存在意味着:
    • 处于某种状态(运行、就绪、等待);
    • 进程控制块PCB(数据结构);
    • 进程的执行程序(一个可执行文件);
    • 进程位于某个队列(就绪、等待某事件队列);
    • 占用某些系统资源(内存,打开某些文件、处理机、外设)

进程状态

  • As a process executes, it changes state
    进程执行时,改变状态
    • new: The process is being created.新建:在创建进程
    • ready: The process is waiting to be assigned to a processor.就绪:进程等待分配处理器
    • running: Instructions are being executed.运行:指令在执行
    • waiting: The process is waiting for some event to occur.等待:进程等待某些事件发生
    • terminated: The process has finished execution.终止:进程执行完毕
  • 只有三级时为:ready,running,waiting

interrupt可能为时间片到了

image-20210930000401080

进程状态变迁

new
  • OS 已完成为创建一进程所必要的工作
    • 已构造了进程标识符;
    • 已创建了管理进程所需的表格;
  • 还没有允许执行该进程
    • 因为资源有限.
ready

存在于处理机调度队列中的那些进程,它们已经准备就绪,一旦得到CPU,就立即可以运行(有多个进程处于此状态

running

当进程由调度/分派程序分派后,得到CPU控制权,它的程序正在运行(在系统中,总只有一个进程处于此状态

Waiting
  • 进程正在等待某个事件的发生(如等待I/O的完成),而暂停执行
  • 即使给它CPU时间,它也无法执行
exit
  • 终止后进程移入该状态;
  • 不再有执行资格;
  • 表格和其它信息暂时由辅助程序保留
    • 例子: 为处理用户帐单而累计资源使用情况的帐务程序;
  • 当数据不再需要后,进程(和它的表格)被删除;

挂起状态

调节负载,对换,父进程,操作系统,终端用户(不知道啥意思)

这部分不考(或许?)

挂起状态的引入
  • 终端用户的需要
    • 当终端用户在自己的程序运行期间,发现有可疑问题时(debug),往往希望暂时使自己的进程静止下来,以便研究其执行情况或对程序进行修改。
      • 如果进程处于执行状态,则暂停执行;
      • 如果进程处于就绪状态,则暂时不接受调度,
  • 父进程的需求
    • 父进程常常希望考察和修改子进程,或者需协调各子进程间的活动,要挂起自己的子进程
  • 操作系统的需要
    • 操作系统有时需要挂起某些进程,检查运行中资源的使用情况及进行记帐,以便改善系统的运行性能。
  • 对换的需要(???)
    • 为了缓和内存紧张的情况,将内存中处于阻塞状态的进程换至外存上
  • 负荷调节的需要
    • 当实时系统中的工作负荷较重,可能影响到对实时任务的控制时,可由系统把一些不重要或不紧迫的进程挂起,以保证系统仍然能正常运行
七状态进程模型
image-20210930001824843
新状态转换
  • 活动就绪 -->静止就绪
    • 当进程处于未被挂起的就绪状态时,称此为活动就绪状态(Readya)
    • 当用挂起原语Suspend将该进程挂起后,该进程便转变为静止就绪状态(Readys), 处在Readys状态的进程,不再被调度执行
  • 活动阻塞 -->静止阻塞
    • 当进程处于未被挂起的阻塞状态时,称它处在活动阻塞状态(Blockeda)。
    • 当Suspend原语将它挂起后,进程变为静止阻塞状态(Blockeds)。
    • 处于该状态的进程,在其所期待的事件出现以后,将从静止阻塞变为静止就绪.
  • 静止就绪 -->活动就绪
    • 处于Readys状态的进程,若用激活原语Activate激活后,该进程将转变为Readya状态。
  • 静止阻塞–>活动阻塞
    • 处于Blockeds状态的进程,若用激活原语Activate激活后,进程将转变为Blockeda状态

激活原语,挂起原语是个啥

Process Control Block (PCB)

  • PCB (Process Control Block):一个专门的数据结构,系统用它来记录进程的外部特征,描述进程的运动变化过程
  • PCB是进程管理和控制的最重要的数据结构,在创建进程时,建立PCB,并伴随进程运行的全过程,直到进程撤消而撤消。
  • PCB是系统感知进程存在的唯一标志,进程与PCB是一一对应的
  • PCB经常被系统访问,如,调度程序、资源分配程序、中断处理程序等,所以PCB应常驻内存。

同进程有关的信息

  • Process state 进程状态
  • Program counter 程序计数器
  • CPU registers CPU寄存器
  • CPU scheduling information CPU调度信息
  • Memory-management information 内存管理信息
  • Accounting information 计账信息
  • I/O status information I/O状态信息

image-20210930002539710image-20210930002548853

  • 进程标识符 name
    • 每个进程都必须有一个唯一的标识符,可以是字符串,也可以是一个数字。
    • UNIX系统中就是一个整型数。在进程创建时由系统赋予。
  • 进程当前状态 status
    • 说明进程当前所处的状态。
  • 当前队列指针 next
    • 登记与本进程处于同一队列的下一个进程的PCB的地址

image-20210930002705647

  • 执行程序开始地址 start-addr
  • 进程优先级 priority
    • 进程的优先级反映进程的紧迫程度,通常由用户指定和系统设置。
    • UNIX系统采用用户设置和系统计算相结合的方式确定进程的优先级。
  • CPU现场保护区 cpu status
    • 当进程因某种原因不能继续占用CPU时(如:等待打印机),释放CPU,这时就要将CPU的各种状态信息保护起来,为将来再次得到处理机恢复CPU的各种状态,继续运行。
  • 通信信息 communication information
    • 是指某个进程在运行的过程中要与其它进程进行通信,该区记录有关进程通信方面的信息。
  • 家族联系 process family
    • 进程可创建自已的子进程,子进程还可以创建,一个进程往往处在一个家族之中,就需要记录进程在家族中位置的信息。
  • 占有资源清单 own-resource
    • 进程占用系统资源的情况,不同系统的处理差别很大,UNIX系统中就没有此项。

PCB的组织方式

  • PCB表:系统把PCB组织在一起,并放在内存的固定区域,就构成了PCB表;
  • PCB表的个数决定了系统中最多可同时存在的进程个数,称为系统的并发度.
  • PCB表的组织方式
    • 链接方式
    • 索引方式
image-20210930002855410 image-20210930002910760

Process Scheduling

Introduction

  • Job queue – set of all processes in the system.
    作业队列 - 在系统中的所有进程的集合(位于外存中)
  • Ready queue – set of all processes residing in main memory,ready and waiting to execute.
    就绪队列 - 在主内存中的,就绪并等待执行的所有进程的集合
  • Device queues – set of processes waiting for an I/O device.
    设备队列 - 等待某一I/O设备的进程队列
  • Process migration between the various queues.
    在各种队列之间进程的迁移

Representation of Process Scheduling (进程调度的描述)

image-20211003182715031

Schedulers

  • Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue.
    长程调度(或作业调度)- 选择可以进入就绪队列的进程

    • 外存中的作业队列进入内存中的就绪队列
    • Controls the degree of multiprogramming 控制了多道程序的“道”,即cpu就绪队列中可同时存在的进程的最大值
    • Long-term scheduler is invoked very infrequently (seconds, minutes), (may be slow). 长程调度不快
  • Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates CPU.
    短程调度(或CPU调度)- 选择可被下一个执行并分配CPU的进程

    • 就绪队列到运行
    • Short-term scheduler is invoked very frequently (milliseconds), (must be fast). 短程调度切换频率高
  • Processes can be described as either: 进程可以用下列方式描述:

    • I/O-bound process – spends more time doing I/O than computations, many short CPU bursts.
      I/O型进程 - 花费I/O 时间多于计算,许多短CPU处理

    • CPU-bound process – spends more time doing computations; few very long CPU bursts.

      CPU 型进程 - 花费更多时间于计算,许多长CPU处理

  • Long-term scheduler controls the process mix of I/O-bound process and CPU-bound process

Medium Term Scheduling (中程调度)

为了缓和内存紧张的情况,将内存中处于阻塞状态的进程换至外存上(挂起),降低多道程序的度。当这些进程重新具备运行条件时,再从外存上调入内存。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W0ceEEMj-1641359605778)(04_Process.assets/image-20211003183358178.png)]

Context Switch (上下文切换)

  • When CPU switches to another process, the system must save the state of the old process and load the saved state for the new process.
    当CPU切换至另一个进程时,系统必须保存旧进程状态并为新进程调入所保留的状态
  • Context-switch time is overhead; the system does no useful work while switching.
    上下文切换的时间开销较重;在切换时,系统没有做有用的工作
  • Time dependent on hardware support.
    时间取决于硬件的支持

Operation on Processes

  • 进程是有生命周期的:产生、运行、暂停、终止。对进程的这些操作叫进程控制。
  • 进程控制的职责是对系统中进程实施有效的管理,它是CPU管理的一部分(还有进程同步、通信和调度)。
    • cpu管理: 进程控制,进程同步,进程通信,进程调度
  • 当系统允许多进程并发执行时,为了实现共享、协调并发进程的关系,处理机管理必须对进程实行有效的管理。

Process Creation

何时创建
  • 作业调度:批处理系统中,作业调度程序调度到某个作业以后,就把这个作业装入内存,并分配必要的资源,创建进程,插入就绪队列。
  • 用户登录:在分时系统中,用户在终端键入登录命令后,若是合法用户,系统建立一个进程,并插入就绪队列。
  • 提供服务:用户向系统提出请求后,系统专门建立一个进程为用户服务。(如打印请求)
  • 应用请求:应用进程的需要,由它自己创建一个新进程,使新进程以并发运行方式完成特定任务。(输入数据并将处理结果输出到表格上)
父子进程
  • Parent process creates children processes, which, in turn create other processes, forming a tree of processes.
    父进程创建子进程,如此轮流创建进程下去,构成一个进程树
  • Resource sharing 资源共享
    • Parent and children share all resources.
      父进程子进程共享所有的资源
    • Children share subset of parent’s resources.
      子进程共享父进程资源的子集
    • Parent and child share no resources.
      父进程和子进程无资源共享
  • Execution 执行
    • Parent and children execute concurrently.
      父进程和子进程并发执行
    • Parent waits until children terminate.
      父进程等待,直到子进程终止
  • Address space 地址空间
    • Child is duplicate of parent. 子女复制双亲
    • Child has a program loaded into it. 子女重新加载一个新程序
进程创建的过程
  1. 申请空白的PCB:为新进程分配唯一的数字标识符,并从PCB集合中索取一个空白的PCB
  2. 为新建立的进程分配资源:为新进程的程序和数据,以及用户栈分配必要的内存空间。
  3. 初始化程序控制块:初始化标识符信息。将系统中分配的标识符、父进程标识符填入新PCB中。初始化处理机状态信息。是程序计数器指向程序的入口地址,栈指针指向栈顶。初始化处理机控制信息。将进程的状态设置为就绪状态或静止就绪状态。
  4. 将新进程插入就绪队列
image-20211003184201527

UNIX examples

  • 在UNIX系统中用户键入一个命令(如date, ps,ls),shell就创建一个进程。
  • fork : system call creates new process
    系统调用创建新进程
  • execlp : system call used after a fork to replace the process’ memory space with a new program.
    在fork之后采用execlp系统调用用一个新程序替代进程的内存空间
#include<stdio.h>
// pid=fork();
// 从系统调用fork返回时,CPU在父进程中时,pid值为所创建子进程的进程号(>0),若在子进程中时,pid的值为零。

Void main(int argc, char *argv[])
{ 
    int pid;
    pid = fork(); /*fork another process*/
    if (pid < 0) {/* error occurred */
        fprintf(stderr, “Fork Failed”); 
        exit(-1); }
    else if (pid == 0) { /* child process */
        execlp(/bin/ls”,”ls”,NULL);
     }  else {   /* parent process */   
        wait(NULL);
        printf(“Child Complete”);
        exit(0);
     }
} 

Process Termination

  • Process executes last statement and asks the operating system to decide it (exit).进程执行最后一项并询问操作系统作出决定(退出)
    • Output data from child to parent (via wait).
      从子进程向父进程输出数据(通过等待)
    • Process’ resources are deallocated by operating system. 操作系统收回进程的资源
  • Parent may terminate execution of children processes (abort).
    父进程可中止子进程的执行(终止)
    • Child has exceeded allocated resources.子进程超量分配资源
    • Task assigned to child is no longer required.
      赋予子进程的任务不再需要
    • Parent is exiting. 父进程终止
      • Operating system does not allow child to continue if its parent terminates. 若父进程终止,不允许子进程继续
      • Cascading termination. 级联终止
  • UNIX系统中是通过exit()终止进程,释放占用的所有资源:
    • 释放内外存空间
    • 关闭所有打开文件
    • 释放当前目录
    • 释放共享内存段和各种锁lock
  • 父进程通过系统调用wait()等待子进程的终止
  • 当父进程终止后,其子进程会以init为其父进程

image-20211003184636060

Process Blocking

  • 引起进程阻塞和唤醒的事件:
    • 一个处在运行状态的进程,因等待某个事件的发生(如等待打印机、同步事件等)而不能继续运行时,将调用阻塞原语,把进程置为阻塞状态,并转进程调度程序(等于让出处理机)。
  • 调用进程阻塞操作是在进程处于运行状态下执行的。它的执行将引起等待某事件的队列的改变.
image-20211003184803297

Process wake-up

  • 当进程所等待的事件发生时,该进程将被唤醒(由进程唤醒操作完成)。
  • 唤醒一个进程有两种方法:
    • 由系统进程唤醒。
    • 由事件发生进程唤醒。
image-20211003184843947

UNIX相关系统调用介绍

  1. fork(): 创建一个子进程.用它创建的子进程是fork调用者进程(即父进程)的复制品,即进程映像.除了进程标识数以及与进程特性有关的一些参数外,其他都与父进程相同,与父进程共享文本段和打开的文件,并都受进程调度程序的调度.如果创建进程失败,则fork()返回值为-1,若创建成功,则从父进程返回值是子进程号,从子进程返回的值是0。

  2. exec(): 装入并执行相应文件.
    因为FORK会将调用进程的所有内容原封不动地拷贝到新创建的子进程中去,而如果之后马上调用exec,这些拷贝的东西又会马上抹掉,非常不划算.于是设计了一种叫作”写时拷贝”的技术,使得fork结束后并不马上复制父进程的内容,而是到了真正要用的时候才复制.

  3. wait():父亲进程处于阻塞状态,等待子进程执行完成终止后继续工作,其返回值为所等待子进程的进程号。

  4. exit():子进程自我终止,释放所占资源,通知父进程可以删除自己,此时它的状态变为P_state= SZOMB,即僵死状态.如果调用进程在执行exit时其父进程正在等待它的中止,则父进程可立即得到它返回的整数。

  5. getpid():获得进程号。

  6. lockf(files,founction,size):用于锁定文件的某些段或整个文件。

    // 本函数适用的头文件为:
    #include<unistd.h>, 
    // 参数定义:
        int lockf(files,founction,size)
        int files,founction;
        long size;
    // 其中,files 是文件描述符,founction表示锁定还是解锁,1表示锁定,0表示解锁;size是锁定或解锁的字节数,若为0则表示从文件的当前位置到文件尾。
    
  7. kill(pid,sig):一个进程通过此系统调用向同一用户的其他进程pid发送一中断信号。

  8. signal(sig,founction):捕捉中断信号sig后执行founction规定的操作。

    //头文件为:
    #include <signal.h>
    //参数定义:
        signal(sig,founction)
        int sig;
        void (*func) ();    // 其中sig共有19个
    
  9. pipe(fd);
    int fd[2];
    其中fd[1]是写端,向管道中写入,fd[0]是读端,从管道中读出。

  10. 暂停一段时间sleep;
    调用sleep将在指定的时间seconds内挂起本进程。
    其调用格式为:“unsigned sleep(unsigned seconds);”;返回值为实际的挂起时间。

  11. 暂停并等待信号pause;
    调用pause挂起本进程以等待信号,接收到信号后恢复执行。当接收到中止进程信号时,该调用不再返回。
    其调用格式为“int pause(void);”

// 例子
Main()
{  
    int i;
    while((i=fork())==-1);/*若创建失败,则反复创建*/
    printf(“i=%d\n”,I);/*打印进程标识符*/
    if (i) printf(“it is a parent process!\n”);                
    /*i>0,在父进程中*/
    else printf(“it is a child process!\n”); 
    /*i=0,在子进程中*/
}

Cooperating Processes

  • Independent process cannot affect or be affected by the execution of another process.
    独立进程不会影响另一个进程的执行或被另一个进程执行影响
  • Cooperating process can affect or be affected by the execution of another process
    协同进程可能影响另一个进程的执行或被另一个进程执行影响
    • 共享内存通信一般都是协同进程
  • Advantages of process cooperation
    进程协同的优点
    • Information sharing 信息共享
    • Computation speed-up 加速运算
    • Modularity 模块化
    • Convenience 方便

Interprocess Communication (IPC)

Two models of IPC

  • Message passing (信息传递)
  • Shared memory (共享内存)

image-20211003185518118

Shared Memory systems

  • Interprocess communication using Shared memory requires communicating processes to establish a shared memory
    使用共享存储模型的进程间通信要建立共享存储区
  • Processes can exchange information by reading or writing data to the shared areas.
    进程通过读写共享存储区来交换信息
  • The form of the exchanged data and location are determined by the communicating processes and are not under the OS’s control
    通信进程来确定交换的数据和位置不受操作系统的控制

Producer-Consumer Problem

  • Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process.
    生产者进程生产供消费者进程消费的信息
    • unbounded-buffer places no practical limit on the size of the buffer.
      无界缓冲没有对缓冲区大小的限制
    • bounded-buffer assumes that there is a fixed buffer size.
      有界缓冲对缓冲区大小作了限定
// Shared data
    var n;
    type item =;
    var buffer. array [0..n–1] of item;
    in, out: 0..n–1;
// Producer process 
    repeat
    …
    produce an item in nextp
    …
    while in+1 mod n = =out do no-op;
    buffer [in] :=nextp;
    in :=in+1 mod n;
    until false;
//Consumer process  (Solution is correct, but can only fill up n–1 buffer.)
    repeat
    while in == out do no-op;
    nextc := buffer [out];
    out := out+1 mod n;
    …
    consume the item in nextc
    …
    until false;

Message passing system

  • Mechanism(机制) for processes to communicate and to synchronize their actions.用于进程通信的机制,同步其间的活动
  • Message system – processes communicate with each other without resorting to shared variables. 消息系统 - 进程间通信无须再利用共享变量
  • IPC facility provides two operations IPC提供两个操作 :
    • send(message) – message size fixed or variable 发送 - 固定或可变大小消息
    • **receive(message)**接受
  • If P and Q wish to communicate, they need to 若P与Q要通信,需要:
    • establish a communication link between them 建立通信连接
    • exchange messages via send/receive 通过send/receive交换消息
  • Implementation of communication link 通信连接的实现
    • physical (e.g., shared memory, hardware bus)物理的(如,共享存储,硬件总线)
    • logical (e.g., logical properties)逻辑的(如,逻辑特性)

Direct Communication

  • Processes must name each other explicitly:进程必须显式的命名

    • send (P, message) – send a message to process P向进程P发消息
    • receive(Q, message) – receive a message from process Q从进程Q收消息
  • Properties of communication link

    • Links are established automatically. 连接自动建立
    • A link is associated with exactly one pair of communicating processes.
      连接精确的与一对在通信的进程相关
    • Between each pair there exists exactly one link.
      在每一对之间就存在一个连接
    • The link may be unidirectional, but is usually bi-directional.
      连接可以无向,但通常是双向的
  • asymmetric communication

  • Sender names the recipient, the recipient is not required to name the sender. 发送时必须指定发给谁,接收时无需指定

    • send (P, message) – send a message to process P向进程P发消息
    • receive(id, message) – receive a message from any process 接收来自任何进程的消息

Indirect Communication

  • Messages are directed and received from mailboxes (also referred to as ports).消息导向至信箱并从信箱接收(被视作端口)

    • Each mailbox has a unique id. 每一个信箱有一个唯一的id
    • Processes can communicate only if they share a mailbox.
      仅当共享一个信箱时进程才能通信
  • Primitives are defined as:

    • send(A, message) – send a message to mailbox A
    • receive(A, message) – receive a message from mailbox A
  • Properties of communication link 通信连接的特性

    • Link established only if processes share a common mailbox

      仅当进程共有一个信箱时连接才能建立

    • A link may be associated with many processes.连接可同多个进程相关

    • Each pair of processes may share several communication links.

      每一对进程可共享多个通信连接

    • Link may be unidirectional or bi-directional.连接可是无向或双向的

  • Operations操作

    • create a new mailbox 创建新的信箱
    • send and receive messages through mailbox 通过信箱发送和接收消息
    • destroy a mailbox 销毁信箱
Problem

P1, P2与P3共享信箱A,P1发消息,P2,P3接收,那么最终谁能收到消息? 这个是不确定的

  • Allow a link to be associated with at most two processes.
    允许一个连接最多同2个进程相关
  • Allow only one process at a time to execute a receive operation.
    只允许一个时刻有一个进程执行接受操作
  • Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.
    允许系统任意选择接收者。发送者被通知谁是接收者。

Synchronization

  • Message passing may be either blocking or non-blocking
  • Blocking(堵塞) is considered synchronous
    • Blocking send has the sender block until the message is received 发送者必须等待信息被接受
    • Blocking receive has the receiver block until a message is available 接收者必须等待到一个信息可以被接受
  • Non-blocking is considered asynchronous
    • Non-blocking send has the sender send the message and continue 发送者只管发
    • Non-blocking receive has the receiver receive a valid message or null 接收者只管收,不管有没有收到消息

Buffering

  • Queue of messages attached to the link; implemented in one of three ways.
    消息队列附加在连接上;采用三个之一的实现方案
    • Zero capacity – 0 messages零容量 - 0 消息
      • Sender must wait for receiver (rendezvous).
        发送者必须等待接收者
    • Bounded capacity – finite length of n messages 有界容量 - n个消息有限长度
      • Sender must wait if link full.
        若连接满了发送者必须等待
    • Unbounded capacity – infinite length 无界容量 - 无限长度
      • Sender never waits.
        发送者从不等待

Examples

Mach

  • Mach communication is message based
  • Even system calls are messages
  • Each task gets two mailboxes at creation- Kernel and Notify
    • Kernal mailbox 内核邮箱
    • Notify mailbox 通知邮箱
    • 内核使用内核邮箱与任务通信,将事件发生的通知发送到通知邮箱
  • Only three system calls needed for message transfer
    • msg_send(), msg_receive(), msg_rpc() (rpc:remote process communication)
  • Mailboxes needed for commuication, created via
    • port_allocate()

Windows XP

Message-passing centric via local procedure call (LPC) facility

  • Only works between processes on the same system
  • Uses ports (like mailboxes) to establish and maintain communication channels
  • Communication works as follows:
    • The client opens a handle to the subsystem’s connection port object客户机打开子系统连接端口对象的句柄
    • The client sends a connection request客户机发送连接请求
    • The server creates two private communication ports and returns the handle to one of them to the client
    • The client and server use the corresponding port handle to send messages or callbacks and to listen for replies
image-20211011000259895

Client-Server Systems

Sockets

  • A socket is defined as an endpoint for communication 套接字被定义为通信的端点
  • Concatenation of IP address and port
  • The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8
  • Communication consists between a pair of sockets
image-20211011000340343

Remote Procedure Calls

  • Remote procedure call (RPC) abstracts procedure calls between processes on networked systems

    远程过程调用(RPC)抽象了网络系统上的进程之间的过程调用

  • Stubs – client-side proxy for the actual procedure on the server 存根是远程服务器上的过程在客户端的代理

  • The client-side stub locates the server and marshalls the parameters客户端的存根定位服务器并编组参数,向服务器发送消息

  • The server-side stub receives this message, unpacks the marshalled parameters, and peforms the procedure on the server

image-20211011000531901

Remote Method Invocation

  • Remote Method Invocation (RMI) is a Java mechanism similar to RPCs
  • RMI allows a Java program on one machine to invoke a method on a remote object
image-20211011000643739
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hydrion-Qlz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值