4.1Applied Operating System Concepts
Module 4,Processes
进程
Process Concept
进程概念
Process Scheduling
进程调度
Operation on Processes
进程上的操作
Cooperating Processes
协同进程
Interprocess Communication
进程间通信
4.2Applied Operating System Concepts
Process Concept
进程概念
An operating system executes a variety of programs:
操作系统执行各种程序
– Batch system – jobs 批处理系统 - 作业
– Time-shared systems – user programs or tasks
分时系统 - 用户程序或任务
Textbook uses the terms job and process almost
interchangeably,本书使用的名词作业和进程,基本可互换
Process – a program in execution; process execution must
progress in sequential fashion.
进程 - 在执行中的程序;进程的执行必须以顺序方式进行
A process includes,一个进程包括
– program counter 程序计数器
– stack 栈
– data section 数据部分
4.3Applied Operating System Concepts
Process State
进程状态
As a process executes,it changes state
进程执行时,改变状态
– new,The process is being created.
新建:在创建进程
– running,Instructions are being executed.
运行:指令在执行
– waiting,The process is waiting for some event to
occur.
等待:进程等待某些事件发生
– ready,The process is waiting to be assigned to a
processor.
就绪:进程等待分配处理器
– terminated,The process has finished execution.
终止:进程执行完毕
4.4Applied Operating System Concepts
Diagram of Process State
进程状态图
4.5Applied Operating System Concepts
Process Control Block (PCB)
进程控制块
Information associated with each process.
同进程有关的信息
Process state 进程状态
Program counter 程序计数器
CPU registers CPU寄存器
CPU scheduling information CPU调度信息
Memory-management information内存管理信息
Accounting information 计账信息
I/O status information I/O状态信息
4.6Applied Operating System Concepts
Process Control Block (PCB)
进程控制块
4.7Applied Operating System Concepts
CPU Switch From Process to Process
进程间 CPU的切换
4.8Applied Operating System Concepts
Process Scheduling Queues
进程调度队列
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.
在各种队列之间进程的迁移
4.9Applied Operating System Concepts
Ready Queue And Various I/O Device Queues
就绪队列和各种 I/O设备队列
4.10Applied Operating System Concepts
Representation of Process Scheduling
进程调度的描述
4.11Applied Operating System Concepts
Schedulers
调度
Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue.
长程调度(或作业调度) -选择可以进入就绪队列的进程
Short-term scheduler (or CPU scheduler) – selects which
process should be executed next and allocates CPU.
短程调度(或 CPU调度) -选择可被下一个执行并分配 CPU的进程
4.12Applied Operating System Concepts
Addition of Medium Term Scheduling
中程调度
4.13Applied Operating System Concepts
Schedulers调度 (Cont.)
Short-term scheduler is invoked very frequently
(milliseconds)? (must be fast),短程调度切换频率高
Long-term scheduler is invoked very infrequently
(seconds,minutes)? (may be slow),长程调度不快
The long-term scheduler controls the degree of multiprogramming.
长程调度控制了多道程序的“道”
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处理
4.14Applied Operating System Concepts
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.
时间取决于硬件的支持
4.15Applied Operating System Concepts
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.
父进程等待,直到子进程终止
4.16Applied Operating System Concepts
Process Creation (Cont.)
进程创建
Address space 地址空间
– Child duplicate of parent,子女复制双亲
– Child has a program loaded into it,子女有一个程序被调入
UNIX examples UNIX例子
– fork system call creates new process
fork 系统调用创建新进程
– execve system call used after a fork to replace the
process’ memory space with a new program.
在 fork 用一个新程序替代了进程的内存空间之后,采用
execve系统调用
4.17Applied Operating System Concepts
A Tree of Processes On A Typical UNIX System
典型 UNIX系统中的进程树
4.18Applied Operating System Concepts
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,级联终止
4.19Applied Operating System Concepts
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 方便
4.20Applied Operating System Concepts
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.
有界缓冲对缓冲区大小作了限定
4.21Applied Operating System Concepts
Bounded-Buffer – Shared-Memory Solution
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;
4.22Applied Operating System Concepts
Bounded-Buffer (Cont.)
Consumer process
repeat
while in = out do no-op;
nextc,= buffer [out];
out,= out+1 mod n;
…
consume the item in nextc
…
until false;
Solution is correct,but can only fill up n–1 buffer.
4.23Applied Operating System Concepts
Threads
线程
A thread (or lightweight process) is a basic unit of CPU
utilization; it consists of:
线程(轻型进程)是 CPU运用的一个基本单元,包括
– program counter 程序计数器
– register set 寄存器集
– stack space 栈空间
A thread shares with its peer threads its:
一个线程与它的对等线程共享:
– code section 代码段
– data section 数据段
– operating-system resources 操作系统资源
collectively know as a task,总体作为一个任务
A traditional or heavyweight process is equal to a task with
one thread
传统的或重型进程等价于只有一个线程的任务
4.24Applied Operating System Concepts
Threads线程 (Cont.)
In a multiple threaded task,while one server thread is blocked and waiting,a
second thread in the same task can run,在一个多线程任务中,当一个服务器线程被阻塞后,在同一个任务中的第 2个线程可运行
– Cooperation of multiple threads in same job confers higher hroughput
and improved performance,同一作业中多线程协同导致高吞吐量并改善性能
– Applications that require sharing a common buffer (i.e.,producer-
consumer) benefit from thread utilization,应用程序需要共享一个公用的缓冲(即生产者 -消费者),从线程运用中得利
Threads provide a mechanism that allows sequential processes to make
blocking system calls while also achieving parallelism.
线程提供了一种机制,允许序列进程阻塞系统调用同时还能实现并行
Kernel-supported threads 支持线程的内核 (Mach and OS/2).
User-level threads; supported above the kernel,via a set of library calls at
the user level (Project Andrew from CMU),用户级线程;在内核之上,通过用户级的库调用
Hybrid approach implements both user-level and kernel-supported threads
(Solar s 2),混合处理实现用户级和内核支持线程
4.25Applied Operating System Concepts
Multiple Threads within a Task
一个任务中的多线程
4.26Applied Operating System Concepts
Threads Support in Solaris 2
Solaris 2支持的线程
Solaris 2 is a version of UNIX with support for threads at the kernel and
user levels,symmetric multiprocessing,and real-time scheduling,Solaris
2 是 UNIX的一个版本,支持内核级和用户级线程,对称多处理和实时调度
LWP – intermediate level between user-level threads and kernel-level
threads,在用户级线程和内核级线程之间层次
Resource needs of thread types,线程需要的资源
– Kernel thread,small data structure and a stack; thread switching
does not require changing memory access information – relatively
fast,内核线程:小数据结构及栈;线程切换不需要内存访问信息 - 相对快
– LWP,PCB with register data,accounting and memory information,;
switching between LWPs is relatively slow,带有寄存器数据的 PCB,记账和内存信息;在 LWP之间切换相对慢
– User-level thread,only need stack and program counter; no kernel
involvement means fast switching,Kernel only sees the LWPs that
support user-level threads,用户级线程:只需栈及程序计数器;无内核涉及所以切换快。内核只关注支持用户级线程的 LWP
4.27Applied Operating System Concepts
Solaris 2 Threads
Solaris 2线程
4.28Applied Operating System Concepts
Interprocess Communication (IPC)
进程间通信
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)逻辑的(如,逻辑特性)
4.29Applied Operating System Concepts
Implementation Questions
实现中的问题
How are links established? 连接如何建立?
Can a link be associated with more than two processes?
连接可同多于两个的进程相关吗?
How many links can there be between every pair of
communicating processes? 每对在通信进程有多少连接?
What is the capacity of a link? 一个连接的容量是多少?
Is the size of a message that the link can accommodate fixed
or variable? 连接可使用的固定或可变消息的大小?
Is a link unidirectional or bi-directional? 连接是无向的还是双向的?
4.30Applied Operating System Concepts
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.
连接可以无向,但通常是双向的
4.31Applied Operating System Concepts
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.
仅当共享一个信箱时进程才能通信
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 销毁信箱
4.32Applied Operating System Concepts
Indirect Communication (Continued)
间接通信
Mailbox sharing 信箱共享
– P1,P2,and P3 share mailbox A,P1,P2与 P3共享信箱 A
– P1,sends; P2 and P3 receive,P1发送; P2与 P3接受
– Who gets the message?谁得到消息?
Solutions 解决方案
– 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.
允许系统任意选择接收者。发送者被通知谁是接收者。
4.33Applied Operating System Concepts
Buffering
缓冲
Queue of messages attached to the link; implemented in one
of three ways.
消息队列附加在连接上;采用三个之一的实现方案
1,Zero capacity – 0 messages
零容量 - 0 消息
Sender must wait for receiver (rendezvous).
发送者必须等待接收者
2,Bounded capacity – finite length of n messages
有界容量 - n个消息有限长度
Sender must wait if link full.
若连接满了发送者必须等待
3,Unbounded capacity – infinite length
无界容量 - 无限长度
Sender never waits.
发送者从不等待
4.34Applied Operating System Concepts
Exception Conditions – Error Recovery
异常条件 - 出错恢复
Process terminates
进程终止
Lost messages
消息丢失
Scrambled Messages
消息受损
4.35Applied Operating System Concepts
例子
Mach的例子
Windows NT的例子小结
Module 4,Processes
进程
Process Concept
进程概念
Process Scheduling
进程调度
Operation on Processes
进程上的操作
Cooperating Processes
协同进程
Interprocess Communication
进程间通信
4.2Applied Operating System Concepts
Process Concept
进程概念
An operating system executes a variety of programs:
操作系统执行各种程序
– Batch system – jobs 批处理系统 - 作业
– Time-shared systems – user programs or tasks
分时系统 - 用户程序或任务
Textbook uses the terms job and process almost
interchangeably,本书使用的名词作业和进程,基本可互换
Process – a program in execution; process execution must
progress in sequential fashion.
进程 - 在执行中的程序;进程的执行必须以顺序方式进行
A process includes,一个进程包括
– program counter 程序计数器
– stack 栈
– data section 数据部分
4.3Applied Operating System Concepts
Process State
进程状态
As a process executes,it changes state
进程执行时,改变状态
– new,The process is being created.
新建:在创建进程
– running,Instructions are being executed.
运行:指令在执行
– waiting,The process is waiting for some event to
occur.
等待:进程等待某些事件发生
– ready,The process is waiting to be assigned to a
processor.
就绪:进程等待分配处理器
– terminated,The process has finished execution.
终止:进程执行完毕
4.4Applied Operating System Concepts
Diagram of Process State
进程状态图
4.5Applied Operating System Concepts
Process Control Block (PCB)
进程控制块
Information associated with each process.
同进程有关的信息
Process state 进程状态
Program counter 程序计数器
CPU registers CPU寄存器
CPU scheduling information CPU调度信息
Memory-management information内存管理信息
Accounting information 计账信息
I/O status information I/O状态信息
4.6Applied Operating System Concepts
Process Control Block (PCB)
进程控制块
4.7Applied Operating System Concepts
CPU Switch From Process to Process
进程间 CPU的切换
4.8Applied Operating System Concepts
Process Scheduling Queues
进程调度队列
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.
在各种队列之间进程的迁移
4.9Applied Operating System Concepts
Ready Queue And Various I/O Device Queues
就绪队列和各种 I/O设备队列
4.10Applied Operating System Concepts
Representation of Process Scheduling
进程调度的描述
4.11Applied Operating System Concepts
Schedulers
调度
Long-term scheduler (or job scheduler) – selects which
processes should be brought into the ready queue.
长程调度(或作业调度) -选择可以进入就绪队列的进程
Short-term scheduler (or CPU scheduler) – selects which
process should be executed next and allocates CPU.
短程调度(或 CPU调度) -选择可被下一个执行并分配 CPU的进程
4.12Applied Operating System Concepts
Addition of Medium Term Scheduling
中程调度
4.13Applied Operating System Concepts
Schedulers调度 (Cont.)
Short-term scheduler is invoked very frequently
(milliseconds)? (must be fast),短程调度切换频率高
Long-term scheduler is invoked very infrequently
(seconds,minutes)? (may be slow),长程调度不快
The long-term scheduler controls the degree of multiprogramming.
长程调度控制了多道程序的“道”
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处理
4.14Applied Operating System Concepts
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.
时间取决于硬件的支持
4.15Applied Operating System Concepts
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.
父进程等待,直到子进程终止
4.16Applied Operating System Concepts
Process Creation (Cont.)
进程创建
Address space 地址空间
– Child duplicate of parent,子女复制双亲
– Child has a program loaded into it,子女有一个程序被调入
UNIX examples UNIX例子
– fork system call creates new process
fork 系统调用创建新进程
– execve system call used after a fork to replace the
process’ memory space with a new program.
在 fork 用一个新程序替代了进程的内存空间之后,采用
execve系统调用
4.17Applied Operating System Concepts
A Tree of Processes On A Typical UNIX System
典型 UNIX系统中的进程树
4.18Applied Operating System Concepts
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,级联终止
4.19Applied Operating System Concepts
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 方便
4.20Applied Operating System Concepts
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.
有界缓冲对缓冲区大小作了限定
4.21Applied Operating System Concepts
Bounded-Buffer – Shared-Memory Solution
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;
4.22Applied Operating System Concepts
Bounded-Buffer (Cont.)
Consumer process
repeat
while in = out do no-op;
nextc,= buffer [out];
out,= out+1 mod n;
…
consume the item in nextc
…
until false;
Solution is correct,but can only fill up n–1 buffer.
4.23Applied Operating System Concepts
Threads
线程
A thread (or lightweight process) is a basic unit of CPU
utilization; it consists of:
线程(轻型进程)是 CPU运用的一个基本单元,包括
– program counter 程序计数器
– register set 寄存器集
– stack space 栈空间
A thread shares with its peer threads its:
一个线程与它的对等线程共享:
– code section 代码段
– data section 数据段
– operating-system resources 操作系统资源
collectively know as a task,总体作为一个任务
A traditional or heavyweight process is equal to a task with
one thread
传统的或重型进程等价于只有一个线程的任务
4.24Applied Operating System Concepts
Threads线程 (Cont.)
In a multiple threaded task,while one server thread is blocked and waiting,a
second thread in the same task can run,在一个多线程任务中,当一个服务器线程被阻塞后,在同一个任务中的第 2个线程可运行
– Cooperation of multiple threads in same job confers higher hroughput
and improved performance,同一作业中多线程协同导致高吞吐量并改善性能
– Applications that require sharing a common buffer (i.e.,producer-
consumer) benefit from thread utilization,应用程序需要共享一个公用的缓冲(即生产者 -消费者),从线程运用中得利
Threads provide a mechanism that allows sequential processes to make
blocking system calls while also achieving parallelism.
线程提供了一种机制,允许序列进程阻塞系统调用同时还能实现并行
Kernel-supported threads 支持线程的内核 (Mach and OS/2).
User-level threads; supported above the kernel,via a set of library calls at
the user level (Project Andrew from CMU),用户级线程;在内核之上,通过用户级的库调用
Hybrid approach implements both user-level and kernel-supported threads
(Solar s 2),混合处理实现用户级和内核支持线程
4.25Applied Operating System Concepts
Multiple Threads within a Task
一个任务中的多线程
4.26Applied Operating System Concepts
Threads Support in Solaris 2
Solaris 2支持的线程
Solaris 2 is a version of UNIX with support for threads at the kernel and
user levels,symmetric multiprocessing,and real-time scheduling,Solaris
2 是 UNIX的一个版本,支持内核级和用户级线程,对称多处理和实时调度
LWP – intermediate level between user-level threads and kernel-level
threads,在用户级线程和内核级线程之间层次
Resource needs of thread types,线程需要的资源
– Kernel thread,small data structure and a stack; thread switching
does not require changing memory access information – relatively
fast,内核线程:小数据结构及栈;线程切换不需要内存访问信息 - 相对快
– LWP,PCB with register data,accounting and memory information,;
switching between LWPs is relatively slow,带有寄存器数据的 PCB,记账和内存信息;在 LWP之间切换相对慢
– User-level thread,only need stack and program counter; no kernel
involvement means fast switching,Kernel only sees the LWPs that
support user-level threads,用户级线程:只需栈及程序计数器;无内核涉及所以切换快。内核只关注支持用户级线程的 LWP
4.27Applied Operating System Concepts
Solaris 2 Threads
Solaris 2线程
4.28Applied Operating System Concepts
Interprocess Communication (IPC)
进程间通信
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)逻辑的(如,逻辑特性)
4.29Applied Operating System Concepts
Implementation Questions
实现中的问题
How are links established? 连接如何建立?
Can a link be associated with more than two processes?
连接可同多于两个的进程相关吗?
How many links can there be between every pair of
communicating processes? 每对在通信进程有多少连接?
What is the capacity of a link? 一个连接的容量是多少?
Is the size of a message that the link can accommodate fixed
or variable? 连接可使用的固定或可变消息的大小?
Is a link unidirectional or bi-directional? 连接是无向的还是双向的?
4.30Applied Operating System Concepts
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.
连接可以无向,但通常是双向的
4.31Applied Operating System Concepts
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.
仅当共享一个信箱时进程才能通信
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 销毁信箱
4.32Applied Operating System Concepts
Indirect Communication (Continued)
间接通信
Mailbox sharing 信箱共享
– P1,P2,and P3 share mailbox A,P1,P2与 P3共享信箱 A
– P1,sends; P2 and P3 receive,P1发送; P2与 P3接受
– Who gets the message?谁得到消息?
Solutions 解决方案
– 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.
允许系统任意选择接收者。发送者被通知谁是接收者。
4.33Applied Operating System Concepts
Buffering
缓冲
Queue of messages attached to the link; implemented in one
of three ways.
消息队列附加在连接上;采用三个之一的实现方案
1,Zero capacity – 0 messages
零容量 - 0 消息
Sender must wait for receiver (rendezvous).
发送者必须等待接收者
2,Bounded capacity – finite length of n messages
有界容量 - n个消息有限长度
Sender must wait if link full.
若连接满了发送者必须等待
3,Unbounded capacity – infinite length
无界容量 - 无限长度
Sender never waits.
发送者从不等待
4.34Applied Operating System Concepts
Exception Conditions – Error Recovery
异常条件 - 出错恢复
Process terminates
进程终止
Lost messages
消息丢失
Scrambled Messages
消息受损
4.35Applied Operating System Concepts
例子
Mach的例子
Windows NT的例子小结