0% found this document useful (0 votes)
21 views

Os Mid

Uploaded by

211184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Os Mid

Uploaded by

211184
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 249

Chapter 1: Introduction

Operating System Concepts – 10h Edition Silberschatz, Galvin and Gagne ©2018
Objectives

 To describe the basic organization of computer systems


 To provide a grand tour of the major components of operating systems
 To give an overview of the many types of computing environments

Operating System Concepts – 10th Edition 1.2 Silberschatz, Galvin and Gagne ©2018
Computer System Structure

Operating System Concepts – 10th Edition 1.3 Silberschatz, Galvin and Gagne ©2018
Computer System Components

 Computer system can be divided into three components:

1. Hardware – provides basic computing resources


 CPU, memory, I/O devices

2. Software
 Operating system – controls and coordinates use of hardware
among various applications and users
 Other system programs
 Application programs – the system resources are used to solve
the computing problems of the users
– Word processors, web browsers, database systems, games

3. Users: People, machines, other computers

Operating System Concepts – 10th Edition 1.4 Silberschatz, Galvin and Gagne ©2018
Computer System Components

Operating System Concepts – 10th Edition 1.5 Silberschatz, Galvin and Gagne ©2018
UNIX Architecture

Operating System Concepts – 10th Edition 1.6 Silberschatz, Galvin and Gagne ©2018
What are operating systems? What they
do?

Operating System Concepts – 10th Edition 1.7 Silberschatz, Galvin and Gagne ©2018
What is an OS?

 OS is a program that acts as an intermediary between a user of a


computer and the computer hardware

 OS Goals:
 Execute user programs and make solving user problems easier
 Make the computer system convenient to use
 Use the computer hardware in an efficient manner

Operating System Concepts – 10th Edition 1.8 Silberschatz, Galvin and Gagne ©2018
What OS’s do?

 Depends on the point of view


 Users want convenience, ease of use and good performance
 Don’t care about resource utilization
 But shared computer such as mainframe or minicomputer must keep
all users happy
 Users of dedicate systems such as workstations have dedicated
resources but frequently use shared resources from servers
 Handheld computers are resource poor, optimized for usability and
battery life
 Some computers have little or no user interface, such as embedded
computers in devices and automobiles

Operating System Concepts – 10th Edition 1.9 Silberschatz, Galvin and Gagne ©2018
OS Definition (System’s View)

 OS is a resource allocator
 Manages all resources
 Decides between conflicting requests for efficient and fair resource
use

 OS is a control program
 Controls execution of programs to prevent errors and improper use
of the computer

Operating System Concepts – 10th Edition 1.10 Silberschatz, Galvin and Gagne ©2018
OS Kernel
 The part of OS that resides in memory at all times, and performs the
most essential system tasks, and has a hardware protection

 The one program running at all times on the computer

 Aka OS nucleus

 Copied into RAM, usually from the hard disk drive, during the computer
boot-up (or startup)

Operating System Concepts – 10th Edition 1.11 Silberschatz, Galvin and Gagne ©2018
Computer Startup

1. Press the Power Button: Sends an electrical signal to the hardware


components

2. Basic Input/Output System (BIOS): A special program stored in a chip on


computer's motherboard. It performs a Power-On Self-Test (POST), checking if
all the essential hardware components.

3. Bootstrap Loader: The BIOS looks for a small program called the "bootstrap
loader" stored in ROM or EPROM, generally known as firmware. Its job is to
locate and start (load into memory) the main part of the operating system
(i.e., the kernel).

4. Operating System: Once the kernel is up and running, it loads the full
operating system (like Windows or macOS) into memory.

Operating System Concepts – 10th Edition 1.12 Silberschatz, Galvin and Gagne ©2018
Why do we study operating systems?

Operating System Concepts – 10th Edition 1.13 Silberschatz, Galvin and Gagne ©2018
Computer System Organization

Operating System Concepts – 10th Edition 1.14 Silberschatz, Galvin and Gagne ©2018
Computer System Organization

 Computer System Operation:


 One or more CPUs and device controllers connect through common
bus providing access to shared memory
 Concurrent execution of CPUs and devices competing for memory
cycles

Operating System Concepts – 10th Edition 1.15 Silberschatz, Galvin and Gagne ©2018
Computer System Operation

 I/O devices and the CPU can execute concurrently


 Each device controller is in charge of a particular device type
 Each device controller has a local buffer
 CPU moves data from/to main memory to/from local buffers
 I/O is from the device to local buffer of controller
 The device controller informs CPU that it has finished its operation by
causing an interrupt

Operating System Concepts – 10th Edition 1.16 Silberschatz, Galvin and Gagne ©2018
Common Functions of Interrupts

 Interrupt transfers control to the interrupt service routine (ISR) generally


through the interrupt vector, which contains the addresses of all the
service routines

 Interrupt architecture must save the address of the interrupted


instruction

 A trap or exception is a software-generated interrupt caused either by a


user request or an error

 An OS is interrupt driven

Operating System Concepts – 10th Edition 1.17 Silberschatz, Galvin and Gagne ©2018
Interrupt Handling

 The OS preserves the state of the CPU by storing registers and the
program counter

 Determines which type of interrupt has occurred:


 polling
 vectored interrupt system

 Separate segments of code determine what action should be taken for


each type of interrupt

Operating System Concepts – 10th Edition 1.18 Silberschatz, Galvin and Gagne ©2018
Interrupt Timeline

Operating System Concepts – 10th Edition 1.19 Silberschatz, Galvin and Gagne ©2018
I/O Structure

 After I/O starts, control returns to user program only upon I/O
completion
 Wait instruction idles the CPU until the next interrupt
 Wait loop (contention for memory access)
 At most one I/O request is outstanding at a time, no simultaneous
I/O processing

 After I/O starts, control returns to user program without waiting for I/O
completion
 System call – request to the OS to allow user to wait for I/O
completion
 Device-status table contains entry for each I/O device indicating
its type, address, and state
 OS indexes into I/O device table to determine device status and to
modify table entry to include interrupt

Operating System Concepts – 10th Edition 1.20 Silberschatz, Galvin and Gagne ©2018
Storage Hierarchy

 Storage systems organized in hierarchy


 Speed
 Cost
 Volatility

Operating System Concepts – 10th Edition 1.21 Silberschatz, Galvin and Gagne ©2018
Storage Device Hierarchy

Operating System Concepts – 10th Edition 1.22 Silberschatz, Galvin and Gagne ©2018
Caching

 Caching – copying information in use from slower to faster storage


system temporarily
 For example, main memory can be viewed as a cache for secondary
storage

 Faster storage (cache) checked first to determine if information is there


 If it is, information used directly from the cache (fast)
 If not, data copied to cache and used there

 Cache is smaller than storage being cached


 Cache management is an important design problem
 Cache size and replacement policy

Operating System Concepts – 10th Edition 1.23 Silberschatz, Galvin and Gagne ©2018
Direct Memory Access (DMA) Structure

 Used for high-speed I/O devices able to transmit information at close to


memory speeds

 Device controller transfers blocks of data from buffer storage directly to


main memory without CPU intervention

 Only one interrupt is generated per block, rather than the one interrupt
per byte

Operating System Concepts – 10th Edition 1.24 Silberschatz, Galvin and Gagne ©2018
How modern computers work?

A von Neumann architecture

Operating System Concepts – 10th Edition 1.25 Silberschatz, Galvin and Gagne ©2018
Computer System Architecture

 Multiprocessors systems growing in use and importance

 Also called parallel systems, tightly-coupled systems

 Advantages include:
1. Increased throughput
2. Economy of scale
3. Increased reliability – graceful degradation or fault tolerance

 Two types:
1. Asymmetric Multiprocessing – each processor is assigned a
specie task.
2. Symmetric Multiprocessing – each processor performs all
tasks

Operating System Concepts – 10th Edition 1.26 Silberschatz, Galvin and Gagne ©2018
Symmetric Multiprocessing

Operating System Concepts – 10th Edition 1.27 Silberschatz, Galvin and Gagne ©2018
A Dual-Core Design
 Multi-chip and multicore

 Systems containing all chips


 Chassis containing multiple separate systems

Operating System Concepts – 10th Edition 1.28 Silberschatz, Galvin and Gagne ©2018
CPU 1 CPU 2

CPU 3 CPU 4

Source: https://www.scaler.com/topics/images/muticore_and_multiprocessor.webp
Operating System Concepts – 10th Edition 1.29 Silberschatz, Galvin and Gagne ©2018
Clustered Systems

 Aka distributed systems or loosely-coupled systems

 Like multiprocessor systems, but multiple systems working together


 Usually sharing storage via a storage-area network (SAN)
 Provides a high-availability service which survives failures
 Asymmetric clustering has one machine in hot-standby mode
 Symmetric clustering has multiple nodes running applications,
monitoring each other
 Some clusters are for high-performance computing (HPC)
 Applications must be written to use parallelization
 Some have distributed lock manager (DLM) to avoid conflicting
operations

Operating System Concepts – 10th Edition 1.30 Silberschatz, Galvin and Gagne ©2018
Clustered Systems

Operating System Concepts – 10th Edition 1.31 Silberschatz, Galvin and Gagne ©2018
Operating System Structures

Operating System Concepts – 10th Edition 1.32 Silberschatz, Galvin and Gagne ©2018
OS Structures
 Simple batch system

 Multiprogramming batch system needed for efficiency


 Organizes jobs (code & data) so that CPU always has one to execute
 OS keeps a subset of jobs in memory
 One job selected and run via job scheduling
 When it has to wait (for I/O for example), OS switches to another job

 Timesharing (aka multitasking or interactive) is logical extension in


which CPU switches jobs so frequently that users can interact with each job while
it is running, creating interactive computing
 Response time should be < 1 second
 Each user has at least one program executing in memory process
 If several jobs are ready to run at the same time  CPU scheduling
 If processes don’t fit in memory, swapping moves them in and out to run
 Virtual memory allows execution of processes not completely in memory

Operating System Concepts – 10th Edition 1.33 Silberschatz, Galvin and Gagne ©2018
Memory Layout for
Multiprogrammed System

