操作系统笔记 from Neso
Index
https://www.youtube.com/watch?v=vBURTt97EkA&list=PLBlnK6fEyqRiVhbXDGLXDk_OQAeuVcp2O
- Introduction to Operating System
- Basics of OS (Computer System Operation)
- Basics of OS (Storage Structure)
- Basics of OS (I/O Structure)
- Computer System Architecture
- Operating System Structure
- Operating System Services
- User Operating System Interface
- System Calls
- Types of System Calls
- System Programs
- Operating System Design & Implementation
- Structures of Operating System
- Virtual Machines
- Operating System Generation and System Boot
- Process Management (Processes and Threads)
- Process State
- Process Control Block
- Process Scheduling
- Context Switch
- Operation on Processes – Process Creation
- Operation on Processes – Process Termination
- Interprocess Communication
- Shared Memory Systems
- Message Passing Systems (Part 1)
- Message Passing Systems (Part 2)
- Message Passing Systems (Part 3)
- Sockets in Operating System
- Remote Procedure Calls (RPC)
- Issues in RPC & How They're Resolved
- Introduction to Threads
- Multithreading Models & Hyperthreading
- fork() and exec() System Calls
- Threading Issues [fork() & exec() System Calls]
- Threading Issues (Thread Cancellation)
- Introduction to CPU Scheduling
- CPU and I/O Burst Cycles
- Preemptive and Non-Preemptive Scheduling
- Scheduling Criteria
- Scheduling Algorithms - First Come First Served (FCFS)
- First Come First Served Scheduling (Solved Problem 1)
- First Come First Served Scheduling (Solved Problem 2)
- Scheduling Algorithms - Shortest Job First (SJF)
- Shortest Job First Scheduling (Solved Problem 1)
- Shortest Job First Scheduling (Solved Problem 2)
- Scheduling Algorithms - Priority Scheduling
- Priority Scheduling (Solved Problem 1)
- Priority Scheduling (Solved Problem 2)
- Scheduling Algorithms - Round Robin Scheduling
- Round Robin Scheduling (Turnaround Time & Waiting Time)
- Round Robin Scheduling - Solved Problem (Part 1)
- Round Robin Scheduling - Solved Problem (Part 2)
- Multilevel Queue Scheduling Algorithm
- Multilevel Feedback-Queue Scheduling Algorithm
- Scheduling Algorithms – Solved Problems
- Process Synchronization | Chapter-6 | Operating System
(To be continued) - Deadlocks | Chapter-7 | Operating System
- Main Memory | Chapter-8 | Operating System
- Virtual Memory | Chapter-9 | Operating System
- File Systems | Chapter-10 | Operating System
- File System Implementation | Chapter-11 | Operating System
- Mass Storage Structure | Chapter-12 | Operating System
Notes
23 IPC
OS里运行的processes可以是independent processes or cooperating processes
- Independent processes: 无法影响和被影响系统里 其他进程
- Cooperating processes: 可以影响和被影响
凡是share data的进程都是cooperating process
进程合作的好处: - Information sharing 共享信息
- Computation speedup 计算加速
- Modularity 模块化
- Convenience
IPC的两种基本模型:
- Shared memory:给一片共享内存,然后合作进程可以通过读写这片共享内存交换信息。
- Message passing: 合作进程通过交换message来进行通信
24 Shared Memory Systems
- 通信的进程需要建立一块共享内存
- 一般来说,共享的内存在发起方的address space里
- 其他进程如需通信,需要将共享内存attach在他们自己的address space上
- 通常OS会阻止不同进程访问彼此内存
- 共享内存会让2个或更多进程同意移除此限制
一个使用例子是生产消费问题, 可以通过buffer of items来满足生产与消费。
Buffer有两种 - Unbounded buffer:buffer size没有limit
- Bounded buffer: fixed buffer size, buffer为空消费者等;buffer为满生产者等
25,26,27 Message-Passing Systems
不用共享内存,在分布式环境里尤其有用。
需要同时拥有收发能力。
进程发送的Message可以是固定长度也可以是可变长度。
Fixed size: 系统层面直接,编程层面困难
可变长度: 系统实现复杂,编程层面简单
进程之间需要有communication link,这个link有多种实现方式。可以逻辑上实现link并拥有send()/receive()操作的方法有:
- Direct or indirect communication
- Synchronous or asynchronous communication
- Automatic or explicit buffering
同时有几个问题要解决:
Naming
Synchronization
Buffering
Naming
需要通信的进程必须要有refer to each other的方法,可以direct or indirect communication
Direct communication
在direct communication里,每个进程必须明确地指出通信的接收方
- send (P, message) -send a message to process P
- receive (Q, message) - receive a message from process Q
直连有如下性质: - 每对想联络的process之间自动生成link, process需要知道对方身份。
- 每个link只关联两个process
- 每对process之间,只存在一个link
这种方案展现了寻址里的对称性(symmetry in addressing): 收发方需要name the other来通信
一个直连方案的变种:发送方需要name接收方,接收方无需name发送方。
- send (P, message) -send a message to process P
- receive (id, message) - receive a message from any process; the variable id is set to the name of the process with which communication has taken place
这种方案展现了寻址里的非对称性(asymmetry in addressing)
这两种方案(symmetric and asymmetric)都有个缺点:limited modularity of the resulting process definitions。如果进程名称更改,则可能需要重新检查所有的其他进程定义。
Indirect communication
消息经由mailboxes或port收发。
- mailbox可被抽象地视为供进程存放移除message的物体
- 每个mailbox有unique identification
- 两进程只有同时拥有shared mailbox时才可以通信
举例 - send (A, message) -send a message to mailbox A
- receive (A, message) - receive a message from mailbox A
间接连有如下性质: - 只有双方进程共享shared mailbox时,link才能建立
- 每个link能关联两个及以上process
- 每对通信的process之间,可以存在多个不同的links, 每个link对应一个mailbox
mailbox可以由进程或者操作系统拥有。
Synchronization
进程间的通信通过send()和receive() 基元/primitives来实现。基元的实现可以有不同设计。消息可以是blocking或nonblocking, 也称之为synchronous和asynchronous。
收发X阻塞分阻塞,4种情况
Buffering
无论通信是直接还是间接,被进程们交换的信息都栖息在一个临时队列里。这种队列可以有三种实现:
Zero capacity: 队列长度为0. 发送者被block直至接受者收到消息。
Bounded capacity: 队列长度为n, 最多存n个messages. 如果link满了,sender会被阻塞,直至队列有新的空。
Unbounded capacity: 队列长度无限,sender永不阻塞。
28 Sockets
- A socket is defined as an endpoint for communication.
- A pair of processes communicating over a network employ a pair of sockets-one for each process.
- A socket is identified by an IP address concatenated with a port number.
- The server waits for incoming client requests by listening to a specified port. Once a request is received, the server accepts a connection from the client socket to complete the connection.
- Servers implementing specific services(such as telnet, ftp and http) listen to well- known ports(telnet 23, ftp 21, http 80)
- All ports below 1024 are considered well known; we can use them to implement standard services
29,30 RPC
RPC是一种通信协议,可以让一个program通过网络去请求另一台机器上的服务,而无需知道网络细节。
- 与IPC机制在很多方面相似,但是因为是跨环境,所以需要用 message based communication scheme来提供远程服务。
- 与IPC对比的是,RPC交换的messages都是well structured, 所以不只是packets of data。
- 每个消息都会发给远端系统里监听port的RPC daemon, 信息里包含了identifier of被执行的function和传递的参数
- 之后function被执行,output由另一个message返回
The semantics(语义) of RPC 让client能向远端host invoke(调用)a procedure如本地调用一般
- RPC系统通过在客户端提供一个stub来隐藏通信细节
- 一般来说,每个独立的remote procedure有一个独立的stub
- 当client要调用remote procedure时,RPC系统会call对应的stub, 传递需要的参数。这个stub会确定服务器上的端口并把参数序列化(marshal the parameters)
- 参数序列化包含把参数放入能通过网络传输的特定格式。
- 然后stub将message通过message passing发送给server
- 服务端类似的stub接受此message并调用服务器procedure
- 如果需要,返回值以相同方式被返回client
RPC有一些问题
- Data presentation: eg big-endian和little-endian.
- A: 解决方法是RPC定义了data的machine-independent representation,比方XDR(external data representation)。客户端参数序列化时就包括了把数据转换成XDR,服务器端反之亦然。
- 因为网络问题导致RPC失败,重复并被执行多次。
A: 解决方法是OS必须保证messages只被执行正好一次。 - 在standard procedure call里,procedure call's name is placed by the memory address of the procedure call。RPC scheme也需要类似的binding of 服务端和客户端端口,客户端怎么知悉服务端port number? 他们又不共享内存。
A:
- Binding information可以是predetermined, 通过fixed port addresses. 程序编译后,服务器无法改变该服务端口。
- Binding可以由rendezvous mechanism(同步汇合机制)来动态搞定。一般操作系统会提供一个rendezvous(也叫matchmaker) daemon在固定的RPC port上。客户端发送含有RPC名称的消息给rendezvous daemon, 请求它所需执行的port address。服务端返回端口信息,RPC调用可以发送到那个端口直至process terminates或者服务器挂了。