CS 111 Spring 2022
Lecture 2 Page 1
Operating System Services, Resources, and Interfaces CS 111
Spring 2022 Operating System Principles Peter Reiher
Copyright By PowCoder代写 加微信 powcoder
• Operating systems services
• System service layers and mechanisms • Service interfaces and standards
• Service and interface abstractions
CS 111 Spring 2022
Lecture 2 Page 2
OS Services
• The operating system offers important services to other programs
• Generally offered as abstractions
• Important basic categories:
– CPU/Memory abstractions
• Processes, threads, virtual machines
• Virtual address spaces, shared segments
– Persistent storage abstractions • Files and file systems
– Other I/O abstractions
• Virtual terminal sessions, windows
• Sockets, pipes, VPNs, signals (as interrupts)
CS 111 Spring 2022
Lecture 2 Page 3
Services: Higher Level Abstractions
• Cooperating parallel processes – Locks, condition variables
– Distributed transactions, leases
• Security
– User authentication
– Secure sessions, at-rest encryption
• User interface
– GUI widgets, desktop and window management
– Multi-media
CS 111 Spring 2022
Lecture 2 Page 4
Services: Under the Covers
Not directly visible to users
Enclosure management
– Hot-plug, power, fans, fault handling
Software updates and configuration registry Dynamic resource allocation and scheduling
– CPU, memory, bus resources, disk, network
Networks, protocols and domain services – USB, BlueTooth
– TCP/IP, DHCP, LDAP, SNMP
– iSCSI, CIFS, NFS
CS 111 Spring 2022
Lecture 2 Page 5
How Can the OS Deliver These Services?
• Several possible ways
– Applications could just call subroutines
– Applications could make system calls
– Applications could send messages to software that performs the services
• Each option works at a different layer of the stack of software
CS 111 Spring 2022
Lecture 2 Page 6
OS Layering
• Modern OSes offer services via layers of software and hardware
• High level abstract services offered at high software layers
• Lower level abstract services offered deeper in the OS
• Ultimately, everything mapped down to relatively simple hardware
CS 111 Spring 2022
Lecture 2 Page 7
Software Layering
(user and system) applications
Operating System services
middleware services
Application Binary Interface
general libraries
Operating System kernel
privileged instruction set
general instruction set
CS 111 Spring 2022
Instruction Set Architecture
Lecture 2 Page 8
Service Delivery via Subroutines • Accessservicesviadirectsubroutinecalls
– Push parameters, jump to subroutine, return values in registers on on the stack
• Typicallyathighlayers
• Advantages
– Extremely fast (nano-seconds)
– Run-time implementation binding possible
• Disadvantages
– All services implemented in same address space – Limited ability to combine different languages – Can’t usually use privileged instructions
CS 111 Spring 2022
Lecture 2 Page 9
Service Delivery via Libraries
Onesubroutineservicedeliveryapproach Programmersneednotwriteallcodeforprograms
– Standard utility functions can be found in libraries
Alibraryisacollectionofobjectmodules
– A single file that contains many files (like a zip or jar) – These modules can be used directly, w/o recompilation
Mostsystemscomewithmanystandardlibraries – System services, encryption, statistics, etc.
– Additional libraries may come with add-on products
Programmerscanbuildtheirownlibraries
– Functions commonly needed by parts of a product Spring 2022
Lecture 2 Page 10
The Library Layer
(user and system) applications
Operating System services
middleware services
Application Binary Interface
general libraries
Operating System kernel
privileged instruction set
general instruction set
CS 111 Spring 2022
Instruction Set Architecture
Lecture 2 Page 11
Characteristics of Libraries
• Many advantages
– Reusable code makes programming easier
– A single well written/maintained copy
– Encapsulates complexity … better building blocks
• Multiple bind-time options
– Static … include in load module at link time
– Shared … map into address space at exec time – Dynamic … choose and load at run-time
• It is only code … it has no special privileges
CS 111 Spring 2022
Lecture 2 Page 12
Sharing Libraries
• Static library modules are added to a program’s load module
– Each load module has its own copy of each library • This dramatically increases the size of each process
– Program must be re-linked to incorporate new library • Existing load modules don’t benefit from bug fixes
• Instead, make each library a sharable code segment
– One in-memory copy, shared by all processes
– Keep the library separate from the load modules
– Operating system loads library along with program
CS 111 Spring 2022
Lecture 2 Page 13
Advantages of Shared Libraries
• Reduced memory consumption
– One copy can be shared by multiple processes/programs
• Faster program start-ups
– If it’s already in memory, it need not be loaded again
• Simplified updates
– Library modules are not included in program load modules
– Library can be updated easily (e.g., a new version with bug fixes)
– Programs automatically get the newest version when they are restarted
CS 111 Spring 2022
Lecture 2 Page 14
Limitations of Shared Libraries • Not all modules will work in a shared library
– They cannot define/include global data storage • They are added into program memory
– Whether they are actually needed or not
• Called routines must be known at compile-time – Only the fetching of the code is delayed ’til run-time – Symbols known at compile time, bound at link time
• Dynamically Loadable Libraries are more general – They eliminate all of these limitations … at a price
CS 111 Spring 2022
Lecture 2 Page 15
Where Is the Library?
Library X App 2
Secondary Storage
CS 111 Spring 2022
Lecture 2 Page 16
Static Libraries
Library X App 2
Secondary Storage
Compile App 1 Run App 1
CS 111 Spring 2022
Compile App 2 Run App 2
Two copies of library X in memory!
Lecture 2 Page 17
Shared Libraries
Library X App 2
Secondary Storage
Compile App 1 Run App 1
CS 111 Spring 2022
Compile App 2 Run App 2
One copy of library X in memory!
Lecture 2 Page 18
Dynamic Libraries
Compile App 1
CS 111 Spring 2022
Compile App 2
App 1 calls library function
Load only the dynamic libraries that are called
At the moment when they are called
Lecture 2 Page 19
Service Delivery via System Calls
• Force an entry into the operating system – Parameters/returns similar to subroutine
– Implementation is in shared/trusted kernel
• Advantages
– Able to allocate/use new/privileged resources
– Able to share/communicate with other processes
• Disadvantages
– 100x-1000x slower than subroutine calls
CS 111 Spring 2022
Lecture 2 Page 20
Providing Services via the Kernel
• Primarily functions that require privilege
– Privileged instructions (e.g., interrupts, I/O)
– Allocation of physical resources (e.g., memory) – Ensuring process privacy and containment
– Ensuring the integrity of critical resources
• Some operations may be out-sourced – System daemons, server processes
• Some plug-ins may be less trusted
– Device drivers, file systems, network protocols
CS 111 Spring 2022
Lecture 2 Page 21
The Kernel Layer
(user and system) applications
Operating System services
middleware services
Application Binary Interface
general libraries
Operating System kernel
privileged instruction set
general instruction set
CS 111 Spring 2022
Instruction Set Architecture
Lecture 2 Page 22
System Services Outside the Kernel
• Not all trusted code must be in the kernel
– It may not need to access kernel data structures
– It may not need to execute privileged instructions
• Some are actually somewhat privileged processes
– Login can create/set user credentials
– Some can directly execute I/O operations
• Some are merely trusted
– sendmail is trusted to properly label messages
– NFS server is trusted to honor access control data Lecture 2
CS 111 Spring 2022
System Service Layer
(user and system) applications
Operating System services
middleware services
Application Binary Interface
general libraries
Operating System kernel
privileged instruction set
general instruction set
CS 111 Spring 2022
Instruction Set Architecture
Lecture 2 Page 24
Service Delivery via Messages
• Exchange messages with a server (via syscalls)
– Parameters in request, returns in response
• Advantages:
– Server can be anywhere on earth (or local)
– Service can be highly scalable and available
– Service can be implemented in user-mode code
• Disadvantages:
– 1,000x-100,000x slower than subroutine
– Limited ability to operate on process resources
CS 111 Spring 2022
Lecture 2 Page 25
System Services via Middleware
• Software that is a key part of the application or service platform, but not part of the OS
– Database, pub/sub messaging system
– Apache, Nginx
– Hadoop, Zookeeper, Beowulf, OpenStack – Cassandra, RAMCloud, Ceph, Gluster
• Kernel code is very expensive and dangerous – User-mode code is easier to build, test and debug – User-mode code is much more portable
– User-mode code can crash and be restarted
CS 111 Spring 2022
Lecture 2 Page 26
The Middleware Layer
(user and system) applications
Operating System services
middleware services
Application Binary Interface
general libraries
Operating System kernel
privileged instruction set
general instruction set
CS 111 Spring 2022
Instruction Set Architecture
Lecture 2 Page 27
OS Interfaces
• Nobody buys a computer to run the OS
• The OS is meant to support other programs – Via its abstract services
• Usually intended to be very general – Supporting many different programs
• Interfaces are required between the OS and other programs to offer general services
CS 111 Spring 2022
Lecture 2 Page 28
Interfaces: APIs • Application Program Interfaces
– A source level interface, specifying: • Include files, data types, constants
• Macros, routines and their parameters
APIs help you write programs for your OS
• A basis for software portability
– Recompile program for the desired architecture
– Linkage edit with OS-specific libraries
– Resulting binary runs on that architecture and OS
• An API compliant program will compile & run on any compliant system
CS 111 – APIs are primarily for programmers Spring 2022
Lecture 2 Page 29
Interfaces: ABIs
• Application Binary Interfaces
– A binary interface, specifying:
• Dynamically loadable libraries (DLLs)
• Data formats, calling sequences, linkage conventions
– The binding of an API to a hardware architecture
• A basis for binary compatibility
– One binary serves all customers for that hardware • E.g. all x86 Linux/BSD/MacOS/Solaris/…
ABIs help you install binaries on your OS
• An ABI compliant program will run (unmodified) on any compliant system
• ABIs are primarily for users CS 111
Spring 2022
Lecture 2 Page 30
Libraries and Interfaces
• Normal libraries (shared and otherwise) are accessed through an API
– Source-level definitions of how to access the library
– Readily portable between different machines
• Dynamically loadable libraries also called through an API
– But the dynamic loading mechanism is ABI- specific
– Issues of word length, stack format, linkages, etc.
CS 111 Spring 2022
Lecture 2 Page 31
Interfaces and Interoperability
• Strong, stable interfaces are key to allowing programs to operate together
• Also key to allowing OS evolution
• You don’t want an OS upgrade to break your
existing programs
• Which means the interface between the OS and those programs better not change
CS 111 Spring 2022
Lecture 2 Page 32
Interoperability Requires Stability
• No program is an island
– Programs use system calls
– Programs call library routines
– Programs operate on external files
– Programs exchange messages with other software – If interfaces change, programs fail
• API requirements are frozen at compile time – Execution platform must support those interfaces – All partners/services must support those protocols
CS 111 – All future upgrades must support older interfacesLecture 2 Spring 2022 Page 33
Interoperability Requires Compliance
• Complete interoperability testing is impossible – Cannot test all applications on all platforms
– Cannot test interoperability of all implementations – New apps and platforms are added continuously
• Instead, we focus on the interfaces
– Interfaces are completely and rigorously specified – Standards bodies manage the interface definitions – Compliance suites validate the implementations
• And hope that sampled testing will suffice
CS 111 Spring 2022
Lecture 2 Page 34
Side Effects
• A side effect occurs when an action on one object has non-obvious consequences
– Effects not specified by interfaces
– Perhaps even to other objects
Often due to shared state between seemingly independent modules and functions
CS 111 Spring 2022
Lecture 2 Page 35
Side effects lead to unexpected behaviors And the resulting bugs can be hard to find
In other words, not good
Tip: Avoid all side effects in complex systems!
Abstractions
• Many things an operating system handles are complex
– Often due to varieties of hardware, software, configurations
• Life is easy for application programmers and users if they work with a simple abstraction
• The operating system creates, manages, and exports such abstractions
CS 111 Spring 2022
Lecture 2 Page 36
Simplifying Abstractions
• Hardware is fast, but complex and limited – Using it correctly is extremely complicated
– It may not support the desired functionality
– It is not a solution, but merely a building block
• Abstractions . . .
– Encapsulate implementation details
• Error handling, performance optimization
• Eliminate behavior that is irrelevant to the user
– Provide more convenient or powerful behavior • Operations better suited to user needs
CS 111 Spring 2022
Lecture 2 Page 37
Critical OS Abstractions
• The OS provides some core abstractions that our computational model relies on
– And builds others on top of those
• Memory abstractions
• Processor abstractions
• Communications abstractions
CS 111 Spring 2022
Lecture 2 Page 38
Abstractions of Memory
• Many resources used by programs and people relate to data storage
– Variables
– Chunks of allocated memory
– Database records
– Messages to be sent and received
• These all have some similar properties – You read them and you write them
CS 111 – But there are complications Spring 2022
Lecture 2 Page 39
Some Complicating Factors
Persistent vs. transient memory
Size of memory operations
– Size the user/application wants to work with – Size the physical device actually works with
Coherence and atomicity Latency
Same abstraction might be implemented with many different physical devices
– Possibly of very different types Spring 2022
Lecture 2 Page 40
Where Do the Complications Come From?
• At the bottom, the OS doesn’t have abstract devices with arbitrary properties
• It has particular physical devices
– With unchangeable, often inconvenient, properties
• The core OS abstraction problem:
– Creating the abstract device with the desirable
CS 111 Spring 2022
properties from the physical device that lacks them
Lecture 2 Page 41
An Example We can read or write the file
– We can read or write arbitrary amounts of data
If we write the file, we expect our next read to
reflect the results of the write
– Coherence
We expect the entire read/write to occur
– Atomicity
If there are several reads/writes to the file, we
A typical file
expect them to occur in some order Spring 2022
Lecture 2 Page 42
What Is Implementing the File? • Often a flash drive
• Flash drives have peculiar characteristics
– Write-once (sort of) semantics
• Re-writing requires an erase cycle • Which erases a whole block
• And is slow
– Atomicity of writing typically at word level – Blocks can only be erased so many times
• So the operating system needs to smooth out these oddities
CS 111 Spring 2022
Lecture 2 Page 43
What Does That Lead To?
• Different structures for the file system
– Since you can’t easily overwrite data words in place
• Garbage collection to deal with blocks largely filled with inactive data
• Maintaining a pool of empty blocks
• Wear-leveling in use of blocks
• Something to provide desired atomicity of multi-word writes
CS 111 Spring 2022
Lecture 2 Page 44
Abstractions of Interpreters
• An interpreter is something that performs commands
• Basically, the element of a computer (abstract or physical) that gets things done
• At the physical level, we have a processor
• That level is not easy to use
• The OS provides us with higher level interpreter abstractions
CS 111 Spring 2022
Lecture 2 Page 45
Basic Interpreter Components • An instruction reference
– Tells the interpreter which instruction to do next • Arepertoire
– The set of things the interpreter can do • An environment reference
– Describes the current state on which the next instruction should be performed
• Interrupts
– Situations in which the instruction reference pointer is overridden
CS 111 Spring 2022
Lecture 2 Page 46
An Example
The OS maintains a program counter for the process
– An instruction reference
Its source code specifies its repertoire
Its stack, heap, and register contents are its environment
– With the OS maintaining pointers to all of them No other interpreters should be able to mess up
the process’ resources Spring 2022
Lecture 2 Page 47
Implementing the Process
Abstraction in the OS
• Easy if there’s only one process
• But there are almost always multiple processes
• The OS has limited physical memory
– To hold the environment information
• There is usually only one set of registers
– Or one per core
• The process shares the CPU or core
– With other processes
CS 111 Spring 2022
Lecture 2 Page 48
What Does That Lead To?
• Schedulers to share the CPU among various processes
• Memory management hardware and software
– To multiplex memory use among the processes
– Giving each the illusion of full exclusive use of memory
• Access control mechanisms for other memory abstractions
– So other processes can’t fiddle with my files
CS 111 Spring 2022
Lecture 2 Page 49
Abstractions of Communications
• A communication link allows one interpreter to talk to another
– On the same or different machines
• At the physical level, memory and cables
• At more abstract levels, networks and interprocess communication mechanisms
• Some similarities to memory abstractions – But also differences
CS 111 Spring 2022
Lecture 2 Page 50
Why Are Communication Links
Distinct From Memory?
• Highly variable performance
• Often asynchronous
– And usually issues with synchronizing the parties
• Receiver may only perform the operation because the send occurred
– Unlike a typical read
• Additional complications when working with a
remote machine
CS 111 Spring 2022
Lecture 2 Page 51
Implementing the Communications Link Abstraction in the OS
• Easy if both ends are on the same machine – Not so easy if they aren’t
• On same machine, use memory for transfer
– Copy message from sender’s memory to receiver’s
– Or transfer control of memory containing the message from sender to receiver
• Again, more complicated when remote
CS 111 Spring 2022
Lecture 2 Page 52
What Does That Lead To?
• Need to optimize costs of copying
• Tricky memory management
• Inclusion of complex network protocols in the OS itself
• Worries about message loss, retransmission, etc.
• New security concerns that OS might need to address
CS 111 Spring 2022
Lecture 2 Page 53
Generalizing Abstractions
• How can applications deal with many varied resources?
• Make many different things appear the same
– Applications can all deal with a
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com