Operating System Concepts – 10th Edition 1.34 Silberschatz, Galvin and Gagne ©2018
Operating System Operations

Operating System Concepts – 10th Edition 1.35 Silberschatz, Galvin and Gagne ©2018
OS Operations
 Interrupt driven (hardware and software)

 Hardware interrupt by one of the devices

 Software interrupt (exception or trap):


 Software error (e.g., division by zero, memory page fault)
 System call (request for OS service)
 Other process problems include infinite loop

Operating System Concepts – 10th Edition 1.36 Silberschatz, Galvin and Gagne ©2018
OS Operations (Cont.)

 Dual-mode operation allows OS to protect itself and other system


components
 User mode and kernel mode
 Mode bit provided by hardware
 Provides ability to distinguish when system is running user code
or kernel code
 Some instructions designated as privileged, only executable in
kernel mode
 System call changes mode to kernel, return from call resets it to
user

 Increasingly CPUs support multi-mode operations


 i.e., virtual machine manager (VMM) mode for guest VMs

Operating System Concepts – 10th Edition 1.37 Silberschatz, Galvin and Gagne ©2018
Transition from User to Kernel Mode
 Timer to prevent infinite loop / process hogging resources
 Timer is set to interrupt the computer after some time period
 Keep a counter that is decremented by the physical clock
 OS sets the counter (privileged instruction)
 When counter = 0, generate an interrupt
 Set up before scheduling process to regain control or terminate
program that exceeds allotted time

Operating System Concepts – 10th Edition 1.38 Silberschatz, Galvin and Gagne ©2018
Operating System Subsystem Managers

Operating System Concepts – 10th Edition 1.39 Silberschatz, Galvin and Gagne ©2018
Source: Textbook 2 (Understanding Operating Systems), Fig. 1.1
Operating System Concepts – 10th Edition 1.40 Silberschatz, Galvin and Gagne ©2018
What does each subsystem manager do?
 Takes responsibility of its own tasks
 Monitors its resource continuously
 Enforces the polices that determine who gets what, when, and
how much
 Allocates the resource (among users, programs, processors, …)
 Deallocates the resource

 Control = sharing + monitoring + protection + report + payment

 Works harmoniously with every other manager


 Example?

Operating System Concepts – 10th Edition 1.41 Silberschatz, Galvin and Gagne ©2018
Example: A user chooses a menu option
to open a program
1. The DM receives the electrical impulse caused by a click of the mouse,
decodes the command by calculating the location of the cursor.
Immediately, it sends the command to the PM.

2. The PM then sends an acknowledgment message (such as “waiting” or


“loading”) to be displayed on the monitor so that the user knows that
the command has been sent successfully.

3. The PM determines whether the user request requires that a file (in this
case a program file) be retrieved from storage, or whether it is already
in memory.

4. If the program is in secondary storage (perhaps on a disk), the FM


calculates its exact location on the disk and passes this information to
the DM, which retrieves the program and sends it to the MM.

Operating System Concepts – 10th Edition 1.42 Silberschatz, Galvin and Gagne ©2018
Example: A user chooses a menu option
to open a program (Cont.)
5. If necessary, the MM finds space for the program file in main memory
and records its exact location. Once the program file is in memory, the
MM keeps track of its location in memory.

6. When the CPU is ready to run it, the program begins execution via the
PM.

7. The PM reassigns the CPU to the next program waiting in line. If the
file was modified, the FM and DM cooperate to store the results in
secondary storage.

8. The MM releases the program’s space in main memory and gets ready
to make it available to the next program that requires memory.

9. Finally, the UI displays the results and gets ready to take the next
command.
Operating System Concepts – 10th Edition 1.43 Silberschatz, Galvin and Gagne ©2018
OS Architectures

Early OS (Single Unit) Object-Oriented OS

OS Kernel File management


functions
Memory allocation
Entire OS
module
….

User program …
User program area
area

Main Memory Main Memory

Operating System Concepts – 10th Edition 1.44 Silberschatz, Galvin and Gagne ©2018
Process Management
 A process is a program in execution. It is a unit of work within the
system. Program is a passive entity, process is an active entity.
 Process needs resources to accomplish its task
 CPU, memory, I/O, files
 Initialization data
 Process termination requires reclaim of any reusable resources
 Single-threaded process has one program counter specifying location
of next instruction to execute
 Process executes instructions sequentially, one at a time, until
completion
 Multi-threaded process has one program counter per thread
 Typically system has many processes, some user, some OS running
concurrently on one or more CPUs
 Concurrency by multiplexing the CPUs among the processes /
threads

Operating System Concepts – 10th Edition 1.45 Silberschatz, Galvin and Gagne ©2018
Process Management Activities

The OS is responsible for the following activities in connection with


process management:
 Creating and deleting both user and system processes
 Suspending and resuming processes
 Providing mechanisms for process synchronization
 Providing mechanisms for process communication
 Providing mechanisms for deadlock handling

Operating System Concepts – 10th Edition 1.46 Silberschatz, Galvin and Gagne ©2018
Memory Management

 To execute a program, all (or part) of the instructions must be in memory

 All (or part) of the data that is needed by the program must be in
memory

 Memory management determines what is in memory and when


 Optimizing CPU utilization and computer response to users

 Memory management activities


 Keeping track of which parts of memory are currently being used
and by whom
 Deciding which processes (or parts thereof) and data to move into
and out of memory
 Allocating and deallocating memory space as needed

Operating System Concepts – 10th Edition 1.47 Silberschatz, Galvin and Gagne ©2018
File Management
 OS provides uniform, logical view of information storage
 Abstracts physical properties to logical storage unit - file
 Each medium is controlled by a device (i.e., disk drive, tape drive)
 Varying properties include access speed, capacity, data-transfer
rate, access method (sequential or random)

 File-System management
 Files usually organized into directories
 Access control on most systems to determine who can access what
 OS activities include
 Creating and deleting files and directories
 Primitives to manipulate files and directories
 Mapping files onto secondary storage
 Backup files onto stable (non-volatile) storage media

Operating System Concepts – 10th Edition 1.48 Silberschatz, Galvin and Gagne ©2018
Mass-Storage Management

 Usually disks used to store data that does not fit in main memory or data
that must be kept for a “long” period of time
 Proper management is of central importance
 Entire speed of computer operation hinges on disk subsystem and its
algorithms
 OS activities
 Free-space management
 Storage allocation
 Disk scheduling
 Some storage doesn’t need to be fast
 Tertiary storage includes optical storage, magnetic tape
 Still must be managed – by OS or applications
 Varies between WORM (write-once, read-many-times) and RW
(read-write)

Operating System Concepts – 10th Edition 1.49 Silberschatz, Galvin and Gagne ©2018
Migration from Disk to Register

 Multitasking environments must be careful to use the most recent value,


no matter where it is stored in the storage hierarchy

 Multiprocessor environment must provide cache coherency in


hardware such that all CPUs have the most recent value in their cache

 Distributed environment situation even more complex


 Several copies of a datum can exist

Operating System Concepts – 10th Edition 1.50 Silberschatz, Galvin and Gagne ©2018
I/O Subsystem

 One purpose of OS is to hide peculiarities of hardware devices from the


user

 I/O subsystem responsible for


 Memory management of I/O including buffering (storing data
temporarily while it is being transferred), caching (storing parts of
data in faster storage for performance), spooling (the overlapping of
output of one job with input of other jobs)
 General device-driver interface
 Drivers for specific hardware devices

Operating System Concepts – 10th Edition 1.51 Silberschatz, Galvin and Gagne ©2018
Protection and Security

Operating System Concepts – 10th Edition 1.52 Silberschatz, Galvin and Gagne ©2018
Protection and Security

 Protection – any mechanism for controlling access of processes or


users to resources defined by the OS

 Security – defense of the system against internal and external attacks


 Huge range, including denial-of-service, worms, viruses, identity
theft, theft of service

 Systems generally first distinguish among users, to determine who can


do what
 User identities (user IDs, security IDs) include name and associated
number, one per user
 User ID then associated with all files, processes of that user to
determine access control
 Group identifier (group ID) allows set of users to be defined and
controls managed, then also associated with each process, file
 Privilege escalation allows user to change to effective ID with more
rights

Operating System Concepts – 10th Edition 1.53 Silberschatz, Galvin and Gagne ©2018
Computing Environments

Operating System Concepts – 10th Edition 1.54 Silberschatz, Galvin and Gagne ©2018
Traditional Computing
 Stand-alone general-purpose machines
 But blurred as most systems interconnect with others (i.e., the Internet)
 Portals provide web access to internal systems
 Network computers (thin clients) are like Web terminals
 Mobile computers interconnect via wireless networks
 Networking becoming ubiquitous – even home systems use firewalls to
protect home computers from Internet attacks

Operating System Concepts – 10th Edition 1.55 Silberschatz, Galvin and Gagne ©2018
Mobile Computing
 Handheld smartphones, tablets, etc
 What is the functional difference between them and a “traditional”
laptop?
 Extra feature – more OS features (GPS, gyroscope)
 Allows new types of apps like augmented reality
 Use IEEE 802.11 wireless, or cellular data networks for connectivity
 Leaders are Apple iOS and Google Android

Operating System Concepts – 10th Edition 1.56 Silberschatz, Galvin and Gagne ©2018
Distributed Computing
 Distributed computiing
 Collection of separate, possibly heterogeneous, systems networked
together
 Network is a communications path, TCP/IP most common
– Local Area Network (LAN)
– Wide Area Network (WAN)
– Metropolitan Area Network (MAN)
– Personal Area Network (PAN)
 Network OS provides features between systems across network
 Communication scheme allows systems to exchange messages
 Illusion of a single system

Operating System Concepts – 10th Edition 1.57 Silberschatz, Galvin and Gagne ©2018
Client-Server Computing
 Client-Server Computing
 Dumb terminals supplanted by smart PCs
 Many systems are now servers, responding to requests generated
by clients
 Compute-server system provides an interface to client to
request services (i.e., database)
 File-server system provides interface for clients to store and
retrieve files

