操作系统笔记 CH3 Process

process concepts

batch system: job; time-sharing system: program/task

Program is passive entity stored on disk (executable file), process is active.

a process includes

  • text section(program code)
  • data section(global variables)
  • heap(动态分配的内存)
  • process stack(临时数据)
    • function parameters, return value
    • return addresses
    • local variables
  • current activity(represented by program counter and registers)

process state

  • new: being created
  • running: instructions are being executed
  • waiting/limiting: waiting for some event
  • ready: waiting to be assigned to a processor
  • terminated/halted: finished execution

一次只有一个进程可以在一个处理器上运行,但多个进程可处于就绪或等待状态。

**PCB(**process control block): process state & program counter & CPU registers(中断时要保存程序计数器和CPU寄存器) & CPU-scheduling information & memory-management information & accounting information & I/O status information

栈是运行时的单位,而堆是存储时的单位。 堆中存的是对象。栈中存的是基本数据类型和堆中对象的引用。

a program becomes a process when an executable file is loaded into memory

两个进程可以和同一个程序相关,虽然text section一样,但data,heap,stack sections不同。

process scheduling

process scheduler selects an available process for program execution on CPU

scheduling queues

job queue作业队列:进程进入系统时,被加入作业队列,该队列包括系统中的所有进程

ready queue就绪队列:驻留在内存的ready或waiting进程

device queue设备队列:等待特点I/O设备的进程列表。每个设备都有自己的设备队列

queueing diagram

schedulers

long-term / job scheduler: select processes from the pool and load them into memory

short-term / CPU scheduler: select processes that are ready and allocates CPU to one of them

短期调度频繁,长期调度(和新进程创建时间间隔有关)控制degree of multiprogramming(内存中的进程数量)

长期调度需仔细选择,一个包含I/O bound process和CPU bound process的组合。全是IO bound, 就绪队列几乎为空,短期调度无事可做;全是CPU bound,IO等待队列几乎为空。

有些系统可能没有长期调度。有些OS如分时系统,引入medium-term scheduler, 将进程从内存(CPU竞争)中移出,降低multiprogramming设计难度。 swapping . The process is swapped out, and is later swapped in, by the medium-term scheduler.

swapped/suspended processes;2 new states: swapped waiting, swapped ready

context switch

context is represented in PCB

context switch(上下文切换): performing a state save of the current process and a state restore of a different process

发生中断时,系统需要保存当前运行在CPU中进程的上下文,上下文通过PCB表示。state save; state restore

Context-switch time is pure overhead

Context-switch times dependent on hardware support

operations on process

process creation:

reasons: Submission of a batch job, User logs on, provide a service, Process creates another process assign a PID, allocate space, initialize PCB, resource sharing.

When a process is created, it obtains initialization data that may be passed along from the parent process to the child process.

UNIX examples

​ system call fork creates new process

​ system call exec used after a fork to replace the child process’ memory space with a new program.

create-process system call

process identifier PID

创建子进程时,可能从操作系统直接获得资源,也可能只从其父进程那获得资源

process termination

Process executes last statement and asks the operating system to delete it (via exit).

Parent may terminate execution of its children processes (via abort). cascading termination

interprocess communication(IPC)

independent / cooperating processes

  • shared memory

    速度快 memory speed

    unbounded/bounded buffer

  • message passing

    交换少量数据, 不必避免冲突,易于实现。通常通过system call实现,内核介入的时间消耗。

    分布式环境中有用

    communication link

    直接通信(direct communication-naming): 每个进程必须明确地命名通信的接收者或发送者。每对进程之间只有一个线路,一个线路只与两个进程有关。symmetry/asymmetry in addressing

    间接通信(indirect): 通过mailbox或port。两个进程共享一个邮箱时,建立通信线路。一个线路可以与多个进程关联。每个线路对应一个邮箱。进程/OS可以拥有邮箱。拥有邮箱的进程只能收。

    blocking or unblocking(synchronous or asynchronous)

    当send和receive都阻塞时,发送者和接收者之间有一个集合点(rendezvous)

    buffering: zero/bounded/unbounded capacity

    message system with no/automatic buffering

examples of IPC

实例

  • POSIX

    shmget,shmat,shmdt,

  • Mach

    Microkernel OS

    Mach communication is message based

    mailbox , called port in Mach

    Each task gets two mailboxes at creation– Kernel and Notify

    Kernel uses the kernel mailbox to communicate with task, and sends notification of event occurrences to the Notify port.

    kernel mailbox, notify mailbox

    msg_send, msg_receive, msg_rpc(remote procedure call远程过程调用)

    为分布式系统设计

  • LPC(local procedure call) in windows

    位于同一机器的两进程通信

    port object

    2 types of ports

    • connection ports

      为所有进程可见

    • communication ports

    两种端口消息传递技术

    • 小消息:

      端口队列(message queue)作为中间存储,将消息从一个进程复制到另一个进程

    • 大消息:

      section object

      不能马上响应: callback mechanism 允许异步消息传递

communication in client-server systems

  • socket

    A socket is defined as an endpoint for communication.

    All ports below 1024 are considered well known;When a client process initiates a request for a connection, it is assigned a port (greater than 1024) by its host computer.

    一对通信进程需使用一对socket,一个进程一个

    socket由IP+端口号组成

    所有连接必须唯一

  • 远程过程调用RPC remote procedure call

    用于通过网络连接系统

    Each message is addressed to an RPC daemon listening to a port on the remote system.

    Stubs – client-side proxy for the actual procedure on the server.

    The client-side stub locates port on the server and marshals the parameters, transmits a message to the server.

    The server-side stub receives this message, unpacks the marshaled parameters, and performs the procedure on the server. Return values are passed back to the client.

    machine-independent representation of data, external data representation (XDR).

    客户端提供存根(stub),对每个独立的远程过程都有一个stub。存根位于服务器的端口,编组(marshal)参数。XDR

  • 远程方法调用RMI remote method invocation

    RPC的java版

    RMI allows a Java program on one machine to invoke a method on a remote object.

    对象位于不同JVM上,就认为是远程的

    存根(stub)和骨干(skeleton).存根为远程对象的代理,驻留在客户机上,存根将包发给服务器,远程对象的骨干会接受它

    参数传递规则:

    • 编排参数是本地对象:对象串行化(object serialization) 复制传递;
    • 编排参数是远程对象:引用传递 reference
    • 本地对象作为参数传给远程对象:实现接口 java.io.Serialization

最后修改于 2020-02-28