Operating System Concepts – 10th Edition 1.58 Silberschatz, Galvin and Gagne ©2018
Peer-to-Peer Computing

 Another model of distributed systems

 P2P does not distinguish clients and servers


 Instead all nodes are considered peers
 May each act as client, server or both
 Node must join P2P network
 Registers its service with central
lookup service on network, or
 Broadcast request for service and
respond to requests for service via
discovery protocol
 Examples include Napster and Gnutella,
Voice over IP (VoIP) such as Skype

Operating System Concepts – 10th Edition 1.59 Silberschatz, Galvin and Gagne ©2018
Virtualization

 Allows OS’s to run applications within other OS’s


 Vast and growing industry

 Emulation used when the source CPU type differs from the target type
(e.g., PowerPC to Intel x86)
 Generally slowest method
 When computer language not compiled to native code –
Interpretation

 Virtualization – OS natively compiled for CPU, running guest OS’s


also natively compiled
 Consider VMware running WinXP guests, each running applications,
all on native WinXP host OS
 VMM (virtual machine Manager) provides virtualization services

Operating System Concepts – 10th Edition 1.60 Silberschatz, Galvin and Gagne ©2018
Virtualization (Cont.)

 Use cases involve laptops and desktops running multiple OS’s for
exploration or compatibility
 Apple laptop running Mac OS X host, Windows as a guest
 Developing apps for multiple OS’s without having multiple systems
 QA testing applications without having multiple systems
 Executing and managing compute environments within data centers

 VMM can run natively, in which case they are also the host
 There is no general-purpose host then (VMware ESX and Citrix
XenServer)

Operating System Concepts – 10th Edition 1.61 Silberschatz, Galvin and Gagne ©2018
Virtualization

Operating System Concepts – 10th Edition 1.62 Silberschatz, Galvin and Gagne ©2018
Cloud Computing
 Delivers computing, storage, even apps as a service across a network

 Logical extension of virtualization because it uses virtualization as the base


for it functionality.
 Amazon EC2 has thousands of servers, millions of virtual machines,
petabytes of storage available across the Internet, pay based on usage

 Many types
 Public cloud – available via Internet to anyone willing to pay
 Private cloud – run by a company for the company’s own use
 Hybrid cloud – includes both public and private cloud components
 Software as a Service (SaaS) – one or more applications available via
the Internet (i.e., word processor)
 Platform as a Service (PaaS) – software stack ready for application use
via the Internet (i.e., a database server)
 Infrastructure as a Service (IaaS) – servers or storage available over
Internet (i.e., storage available for backup use)

Operating System Concepts – 10th Edition 1.63 Silberschatz, Galvin and Gagne ©2018
Cloud Computing (Cont.)
 Cloud computing environments composed of traditional OS’s,
plus VMMs, plus cloud management tools

Operating System Concepts – 10th Edition 1.64 Silberschatz, Galvin and Gagne ©2018
Real-Time Embedded Computing

 Real-time embedded systems most prevalent form of computers


 Vary considerable, special purpose, limited purpose OS,
real-time OS
 Use expanding

 Many other special computing environments as well


 Some have OS’s, some perform tasks without an OS

 Real-time OS has well-defined fixed time constraints


 Processing must be done within constraint
 Correct operation only if constraints met

Operating System Concepts – 10th Edition 1.65 Silberschatz, Galvin and Gagne ©2018
End of Chapter 1

Operating System Concepts – 10h Edition Silberschatz, Galvin and Gagne ©2018
Chapter 2: Operating System
Structures

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Objectives
 To describe the services an OS provides to users, processes, and other
systems
 To discuss the various ways of structuring an OS
 To explain how OS’s are debugged

Operating System Concepts – 10th Edition 2.2 Silberschatz, Galvin and Gagne ©2018
Operating System Services

Operating System Concepts – 10th Edition 2.3 Silberschatz, Galvin and Gagne ©2018
OS Services
 OS provides an environment for execution of programs and services to programs
and users

 OS provides functions that are helpful to the user:

 User interface - Almost all OS’s have a user interface (UI).


 Varies between Command-Line (CLI), Graphics User Interface (GUI),
Batch

 Program execution - The system must be able to load a program into


memory and to run that program, end execution, either normally or
abnormally (indicating error)

 I/O operations - A running program may require I/O, which may involve a
file or an I/O device

Operating System Concepts – 10th Edition 2.4 Silberschatz, Galvin and Gagne ©2018
OS Services (Cont.)

 OS provides functions that are helpful to the user (Cont.):

 File system manipulation - Programs need to read and write files and
directories, create and delete them, search them, list file Information,
permission management.

 Communications – Processes may exchange information, on the same


computer or between computers over a network
 Communications may be via shared memory or through message
passing (packets moved by the OS)

 Error detection – OS needs to be constantly aware of possible errors


 May occur in the CPU and memory, in I/O devices, in user program
 For each type of error, OS should take the appropriate action to ensure
correct and consistent computing
 Debugging facilities can greatly enhance the user’s and programmer’s
abilities to efficiently use the system

Operating System Concepts – 10th Edition 2.5 Silberschatz, Galvin and Gagne ©2018
OS Services (Cont.)
 Another set of OS functions exists for ensuring the efficient operation of the
system itself via resource sharing

 Resource allocation - When multiple users or multiple jobs running


concurrently, resources must be allocated to each of them
 Many types of resources - CPU cycles, memory, file storage, I/O devices.

 Accounting - To keep track of which users use how much and what kinds of
computer resources

 Protection and security - The owners of information stored in a multiuser or


networked computer system may want to control use of that information,
concurrent processes should not interfere with each other
 Protection involves ensuring that all access to system resources is
controlled
 Security of the system from outsiders requires user authentication,
extends to defending external I/O devices from invalid access attempts

Operating System Concepts – 10th Edition 2.6 Silberschatz, Galvin and Gagne ©2018
Overview of OS Services

Operating System Concepts – 10th Edition 2.7 Silberschatz, Galvin and Gagne ©2018
User Operating System Interface

Operating System Concepts – 10th Edition 2.8 Silberschatz, Galvin and Gagne ©2018
User OS Interface – CLI

CLI or command interpreter allows direct command entry


 Sometimes implemented in kernel, sometimes by system program
 Sometimes multiple flavors implemented – shells
 Primarily fetches a command from user and executes it
 Built-in commands or just names of programs

Operating System Concepts – 10th Edition 2.9 Silberschatz, Galvin and Gagne ©2018
Bourne Shell Command Interpreter

Operating System Concepts – 10th Edition 2.10 Silberschatz, Galvin and Gagne ©2018
User OS Interface – GUI
 User-friendly desktop metaphor interface
 Usually mouse, keyboard, and monitor
 Icons represent files, programs, actions, etc.
 Various mouse buttons over objects in the interface cause various
actions (provide information, options, execute function, open
directory (known as a folder)
 Invented at Xerox PARC

 Many systems now include both CLI and GUI interfaces


 Microsoft Windows is GUI with CLI “command” shell
 Apple Mac OS X is “Aqua” GUI interface with UNIX kernel
underneath and shells available
 Unix and Linux have CLI with optional GUI interfaces (CDE, KDE,
GNOME)

Operating System Concepts – 10th Edition 2.11 Silberschatz, Galvin and Gagne ©2018
Touchscreen Interfaces

 Touchscreen devices require new


interfaces
 Mouse not possible or not
desired
 Actions and selection based on
gestures
 Virtual keyboard for text entry

 Voice commands

Operating System Concepts – 10th Edition 2.12 Silberschatz, Galvin and Gagne ©2018
The Mac OS X GUI

Operating System Concepts – 10th Edition 2.13 Silberschatz, Galvin and Gagne ©2018
System Calls

Operating System Concepts – 10th Edition 2.14 Silberschatz, Galvin and Gagne ©2018
System Calls
 Programming interface to the services provided by the OS

 Typically written in a high-level language (C or C++)

 Mostly accessed by programs via a high-level Application


Programming Interface (API)

 Three most common APIs are


 Win32 API for Windows
 POSIX API for POSIX-based systems (including virtually all
versions of UNIX, Linux, and Mac OS X)
 Java API for the Java virtual machine (JVM)

Operating System Concepts – 10th Edition 2.15 Silberschatz, Galvin and Gagne ©2018
Example of System Calls

 System call sequence to copy the contents of one file to another file

Operating System Concepts – 10th Edition 2.16 Silberschatz, Galvin and Gagne ©2018
Example of Standard API

Operating System Concepts – 10th Edition 2.17 Silberschatz, Galvin and Gagne ©2018
System Call Implementation

 Typically, a number associated with each system call


 System call interface maintains a table indexed according to these
numbers

 The system call interface invokes the intended system call in OS


kernel and returns status of the system call and any returned values

 The caller doesn’t need to know how the system call is implemented
 Just needs to obey API and understand what OS will do as a result
call
 Most details of OS interface hidden from programmer by API

Operating System Concepts – 10th Edition 2.18 Silberschatz, Galvin and Gagne ©2018
API – System Call – OS Relationship

Operating System Concepts – 10th Edition 2.19 Silberschatz, Galvin and Gagne ©2018
System Call Parameter Passing

 Three general methods used to pass parameters to the OS:

 Simplest: pass the parameters into registers


 In some cases, may be more parameters than registers

 Parameters stored in a block, or table, in memory, and address of


block passed as a parameter into a register
 This approach taken by Linux and Solaris

 Parameters placed, or pushed, onto the stack by the program and


popped off the stack by the OS

Operating System Concepts – 10th Edition 2.20 Silberschatz, Galvin and Gagne ©2018
Parameter Passing via Table

Operating System Concepts – 10th Edition 2.21 Silberschatz, Galvin and Gagne ©2018
Types of System Calls
 Process control
 create process, terminate process
 end, abort
 load, execute
 get process attributes, set process attributes
 wait for time
 wait event, signal event
 allocate and free memory
 Dump memory if error
 Debugger for determining bugs, single step execution
 Locks for managing access to shared data between processes

Operating System Concepts – 10th Edition 2.22 Silberschatz, Galvin and Gagne ©2018
Types of System Calls (Cont.)

 File management
 create file, delete file
 open, close file
 read, write, reposition
 get and set file attributes

 Device management
 request device, release device
 read, write, reposition
 get device attributes, set device attributes
 logically attach or detach devices

Operating System Concepts – 10th Edition 2.23 Silberschatz, Galvin and Gagne ©2018
Types of System Calls (Cont.)

 Information maintenance
 get time or date, set time or date
 get system data, set system data
 get and set process, file, or device attributes

 Communications
 create, delete communication connection
 send, receive messages if message passing model to host name
or process name
 From client to server
 Shared-memory model create and gain access to memory regions
 transfer status information
 attach and detach remote devices

Operating System Concepts – 10th Edition 2.24 Silberschatz, Galvin and Gagne ©2018
Types of System Calls (Cont.)

 Protection
 Control access to resources
 Get and set permissions
 Allow and deny user access

Operating System Concepts – 10th Edition 2.25 Silberschatz, Galvin and Gagne ©2018
Examples: Windows and Unix

Operating System Concepts – 10th Edition 2.26 Silberschatz, Galvin and Gagne ©2018
Standard C Library Example
 C program invoking printf() library call, which calls write() system call

Operating System Concepts – 10th Edition 2.27 Silberschatz, Galvin and Gagne ©2018
Example: MS-DOS

 Single-tasking
 Shell invoked when system
booted
 Simple method to run
program
 No process created
 Single memory space
 Loads program into memory,
overwriting all but the kernel
 Program exit  shell
reloaded

At system startup running a program

Operating System Concepts – 10th Edition 2.28 Silberschatz, Galvin and Gagne ©2018
Example: FreeBSD
 Unix variant
 Multitasking
 User login  invoke user’s choice of shell
 Shell executes fork() system call to create
process
 Executes exec() to load program into
process
 Shell waits for process to terminate or
continues with user commands
 exit(): Process exits with:
 code = 0 – no error
 code > 0 – error code

Operating System Concepts – 10th Edition 2.29 Silberschatz, Galvin and Gagne ©2018
System Programs

Operating System Concepts – 10th Edition 2.30 Silberschatz, Galvin and Gagne ©2018
System Programs
 System programs provide a convenient environment for program
development and execution. They can be divided into:
 File manipulation
 Status information sometimes stored in a file modification
 Programming language support
 Program loading and execution
 Communications
 Background services

 Most users’view of the OS is defined by system programs, not the


actual system calls

Operating System Concepts – 10th Edition 2.31 Silberschatz, Galvin and Gagne ©2018
System Programs (Cont.)
 Provide a convenient environment for program development and
execution
 Some of them are simply user interfaces to system calls; others are
considerably more complex

 File management - Create, delete, copy, rename, print, dump, list, and
generally manipulate files and directories

 Status information
 Some ask the system for info - date, time, amount of available
memory, disk space, number of users
 Others provide detailed performance, logging, and debugging
information
 Typically, these programs format and print the output to the terminal
or other output devices
 Some systems implement a registry - used to store and retrieve
configuration information
Operating System Concepts – 10th Edition 2.32 Silberschatz, Galvin and Gagne ©2018
System Programs (Cont.)
 File modification
 Text editors to create and modify files
 Special commands to search contents of files or perform
transformations of the text

 Programming-language support - Compilers, assemblers, debuggers


and interpreters sometimes provided

 Program loading and execution- Absolute loaders, relocatable


loaders, linkage editors, and overlay-loaders, debugging systems for
higher-level and machine language

 Communications - Provide the mechanism for creating virtual


connections among processes, users, and computer systems
 Allow users to send messages to one another’s screens, browse
web pages, send electronic-mail messages, log in remotely, transfer
files from one machine to another

Operating System Concepts – 10th Edition 2.33 Silberschatz, Galvin and Gagne ©2018
System Programs (Cont.)
 Background services
 Launch at boot time
 Some for system startup, then terminate
 Some from system boot to shutdown
 Provide facilities like disk checking, process scheduling, error
logging, printing
 Run in user context not kernel context
 Known as services, subsystems, daemons

Operating System Concepts – 10th Edition 2.34 Silberschatz, Galvin and Gagne ©2018
Operating System Design and
Implementation

Operating System Concepts – 10th Edition 2.35 Silberschatz, Galvin and Gagne ©2018
OS Design & Implementation
 Internal structure of different OS’s can vary widely

 Start the design by defining goals and specifications

 Affected by choice of hardware and type of system

 User goals and System goals


 User goals – OS should be convenient to use, easy to learn,
reliable, safe, and fast
 System goals – OS should be easy to design, implement, and
maintain, as well as flexible, reliable, error-free, and efficient

Operating System Concepts – 10th Edition 2.36 Silberschatz, Galvin and Gagne ©2018
OS Design & Implementation (Cont.)
 Important principle to separate
Policy: What will be done?
Mechanism: How to do it?
 Mechanisms determine how to do something
 Policies decide what will be done

 The separation of policy from mechanism is a very important principle;


it allows maximum flexibility if policy decisions are to be changed later

Operating System Concepts – 10th Edition 2.37 Silberschatz, Galvin and Gagne ©2018
Implementation
 Variations
 Early OS’s programmed in assembly
language
 Then system programming languages
like Algol, PL/1
 Now C, C++
Android:

 Usually, a mix of languages • Kernel: mostly in C


with some assembly
 Lowest levels of kernels in assembly
 Main body in C • System libraries: are
 Systems programs in C, C++, mostly in C or C++
scripting languages like PERL,
Python, shell scripts • App frameworks: are
in Java

 More high-level language easier to port to


other hardware  But slower
Operating System Concepts – 10th Edition 2.38 Silberschatz, Galvin and Gagne ©2018
Operating System Structure

Operating System Concepts – 10th Edition 2.39 Silberschatz, Galvin and Gagne ©2018
OS Structure
 General-purpose OS is a very large program

 Various ways to structure OS’s


 Simple structure – MS-DOS
 More complex – UNIX
 Layered – an abstrcation
 Microkernel – Mach

Operating System Concepts – 10th Edition 2.40 Silberschatz, Galvin and Gagne ©2018
Simple OS Structure – MS DOS

 MS-DOS – written to provide the


most functionality in the least
space
 Not divided into modules
 Although MS-DOS has some
structure, its interfaces and
levels of functionality are not
well separated

Operating System Concepts – 10th Edition 2.41 Silberschatz, Galvin and Gagne ©2018
Non-Simple OS Structure – UNIX
 The UNIX OS consists of two separable parts
 Systems programs
 The kernel
 Consists of everything below the system-call interface and above
the physical hardware
 Provides the file system, CPU scheduling, memory management,
and other operating-system functions; a large number of functions
for one level

Operating System Concepts – 10th Edition 2.42 Silberschatz, Galvin and Gagne ©2018
Non-Simple OS Structure – UNIX
Beyond simple but not fully layered

Operating System Concepts – 10th Edition 2.43 Silberschatz, Galvin and Gagne ©2018
Layered Approach

 The OS is divided into a


number of layers (levels), each
built on top of lower layers.
The bottom layer (layer 0), is
the hardware; the highest (layer
N) is the user interface.

 With modularity, layers are


selected such that each uses
functions (operations) and
services of only lower-level
layers

Operating System Concepts – 10th Edition 2.44 Silberschatz, Galvin and Gagne ©2018
Architecture of macOS and iOS

Operating System Concepts – 10th Edition 2.45 Silberschatz, Galvin and Gagne ©2018
Mac OS X Structure

graphical user interface


Aqua

application environments and services

Java Cocoa Quicktime BSD

kernel environment
BSD

Mach

I/O kit kernel extensions

Operating System Concepts – 10th Edition 2.46 Silberschatz, Galvin and Gagne ©2018
iOS

 Apple mobile OS for iPhone, iPad


 Structured on Mac OS X, added functionality
 Does not run OS X applications natively
 Also runs on different CPU architecture
(ARM vs. Intel)
 Cocoa Touch Objective-C API for
developing apps
 Media services layer for graphics, audio,
video
 Core services provides cloud computing,
databases
 Core OS, based on Mac OS X kernel

Operating System Concepts – 10th Edition 2.47 Silberschatz, Galvin and Gagne ©2018
Android
 Developed by Open Handset Alliance (mostly led by Google)
 Open Source
 Similar stack to iOS (layered)
 Based on Linux kernel but modified
 Provides process, memory, device-driver management
 Adds power management
 Runtime environment includes core set of libraries and Dalvik virtual
machine
 Apps developed in Java plus Android API
 Java class files compiled to Java bytecode then translated to
executable than runs in Dalvik VM
 Libraries include frameworks for web browser (webkit), database
(SQLite), multimedia, smaller libc

Operating System Concepts – 10th Edition 2.48 Silberschatz, Galvin and Gagne ©2018
Android Architecture

Application Framework

Libraries Android runtime

SQLite openGL Core Libraries

surface media
Dalvik
manager framework
virtual machine
webkit libc

Modified Linux Kernel

Operating System Concepts – 10th Edition 2.49 Silberschatz, Galvin and Gagne ©2018
Operating System Debugging

Operating System Concepts – 10th Edition 2.50 Silberschatz, Galvin and Gagne ©2018
OS Debugging

 Debugging is finding and fixing errors, or bugs

 OS generates log files containing error information

 Failure of an application can generate core dump file capturing memory


of the process

 OS failure can generate crash dump file containing kernel memory

 Beyond crashes, performance tuning can optimize system performance


 Sometimes using trace listings of activities, recorded for analysis
 Profiling is periodic sampling of instruction pointer to look for
statistical trends (counters)

Operating System Concepts – 10th Edition 2.51 Silberschatz, Galvin and Gagne ©2018
End of Chapter 2

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Chapter 3: Processes

OS Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018


Objectives
 To introduce the notion of a process
 To describe the various features of processes, including scheduling,
creation and termination, and communication
 To explore inter-process communication using shared memory and
message passing
 To describe communication in client-server systems

OS Concepts – 10th Edition 3.2 Silberschatz, Galvin and Gagne ©2018


Process Concept

OS Concepts – 10th Edition 3.3 Silberschatz, Galvin and Gagne ©2018


Process Concept
 Process
 A program in execution
 An active entity
 Requires resources (e.g., processor) to perform a function
 A single instance of an executable program
 Executes sequentially - (a single instruction is executed on behalf
of a process at a time)
 The terms “process” and “job” (or “task”) are used interchangeably

 Processes can be described as either:


 I/O-bound process – spends more time doing I/O than
computations; many short CPU bursts
 CPU-bound process – spends more time doing computations;
few very long CPU bursts

OS Concepts – 10th Edition 3.4 Silberschatz, Galvin and Gagne ©2018


Process Parts

 Multiple process parts:


 The program code, also called text section
 Current activity including program counter, and content of
processor’s registers
 Stack containing temporary data when invoking functions
 Function parameters, return addresses, local variables
 Data section containing global variables
 Heap containing memory dynamically allocated during run time

OS Concepts – 10th Edition 3.5 Silberschatz, Galvin and Gagne ©2018


Process in Memory

OS Concepts – 10th Edition 3.6 Silberschatz, Galvin and Gagne ©2018


Process Concept (Cont.)
 Program is a passive entity stored on disk (executable file)
 Program becomes process (active entity) when executable file
loaded into memory

 Execution of program starts via GUI mouse clicks, command line


entry of its name, etc.

 One program can be several processes


 Consider multiple users executing the same program

OS Concepts – 10th Edition 3.7 Silberschatz, Galvin and Gagne ©2018


Process State

 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

OS Concepts – 10th Edition 3.8 Silberschatz, Galvin and Gagne ©2018


Diagram of Process State

OS Concepts – 10th Edition 3.9 Silberschatz, Galvin and Gagne ©2018


Process Control Block (PCB)
Each process is represented in OS by a data
structure called a PCB (also called task
control block)
 Process state – running, waiting, etc.
 Program counter – location of instruction to
next execute
 CPU registers – contents of all process-
centric registers
 CPU scheduling information- priorities,
scheduling queue pointers
 Memory-management information –
memory allocated to the process
 Accounting information – CPU used, clock
time elapsed since start, time limits
 I/O status information – I/O devices
allocated to process, list of open files

OS Concepts – 10th Edition 3.10 Silberschatz, Galvin and Gagne ©2018


Process Scheduling

OS Concepts – 10th Edition 3.11 Silberschatz, Galvin and Gagne ©2018


Process Scheduling

 Maximize CPU use, quickly switch processes onto CPU for time
sharing

 Process scheduler selects among available processes for next


execution on CPU

 Maintains scheduling queues of processes


 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

OS Concepts – 10th Edition 3.12 Silberschatz, Galvin and Gagne ©2018


Ready Queue & I/O Device Queues

OS Concepts – 10th Edition 3.13 Silberschatz, Galvin and Gagne ©2018


Representation of Scheduling

 Queueing diagram represents queues, resources, flows

OS Concepts – 10th Edition 3.14 Silberschatz, Galvin and Gagne ©2018


Schedulers
 Long-term scheduler (or job scheduler) – selects which
processes in the job queue should be brought into the ready queue
 The first manager that the job encounters
 Invoked infrequently (seconds, minutes)  (may be slow)

 Short-term scheduler (or CPU scheduler) – selects which


process in the ready queue should be executed next and allocates
CPU, and when it should be terminated
 Sometimes the only scheduler in a system
 Invoked frequently (milliseconds)  (must be fast)

 Medium-term scheduler can be added if degree of multiple


programming needs to decrease (or when the system is overloaded)
 Used in a highly interactive systems
 Remove process from memory, store on disk, bring back in from
disk to continue execution: swapping
OS Concepts – 10th Edition 3.15 Silberschatz, Galvin and Gagne ©2018
3
1 6
new ready running terminat.

5 4

waiting  1 = some predefined policy

 2 = some predefined algorithm

 1 = job scheduler  3 = time interrupt or priority interrupt

 4 = initiated by an I/O or page request


 2,3,4,5 = process scheduler instructions

 6 = process scheduler (or job  5 = initiated by a signal from the I/O device
manager, or the page fault handler
scheduler)
 6 = termination (including releasing resources)
could be either natural or by force

OS Concepts – 10th Edition 3.16 Silberschatz, Galvin and Gagne ©2018


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
via a context switch

 Context of a process represented in the PCB

 Context-switch time is overhead; the system does no useful work while


switching
 The more complex the OS and the PCB, the longer the context
switch (the more work must be done during a context switch)

OS Concepts – 10th Edition 3.17 Silberschatz, Galvin and Gagne ©2018


CPU Switching Processes

OS Concepts – 10th Edition 3.18 Silberschatz, Galvin and Gagne ©2018


Context Switch

P1 CPU scheduler P2

Interrupt
Running Save state P1 Ready
Select P2

Restore state P2

Ready Running
Interrupt

Save state P2

Select P1

Restore state P1
Running Ready

OS Concepts – 10th Edition 3.19 Silberschatz, Galvin and Gagne ©2018


Operations on Processes

OS Concepts – 10th Edition 3.20 Silberschatz, Galvin and Gagne ©2018


Operations on Processes

 System must provide mechanisms for:


 Process creation
 Process termination
 …

OS Concepts – 10th Edition 3.21 Silberschatz, Galvin and Gagne ©2018


Process Creation

 Parent process creates children processes, which, in turn create


other processes, forming a tree of processes

 Generally, process identified and managed via a process identifier


(pid)

Examples:
 Resource sharing options
 Parent and children share all resources • Shell & user
programs
 Children share subset of parent’s resources
 Parent and child share no resources • Web browser

• Web server
 Execution options
 Parent and children execute concurrently (e.g., Web Server)
 Parent waits until children terminate (e.g., Batch Processing)

OS Concepts – 10th Edition 3.22 Silberschatz, Galvin and Gagne ©2018


A Tree of Processes in Linux

i ni t
pi d = 1

l ogi n kt hr e add s s hd
pi d = 8415 pi d = 2 pi d = 3028

bas h khe l pe r pdf l us h s s hd


pi d = 8416 pi d = 6 pi d = 200 pi d = 3610

e mac s t cs ch
ps
pi d = 9204 pi d = 4005
pi d = 9298

OS Concepts – 10th Edition 3.23 Silberschatz, Galvin and Gagne ©2018


Process Creation (Cont.)
 Address space
 Child duplicate of parent (has the same program); e.g.,
Notepad++
 Child has a program loaded into it; e.g., Web Server (parent) and
child process handling database interactions

 UNIX examples
 fork() system call creates new process
 exec() system call used after a fork() to replace the process’
memory space with a new program

OS Concepts – 10th Edition 3.24 Silberschatz, Galvin and Gagne ©2018


Process Termination

 Process executes last statement and then asks the OS to delete it


using the exit() system call.
 Returns status data from child to parent (via wait())
 Process’resources are deallocated by OS

 Parent may terminate the execution of children processes using the


abort() system call. Some reasons for doing so:
 Child has exceeded allocated resources
 Task assigned to child is no longer required
 The parent is exiting, and the OS does not allow a child to continue
if its parent terminates

OS Concepts – 10th Edition 3.25 Silberschatz, Galvin and Gagne ©2018


Process Termination (Cont.)

 Some OS’s do not allow child to exists if its parent has terminated. If
a process terminates, then all its children must also be terminated.
 Cascading termination: All children, grandchildren, etc. are
terminated.
 The termination is initiated by the OS

 The parent process may wait for termination of a child process by


using the wait()system call. The call returns status information
and the pid of the terminated process
pid = wait(&status);

 If no parent waiting (did not invoke wait() yet), terminated process


is called a zombie

 If parent terminated without invoking wait, process is an orphan

OS Concepts – 10th Edition 3.26 Silberschatz, Galvin and Gagne ©2018


Chrome Browser

 Many web browsers ran as a single process (some still do)


 If one website causes trouble, entire browser can hang or crash

 Google Chrome is a multi-process browser with 3 different types of


processes:
 Browser process manages user interface, disk and network I/O
 Renderer process renders web pages, deals with HTML,
Javascript. A new renderer created for each website opened
 Runs in sandbox restricting disk and network I/O, minimizing
effect of security exploits
 Plug-in process for each type of plug-in

OS Concepts – 10th Edition 3.27 Silberschatz, Galvin and Gagne ©2018


Inter-process Communication

OS Concepts – 10th Edition 3.28 Silberschatz, Galvin and Gagne ©2018


Inter-process Communication (IPC)

 Processes within a system may be independent or cooperating


 Cooperating process can affect or be affected by other processes,
including sharing data
 Reasons for cooperating processes:
 Information sharing
 Computation speedup
 Modularity
 Convenience
 Cooperating processes need interprocess communication (IPC)
 Two models of IPC
 Shared memory
 Message passing

OS Concepts – 10th Edition 3.29 Silberschatz, Galvin and Gagne ©2018


Communications Models
(a) Message passing. (b) shared memory.

OS Concepts – 10th Edition 3.30 Silberschatz, Galvin and Gagne ©2018


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

OS Concepts – 10th Edition 3.31 Silberschatz, Galvin and Gagne ©2018


IPC – Shared Memory

 An area of memory is shared among the processes that wish to


communicate

 The communication is under the control of the users processes not


the OS

 Major issues is to provide mechanism that will allow the user


processes to synchronize their actions when they access shared
memory

 Synchronization is discussed in great details in Chapter 6

OS Concepts – 10th Edition 3.32 Silberschatz, Galvin and Gagne ©2018


IPC – Message Passing

 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:


 send(message)
 receive(message)

 The message size is either fixed or variable

OS Concepts – 10th Edition 3.33 Silberschatz, Galvin and Gagne ©2018


IPC – Message Passing (Cont.)

 If processes P and Q wish to communicate, they need to:


 Establish a communication link between them
 Exchange messages via send/receive

 Implementation issues:
 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?

OS Concepts – 10th Edition 3.34 Silberschatz, Galvin and Gagne ©2018


IPC – Message Passing (Cont.)
 Implementation of communication link

 Physical:
 Shared memory
 Hardware bus
 Network

 Logical:
 Direct or indirect
 Synchronous or asynchronous
 Automatic or explicit buffering

OS Concepts – 10th Edition 3.35 Silberschatz, Galvin and Gagne ©2018


Direct Communication
 Processes must name each other explicitly:
 send (P, message) – send a message to process P
 receive(Q, message) – receive a message from process 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

OS Concepts – 10th Edition 3.36 Silberschatz, Galvin and Gagne ©2018


Indirect Communication

 Messages are directed and received via mailboxes (also referred to as


ports)
 Each mailbox has a unique 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

OS Concepts – 10th Edition 3.37 Silberschatz, Galvin and Gagne ©2018


Indirect Communication (Cont.)
 Operations
 create a new mailbox (port)
 send and receive messages through mailbox
 destroy a mailbox

 Primitives are defined as:


send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A

OS Concepts – 10th Edition 3.38 Silberschatz, Galvin and Gagne ©2018


Indirect Communication (Cont.)
 Mailbox sharing
 P1, P2, and P3 share mailbox A
 P1, sends; P2 and P3 receive
 Who gets the message?

 Solutions
 Allow a link to be associated with at most two processes
 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.

OS Concepts – 10th Edition 3.39 Silberschatz, Galvin and Gagne ©2018


Synchronization
 Message passing may be either blocking or non-blocking

 Blocking is considered synchronous


 Blocking send -- the sender is blocked until the message is
received
 Blocking receive -- the receiver is blocked until a message is
available

 Non-blocking is considered asynchronous


 Non-blocking send -- the sender sends the message and continue
 Non-blocking receive -- the receiver receives:
 A valid message or null message

 Different combinations possible


 If both send and receive are blocking, we have a rendezvous

OS Concepts – 10th Edition 3.40 Silberschatz, Galvin and Gagne ©2018


Buffering

 Queue of messages attached to the link.

 Implemented in one of three ways

1. Zero capacity – no messages are queued on a link.


Sender must wait for receiver (rendezvous)

2. Bounded capacity – finite length of n messages


Sender must wait if the link is full

3. Unbounded capacity – infinite length


Sender never waits

OS Concepts – 10th Edition 3.41 Silberschatz, Galvin and Gagne ©2018


Communications in Client-Server
Systems

OS Concepts – 10th Edition 3.42 Silberschatz, Galvin and Gagne ©2018


Communications in C/S Systems

 Sockets
 Remote Procedure Calls (RPC)
 Pipes

OS Concepts – 10th Edition 3.43 Silberschatz, Galvin and Gagne ©2018


Sockets

 A socket is defined as an endpoint for communication

 Concatenation of IP address and port – a number included at start of


message packet to differentiate network services on a host

 The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8

 Communication is between a pair of sockets

 All ports below 1024 are well known, used for standard services

 Special IP address 127.0.0.1 (loopback) to refer to system on which


process is running

OS Concepts – 10th Edition 3.44 Silberschatz, Galvin and Gagne ©2018


Socket Communication

OS Concepts – 10th Edition 3.45 Silberschatz, Galvin and Gagne ©2018


Remote Procedure Calls (RPC)
 Remote procedure call (RPC) abstracts procedure calls between
processes on networked systems
 Again, uses ports for service differentiation

 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 performs the procedure on the server

 On Windows, stub code compile from specification written in


Microsoft Interface Definition Language (MIDL)

OS Concepts – 10th Edition 3.46 Silberschatz, Galvin and Gagne ©2018


Remote Procedure Calls (RPC)

OS Concepts – 10th Edition 3.47 Silberschatz, Galvin and Gagne ©2018


Pipes

 Acts as a conduit (or channel) allowing two processes to communicate

 Issues:
 Is communication unidirectional or bidirectional?
 In the case of two-way communication, is it half or full-duplex?
 Must there exist a relationship (i.e., parent-child) between the
communicating processes?
 Can the pipes be used over a network?

 Ordinary pipes – cannot be accessed from outside the process that


created it. Typically, a parent process creates a pipe and uses it to
communicate with a child process that it created.

 Named pipes – can be accessed without a parent-child relationship.

OS Concepts – 10th Edition 3.48 Silberschatz, Galvin and Gagne ©2018


Pipes (Unix Examples)

 Ordinary pipes

$ Ls | grep “examples”

 Named (or fifo) pipes

$ mkfifo named_pipe_example
$ cat named_pipe_example

$ echo "Message from T2" > named_pipe_example

OS Concepts – 10th Edition 3.49 Silberschatz, Galvin and Gagne ©2018


End of Chapter 3

OS Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018


Chapter 4: Threads &
Concurrency

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Objectives
 To introduce the notion of a thread
 To examine issues related to multithreaded programming
 To cover operating system support for threads

Operating System Concepts – 10th Edition 4.2 Silberschatz, Galvin and Gagne ©2018
Concurrency and Parallelism

Operating System Concepts – 10th Edition 4.3 Silberschatz, Galvin and Gagne ©2018
Concurrency and Parallelism
 Parallelism implies that a system can perform more than one task
simultaneously

 Concurrency supports more than one task making progress


 Single processor / core, scheduler providing concurrency

 Multi-core or multiprocessor systems putting pressure on


programmers, challenges include:
 Dividing activities
 Balance
 Data splitting
 Data dependency
 Testing and debugging

Operating System Concepts – 10th Edition 4.4 Silberschatz, Galvin and Gagne ©2018
Concurrency vs. Parallelism
 Concurrent execution on single-core system:

 Parallelism on a multi-core system:

Operating System Concepts – 10th Edition 4.5 Silberschatz, Galvin and Gagne ©2018
Types of Parallelism
 Data parallelism – distributes subsets of the same data across multiple
cores, same operation on each

 Task parallelism – distributing threads across cores, each thread


performing unique operation

Operating System Concepts – 10th Edition 4.6 Silberschatz, Galvin and Gagne ©2018
Amdahl’s Law
 Identifies performance gains from adding additional cores to an
application that has both serial and parallel components
 S is serial portion
 N processing cores

 Example: If application is 75% parallel / 25% serial, moving from 1


to 2 cores results in speedup of 1.6 times

 As N approaches infinity, speedup approaches 1 / S

Operating System Concepts – 10th Edition 4.7 Silberschatz, Galvin and Gagne ©2018
Introduction to Threads and
Multithreading

Operating System Concepts – 10th Edition 4.8 Silberschatz, Galvin and Gagne ©2018
What is a thread?
 Traditional (single-threaded) process has a single thread of control.

 A thread is a basic unit of CPU utilization


 Also called a “lightweight process (LWP)”  low creation overhead

 The thread shares with other threads (belonging to the same process) its
code section, data section, and other OS resources, such as open files.

 A process with multiple threads can do more than one task at a time.

 What resources are used when a thread is created? How do they differ
from those used when a process is created?

 What two advantages do threads have over multiple processes? What


major disadvantage do they have?
Operating System Concepts – 10th Edition 4.9 Silberschatz, Galvin and Gagne ©2018
Single and Multithreaded (MT)
Processes

Operating System Concepts – 10th Edition 4.10 Silberschatz, Galvin and Gagne ©2018
Examples of MT Applications
 Web browser

 Word processor

 Web-server
 Problem?
 Two possible solutions

 Suggest another application that would benefit from the use of threads,
and an application that would not.

Operating System Concepts – 10th Edition 4.11 Silberschatz, Galvin and Gagne ©2018
Multithreaded Server Architecture

Operating System Concepts – 10th Edition 4.12 Silberschatz, Galvin and Gagne ©2018
Benefits of MT Applications
 Responsiveness – may allow continued execution if part of
process is blocked, especially important for user interfaces

 Resource Sharing – threads share resources of process, easier


than shared memory or message passing

 Economy – cheaper than process creation, thread switching


lower overhead than context switching

 Scalability – process can take advantage of multiprocessor


architectures

Operating System Concepts – 10th Edition 4.13 Silberschatz, Galvin and Gagne ©2018
ST Example
def calculate_sum_single_thread(N):
total = 0
for i in range(1, N + 1):
total += i
return total

N = 10000
result = calculate_sum_single_thread(N)
print({result})

Operating System Concepts – 10th Edition 4.14 Silberschatz, Galvin and Gagne ©2018
MT Example
import threading

def calculate_sum_partial(N, start, end, result):


partial_sum = 0
for i in range(start, end + 1):
partial_sum += i
result.append(partial_sum)

N = 10000
thread1_result = [ ]
thread2_result = [ ]

Operating System Concepts – 10th Edition 4.15 Silberschatz, Galvin and Gagne ©2018
MT Example (Cont.)
# Split the work into two threads
half_N = N / 2
thread1 = threading.Thread(target=calculate_sum_partial, args=(N,
1, half_N, thread1_result))
thread2 = threading.Thread(target=calculate_sum_partial, args=(N,
half_N + 1, N, thread2_result))

thread1.start()
thread2.start()
thread1.join()
thread2.join()

# Combine results from both threads


total_sum = thread1_result[0] + thread2_result[0]
print(total_sum)
Operating System Concepts – 10th Edition 4.16 Silberschatz, Galvin and Gagne ©2018
User Threads and Kernel Threads

Operating System Concepts – 10th Edition 4.17 Silberschatz, Galvin and Gagne ©2018
User Threads
 Implemented by a thread library at the user level (above the kernel)
 The library provides support for thread creation, scheduling, and
management.
 The programmer of the library writes code to synchronize threads and to
context switch them, and they all run in one process.
 Kernel does not provide any support; it is unaware that user-level
threads are even running.
 Fast to create and manage (i.e., high efficiency), mainly because the
kernel is not involved.
 What if the kernel is single-threaded?
 Three primary thread libraries: POSIX Pthreads, Windows threads, Java
threads

Operating System Concepts – 10th Edition 4.18 Silberschatz, Galvin and Gagne ©2018
Kernel Threads
 Supported directly by the OS
 The kernel performs thread creation, scheduling, and management
in kernel space.
 Slower to create and manage than user threads and have more
overhead in the kernel.
 If a thread performs a blocking system call, the kernel can schedule
another thread in the application for execution.
 In a parallel environment, the kernel can schedule threads on different
processors.
 Examples – virtually all general-purpose OS’s, including Windows,
Solaris, Linux, Mac OS X

Operating System Concepts – 10th Edition 4.19 Silberschatz, Galvin and Gagne ©2018
Multithreading Models

Operating System Concepts – 10th Edition 4.20 Silberschatz, Galvin and Gagne ©2018
MT Models
 Many-to-One

 One-to-One

 Many-to-Many

Operating System Concepts – 10th Edition 4.21 Silberschatz, Galvin and Gagne ©2018
Many-to-One MT
 Many user-level threads mapped to
single kernel thread
 One thread blocking causes all to block
 Multiple threads may not run in parallel
on muti-core system because only one
may be in kernel at a time
 Used in systems that do not support
kernel threads
 Few systems currently use this model
 Examples:
 Solaris Green Threads
 GNU Portable Threads

Operating System Concepts – 10th Edition 4.22 Silberschatz, Galvin and Gagne ©2018
One-to-One MT
 Each user-level thread maps to a kernel thread

 Creating a user-level thread creates a kernel thread

 More concurrency than many-to-one


 allows a thread to run when another thread makes a blocking
system call
 allows multiple threads to run in parallel on microprocessors

 Examples:
 Windows
 Linux
 Solaris 9 and later

Operating System Concepts – 10th Edition 4.23 Silberschatz, Galvin and Gagne ©2018
Many-to-Many MT
 Allows many user-level threads to be
mapped to many kernel threads

 Allows the operating system to create


a sufficient number of kernel threads

 Addresses the limitations of one-to-


many and one-to-one models

 Examples:
 Solaris prior to version 9
 Windows with the ThreadFiber
package

Operating System Concepts – 10th Edition 4.24 Silberschatz, Galvin and Gagne ©2018
Two-Level MT
 Similar to the many-to-many model, except that it allows a
user thread to be bound to kernel thread

 Examples
 IRIX
 HP-UX
 Tru64 UNIX
 Solaris 8 and earlier

Operating System Concepts – 10th Edition 4.25 Silberschatz, Galvin and Gagne ©2018
MT Models: Summary
 Many-to-one
 The developer can create as many user threads as he wishes
 But the true concurrency is not gained because the kernel can
schedule only one thread at a time

 One-to-one
 Greater concurrency than many-to-one
 But the developer has to be careful (and in some instances be
limited) not to create too many threads within an application

 Many-to-many
 Does not suffer from the above shortcomings
 Developers can create as many user threads as necessary, and the
corresponding kernel threads can run in parallel on a multiprocessor

Operating System Concepts – 10th Edition 4.26 Silberschatz, Galvin and Gagne ©2018
End of Chapter 4

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Chapter 5: CPU Scheduling

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018
Objectives

 To introduce CPU scheduling, which is the basis for multi-programmed


OS’s

 To describe various CPU scheduling algorithms

 To discuss evaluation criteria for selecting a CPU scheduling algorithm


for a particular system

Operating System Concepts – 10th Edition 5.2 Silberschatz, Galvin and Gagne ©2018
Basic Concepts

Operating System Concepts – 10th Edition 5.3 Silberschatz, Galvin and Gagne ©2018
Basic Concepts

 Maximum CPU utilization


obtained with multiprogramming

 CPU–I/O Burst Cycle: Process


execution consists of a cycle of
CPU execution and I/O wait

 CPU burst followed by I/O burst

 CPU burst distribution is of main


concern

Operating System Concepts – 10th Edition 5.4 Silberschatz, Galvin and Gagne ©2018
Histogram of CPU-burst Times

Operating System Concepts – 10th Edition 5.5 Silberschatz, Galvin and Gagne ©2018
CPU Scheduler

 Short-term (or CPU) scheduler selects from the processes in the


ready queue, and allocates the CPU to one of them
 Queue may be ordered in various ways

 Process scheduling decisions may take place when a process:


1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
2
4. Terminates 4
new ready running terminat.

***

3 1
 Scheduling in 1 & 4 is non-preemptive
waiting
 All other scheduling is preemptive

Operating System Concepts – 10th Edition 5.6 Silberschatz, Galvin and Gagne ©2018
Scheduling and Optimization Criteria

Operating System Concepts – 10th Edition 5.7 Silberschatz, Galvin and Gagne ©2018
Scheduling Criteria

 CPU utilization – keep the CPU as busy as possible

 Throughput – # of processes that complete their execution per time


unit

 Turnaround time – amount of time to execute a particular process

 Waiting time – amount of time a process has been waiting in the ready
queue

 Response time – amount of time it takes from when a request was


submitted until the first response is produced

Operating System Concepts – 10th Edition 5.8 Silberschatz, Galvin and Gagne ©2018
Optimization Criteria

 Maximize:
 CPU utilization
 Throughput

 Minimize:
 Turnaround time
 Waiting time
 Response time

Operating System Concepts – 10th Edition 5.9 Silberschatz, Galvin and Gagne ©2018
CPU Scheduling Algorithms

Operating System Concepts – 10th Edition 5.10 Silberschatz, Galvin and Gagne ©2018
CPU Scheduling Algorithms

 Non-preemptive:
 First-come, first-served (FCFS)
 Shortest job first (SJF)
 Priority

 Preemptive:
 Shortest remaining time first (SRTF)
 Priority
 Round robin

Operating System Concepts – 10th Edition 5.11 Silberschatz, Galvin and Gagne ©2018
First-Come, First-Served (FCFS)

Process Burst Time


P1 24
P2 3
P3 3
 Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

P1 P2 P3
0 24 27 30

 Waiting time for P1 = 0; P2 = 24; P3 = 27


 Average waiting time: (0 + 24 + 27)/3 = 17
 Average turnaround time = ((24-0) + (27-0) + (30-0)) / 3 = …

Operating System Concepts – 10th Edition 5.12 Silberschatz, Galvin and Gagne ©2018
FCFS (Cont.)

Suppose that the processes arrive in the order:


P 2 , P3 , P 1
 The Gantt chart for the schedule is:

P2 P3 P1
0 3 6 30

 Waiting time for P1 = 6; P2 = 0; P3 = 3


 Average waiting time: (6 + 0 + 3)/3 = 3
 Average turnaround time = ((30-0) + (3-0) + (6-0)) / 3
 Much better than previous case
 Convoy effect - short process behind long process
 Consider one CPU-bound and many I/O-bound processes

Operating System Concepts – 10th Edition 5.13 Silberschatz, Galvin and Gagne ©2018
Shortest Job First (SJF)

 Also called “Shortest Job Next (SJN)”

 Associate with each process the length of its next CPU burst
 Use these lengths to schedule the process with the shortest
time

 SJF is optimal – gives minimum average waiting time for a given


set of processes
 The difficulty is knowing the length of the next CPU request

Operating System Concepts – 10th Edition 5.14 Silberschatz, Galvin and Gagne ©2018
Example of SJF

ProcessArrival Time Burst Time


P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3

 SJF scheduling chart

P4 P1 P3 P2
0 3 9 16 24

 Average waiting time = (3 + 16 + 9 + 0) / 4 = 7


 Average turnaround time = (9 + 24 + 16 + 3) / 4

Operating System Concepts – 10th Edition 5.15 Silberschatz, Galvin and Gagne ©2018
Example of SJF

ProcessArrival Time Burst Time Arrival Time


P1 0.0 6 2
P2 2.0 8 2
P3 4.0 7 3
P4 5.0 3 3

 SJF scheduling chart

 Average waiting time = ((2-2) + (18-2) + (11-3) + (8-3)) / 4 =


 Average turnaround time = ((8-2) + (26-2) + (18-3) + (11-3)) / 4

Operating System Concepts – 10th Edition 5.16 Silberschatz, Galvin and Gagne ©2018
Predicting the Next CPU Burst

 Can only estimate the length – should be similar to the previous one
 Then pick process with shortest predicted next CPU burst

 Can be done by using the length of previous CPU bursts, using


exponential averaging

1. t n  actual length of n th CPU burst


2.  n 1  predicted value for the next CPU burst
3.  , 0    1
4. Define :  n 1   t n  1    n .

 Commonly, α set to ½

Operating System Concepts – 10th Edition 5.17 Silberschatz, Galvin and Gagne ©2018
Predicting the Next CPU Burst

Operating System Concepts – 10th Edition 5.18 Silberschatz, Galvin and Gagne ©2018
Shortest Remaining Time First (SRT)

 Now we add the concepts of varying arrival times and preemption to


the analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
 Preemptive SJF Gantt Chart

P1 P2 P4 P1 P3
0 1 5 10 17 26

 Average waiting time = [((0-0)+(10-1))+(1-1)+(17-2)+(5-3)]/4 = 26/4 =


6.5
 Average turnaround time = [(17-0) + (5-1) + (26-2) + (10-3)] / 4 =

Operating System Concepts – 10th Edition 5.19 Silberschatz, Galvin and Gagne ©2018
Priority Scheduling

 A priority number (integer) is associated with each process

 The CPU is allocated to the process with the highest priority


(smallest integer  highest priority)
 Preemptive
 Non-preemptive

 SJF is priority scheduling where priority is the inverse of predicted


next CPU burst time

 Problem  Starvation – low priority processes may never execute

 Solution  Aging – as time progresses increase the priority of the


process

Operating System Concepts – 10th Edition 5.20 Silberschatz, Galvin and Gagne ©2018
Example of Priority Scheduling

ProcessA arri Burst TimeT Priority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2

 Priority scheduling Gantt Chart

 Average waiting time = [(6-0) + (0-0) + (16-0) + (18-0) + (1-0)] / 5


 Average turnaround time = [(16-0) + (1-0) + (18-0) + (19-0) + (6-0)] / 5

Operating System Concepts – 10th Edition 5.21 Silberschatz, Galvin and Gagne ©2018
Example of Preemptive Priority
Scheduling

Process AT Burst Time Priority


P1 1 10 3
P2 2 1 1
P3 7 2 3
P4 22 5 4
P5 23 5 2

Operating System Concepts – 10th Edition 5.22 Silberschatz, Galvin and Gagne ©2018
Example of Preemptive Priority
Scheduling

Process AT
P1 1
P2 2
P3 7
P4 22
P5 23

Operating System Concepts – 10th Edition 5.23 Silberschatz, Galvin and Gagne ©2018
Round Robin (RR)

 Each process gets a small unit of CPU time (time quantum q), usually
10-100 milliseconds. After this time has elapsed, the process is
preempted and added to the end of the ready queue.

 If there are n processes in the ready queue and the time quantum is q,
then each process gets 1/n of the CPU time in chunks of at most q time
units at once. No process waits more than (n-1)q time units.

 Timer interrupts every quantum to schedule next process

 Performance
 q large  FIFO
 q small  q must be large with respect to context switch, otherwise
overhead is too high

Operating System Concepts – 10th Edition 5.24 Silberschatz, Galvin and Gagne ©2018
Example of RR with q = 4

Process Burst Time


P1 24
P2 3
P3 3
 The Gantt chart is:

P1 P2 P3 P1 P1 P1 P1 P1
0 4 7 10 14 18 22 26 30

 Typically, higher average turnaround than SJF, but better response


 q should be large compared to context switch time
 q usually 10ms to 100ms, context switch < 10 usec

Operating System Concepts – 10th Edition 5.25 Silberschatz, Galvin and Gagne ©2018
“q” & Context Switch Time

Operating System Concepts – 10th Edition 5.26 Silberschatz, Galvin and Gagne ©2018
Turnaround Time Varies with “q”

80% of CPU bursts


should be shorter than q

Operating System Concepts – 10th Edition 5.27 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue
 Ready queue is partitioned into separate queues, e.g.:
 foreground (interactive)
 background (batch)

 Each queue has its own scheduling algorithm, e.g.:


 foreground – RR
 background – FCFS

 Scheduling must be done between the queues:


 Fixed priority scheduling; (i.e., serve all from foreground then from
background). Possibility of starvation.
 Time slice – each queue gets a certain amount of CPU time which it
can schedule amongst its processes; e.g., 80% to foreground in RR
& 20% to background in FCFS

Operating System Concepts – 10th Edition 5.28 Silberschatz, Galvin and Gagne ©2018
Multilevel Queue

Operating System Concepts – 10th Edition 5.29 Silberschatz, Galvin and Gagne ©2018
Multilevel Feedback Queue

 A process can move between the various queues


 Aging can be implemented this way

 Multilevel-feedback-queue scheduler defined by the following


parameters:
 number of queues
 scheduling algorithms for each queue
 method used to determine when to upgrade a process
 method used to determine when to demote a process
 method used to determine which queue a process will enter when
that process needs service

Operating System Concepts – 10th Edition 5.30 Silberschatz, Galvin and Gagne ©2018
Example of Multilevel Feedback Queue

 Three queues:
 Q0 – RR with time quantum 8
milliseconds
 Q1 – RR time quantum 16 milliseconds
 Q2 – FCFS

 Scheduling
 A new job enters queue Q0 which is
served FCFS
 When it gains CPU, job receives 8
milliseconds
 If it does not finish in 8
milliseconds, job is moved to
queue Q1
 At Q1 job is again served FCFS and
receives 16 additional milliseconds
 If it still does not complete, it is
preempted and moved to queue Q2

Operating System Concepts – 10th Edition 5.31 Silberschatz, Galvin and Gagne ©2018
Operating System Concepts – 10th Edition 5.32 Silberschatz, Galvin and Gagne ©2018
Thread Scheduling

Operating System Concepts – 10th Edition 5.33 Silberschatz, Galvin and Gagne ©2018
Thread Scheduling

 Distinction between user-level and kernel-level threads

 Many-to-one and many-to-many models, thread library schedules user-


level threads to run on LWP
 Known as process-contention scope (PCS) since scheduling
competition is within the process
 Typically done via priority set by programmer

 Kernel thread scheduled onto available CPU is system-contention


scope (SCS) – competition among all threads in system

Operating System Concepts – 10th Edition 5.34 Silberschatz, Galvin and Gagne ©2018
Multiple-Processor Scheduling

Operating System Concepts – 10th Edition 5.35 Silberschatz, Galvin and Gagne ©2018
Multiple-Processor (MP) Scheduling
 CPU scheduling is more complex when multiple CPUs are available
 Homogeneous processors within a multiprocessor
 Asymmetric multiprocessing – only one processor accesses the
system data structures, alleviating the need for data sharing
 Symmetric multiprocessing (SMP) – each processor is self-
scheduling, all processes in common ready queue, or each has its own
private queue of ready processes
 Currently, most common
 Processor affinity – process has affinity for processor on which it is
currently running
 soft affinity
 hard affinity
 Variations including processor sets

Operating System Concepts – 10th Edition 5.36 Silberschatz, Galvin and Gagne ©2018
MP Scheduling – Load Balancing

 If SMP, need to keep all CPUs loaded for efficiency

 Load balancing attempts to keep workload evenly distributed

 Push migration – periodic task checks load on each processor, and if


found pushes task from overloaded CPU to other CPUs

 Pull migration – idle processors pulls waiting task from busy processor

Operating System Concepts – 10th Edition 5.37 Silberschatz, Galvin and Gagne ©2018
Multicore Processors

 Recent trend to place multiple processor cores on same physical chip

 Faster and consumes less power

 Multiple threads per core also growing


 Takes advantage of memory stall to make progress on another
thread while memory retrieve happens

Operating System Concepts – 10th Edition 5.38 Silberschatz, Galvin and Gagne ©2018
Multithreaded Multicore System

Operating System Concepts – 10th Edition 5.39 Silberschatz, Galvin and Gagne ©2018
Real-Time CPU Scheduling

Operating System Concepts – 10th Edition 5.40 Silberschatz, Galvin and Gagne ©2018
Real-Time CPU Scheduling

 Can present obvious


challenges
 Soft real-time systems – no
guarantee as to when critical
real-time process will be
scheduled
 Hard real-time systems –
task must be serviced by its
deadline
 Two types of latencies affect
performance
1. Interrupt latency – time from
arrival of interrupt to start of
routine that services interrupt
2. Dispatch latency – time for
scheduler to take current process
off CPU and switch to another

Operating System Concepts – 10th Edition 5.41 Silberschatz, Galvin and Gagne ©2018
Real-Time CPU Scheduling (Cont.)

 Rate-Montonic Scheduling

 A priority is assigned based on the inverse of the period

 Shorter periods = higher priority

 Longer periods = lower priority

 P1 is assigned a higher priority than P2

Operating System Concepts – 10th Edition 5.42 Silberschatz, Galvin and Gagne ©2018
Missed Deadlines with
Rate-Monotonic Scheduling

Operating System Concepts – 10th Edition 5.43 Silberschatz, Galvin and Gagne ©2018
Real-Time CPU Scheduling (Cont.)

 Earliest Deadline First Scheduling (EDF)


 Priorities are assigned according to deadlines:
 the earlier the deadline, the higher the priority;
 the later the deadline, the lower the priority

Operating System Concepts – 10th Edition 5.44 Silberschatz, Galvin and Gagne ©2018
Real-Time CPU Scheduling (Cont.)

 Proportional Share Scheduling

 T shares are allocated among all processes in the system


 An application receives N shares where N < T
 This ensures each application will receive N / T of the total
processor time

Operating System Concepts – 10th Edition 5.45 Silberschatz, Galvin and Gagne ©2018
Evaluation of Scheduling Algorithms

Operating System Concepts – 10th Edition 5.46 Silberschatz, Galvin and Gagne ©2018
Algorithm Evaluation
 How to select CPU-scheduling algorithm for an OS?

 Determine criteria, then evaluate algorithms

 Deterministic modeling
 Type of analytic evaluation
 Takes a particular predetermined workload and defines the
performance of each algorithm for that workload

 Consider 5 processes arriving at time 0:

Operating System Concepts – 10th Edition 5.47 Silberschatz, Galvin and Gagne ©2018
Deterministic Evaluation

 For each algorithm, calculate minimum average waiting time


 Simple and fast, but requires exact numbers for input

 FCS is 28ms:

 Non-preemptive SFJ is 13ms:

 RR is 23ms:

Operating System Concepts – 10th Edition 5.48 Silberschatz, Galvin and Gagne ©2018
Queueing Models

 Describes the arrival of processes, and CPU and I/O bursts


probabilistically
 Commonly exponential, and described by mean
 Computes average throughput, utilization, waiting time, etc.

 Computer system described as network of servers, each with queue of


waiting processes
 Knowing arrival rates and service rates
 Computes utilization, average queue length, average wait time, etc.

Operating System Concepts – 10th Edition 5.49 Silberschatz, Galvin and Gagne ©2018
Little’s Formula

 n = average queue length


 W = average waiting time in queue
 λ = average arrival rate into queue

 Little’s law – in steady state, processes leaving queue must equal


processes arriving, thus:
n=λxW
 Valid for any scheduling algorithm and arrival distribution

 For example, if on average 7 processes arrive per second, and


normally 14 processes in queue, then average wait time per process =
2 seconds

Operating System Concepts – 10th Edition 5.50 Silberschatz, Galvin and Gagne ©2018
Simulations

 Queueing models are limited

 Simulations are more accurate


 Programmed model of computer system
 Clock is a variable
 Gather statistics indicating algorithm performance
 Data to drive simulation gathered via
 Random number generator according to probabilities
 Distributions defined mathematically or empirically
 Trace tapes record sequences of real events in real systems

Operating System Concepts – 10th Edition 5.51 Silberschatz, Galvin and Gagne ©2018
Simulations (Cont.)

Operating System Concepts – 10th Edition 5.52 Silberschatz, Galvin and Gagne ©2018
Implementation

 Even simulations have limited accuracy

 Just implement new scheduler and test in real systems


 High cost, high risk
 Environments vary

Operating System Concepts – 10th Edition 5.53 Silberschatz, Galvin and Gagne ©2018
End of Chapter 5

Operating System Concepts – 10th Edition Silberschatz, Galvin and Gagne ©2018

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy