operating systems - introduction, processes, threads
TRANSCRIPT
![Page 1: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/1.jpg)
Operating Systems
Introduction, Processes, Threads
Daniel Gruss
2021-03-02
![Page 2: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/2.jpg)
Table of contents
1. Basics
2. Booting
3. Process and Thread Fundamentals
4. Context Switches
5. Process and Thread Organization
![Page 3: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/3.jpg)
![Page 4: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/4.jpg)
Basics
![Page 5: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/5.jpg)
What is an Operating System
![Page 6: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/6.jpg)
What is an Operating System
![Page 7: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/7.jpg)
What is an Operating System
![Page 8: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/8.jpg)
What is an Operating System
� Run on all sorts of devices:
� Servers, Desktops, Notebooks
� Tablets, Smartphones
� Routers, Switches, Displays
� Door Locks, Washing Machines, Toasters
� Cars, Airplanes
� ....
� We focus on general purpose operating systems
![Page 9: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/9.jpg)
Operating System Roles
� Referee
� Illusionist
� Glue
![Page 10: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/10.jpg)
Operating System Roles: Referee
Referee
� Manage resource allocation among users and applications
� Isolation of different users, applications from each other
� Isolation of applications and operating system
� Communication between users, application
![Page 11: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/11.jpg)
Operating System Roles: Illusionist
Illusionist
� Abstraction of hardware and other things
� Makes application design simpler
� Each application appears to have the entire machine to itself
� Illusion of
� Infinite number of processors
� (near) infinite amount of memory
� reliable storage
� reliable network transport
![Page 12: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/12.jpg)
Operating System Roles: Glue
Glue
� Libraries, user interface widgets, . . .
![Page 13: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/13.jpg)
Example: File Systems
� Referee
� Prevent users from accessing each other’s files without permission
� Even after a file is deleted and its space re-used
� Illusionist
� Files can grow (nearly) arbitrarily large
� Files persist even when the machine crashes in the middle of a save
![Page 14: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/14.jpg)
Example: File Systems
� Glue
� Nam ed directories, printf, . . .
![Page 15: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/15.jpg)
OS Design Patterns I
� OS challenges are not unique - apply to many different computing domains
� many complex software systems
� have multiple users
� run programs written by third-party developers
� need to coordinate simultaneous activities
![Page 16: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/16.jpg)
OS Design Patterns II
Challenges:
� resource allocation
� fault isolation
� communication
� abstraction
� how to provide a set of common services
![Page 17: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/17.jpg)
Design Criteria for OS
Design Criteria for Operating Systems
� Reliability and Availability
� Security
� Portability
� Performance
� Adoption
![Page 18: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/18.jpg)
Design Criteria for OS
Design Criteria for Operating Systems
� Reliability and Availability
� Security
� Portability
� Performance
� Adoption
![Page 19: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/19.jpg)
Design Criteria for OS
Design Criteria for Operating Systems
� Reliability and Availability
� Security
� Portability
� Performance
� Adoption
![Page 20: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/20.jpg)
Design Criteria for OS
Design Criteria for Operating Systems
� Reliability and Availability
� Security
� Portability
� Performance
� Adoption
![Page 21: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/21.jpg)
Design Criteria for OS
Design Criteria for Operating Systems
� Reliability and Availability
� Security
� Portability
� Performance
� Adoption
![Page 22: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/22.jpg)
Design Criteria for OS
Design Criteria for Operating Systems
� Reliability and Availability
� Security
� Portability
� Performance
� Adoption
![Page 23: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/23.jpg)
Reliability and Availability
Reliability
� It does, what it is designed to do
� Application failures can be “ok” - OS provides fault isolation and clean restart after errors
� OS errors are bad, real bad...
![Page 24: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/24.jpg)
Reliability and Availability
Reliability
� It does, what it is designed to do
� Application failures can be “ok” - OS provides fault isolation and clean restart after errors
� OS errors are bad, real bad...
![Page 25: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/25.jpg)
Reliability and Availability
Reliability
� It does, what it is designed to do
� Application failures can be “ok” - OS provides fault isolation and clean restart after errors
� OS errors are bad, real bad...
![Page 26: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/26.jpg)
Reliability
Making an OS reliable is challenging
� hostile environment (viruses, malicious code take control)
� testing helps - but is much more difficult
� extremely rare corner cases can occur regularly
![Page 27: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/27.jpg)
Reliability
Making an OS reliable is challenging
� hostile environment (viruses, malicious code take control)
� testing helps - but is much more difficult
� extremely rare corner cases can occur regularly
![Page 28: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/28.jpg)
Availability
Availability
� Percentage of time the system is usable
� Buggy OS that loses users work is unreliable AND unavailable
� Buggy OS that never loses users work is reliable BUT unavailable
� Buggy OS that has been subverted but continues to run (and e.g. logs keystrokes and
sends them of) is available BUT unreliable
![Page 29: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/29.jpg)
Availability
Availability
� Percentage of time the system is usable
� Buggy OS that loses users work is unreliable AND unavailable
� Buggy OS that never loses users work is reliable BUT unavailable
� Buggy OS that has been subverted but continues to run (and e.g. logs keystrokes and
sends them of) is available BUT unreliable
![Page 30: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/30.jpg)
Availability
Availability
� Percentage of time the system is usable
� Buggy OS that loses users work is unreliable AND unavailable
� Buggy OS that never loses users work is reliable BUT unavailable
� Buggy OS that has been subverted but continues to run (and e.g. logs keystrokes and
sends them of) is available BUT unreliable
![Page 31: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/31.jpg)
Reliability and Availability
� Reliablity and Availability are desireable
� Two factors:
MTTF: mean time to failure
MTTR: mean time to repair
![Page 32: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/32.jpg)
Security
Security: computers operation cannot be compromised by a malicious attacker
Privacy: data stored on the computer is only accessible to authorized users
![Page 33: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/33.jpg)
Security
� Unfortunately: no useful computer is perfectly secure!
� OS is a complex piece of software
� complex software has bugs
� bugs can be exploited
� HW may be tampered with
� Sysadmin may be untrustworthy
� SW-developer may be untrustworthy (backdoors)
![Page 34: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/34.jpg)
Security
� OS should be designed to minimize its vulnerability to attack
� Fault isolation
� But: interaction between users and programs required
![Page 35: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/35.jpg)
Security Policy
Security Policy defines
� who can access what data
� who can perform what operations
![Page 36: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/36.jpg)
Enforcement
Enforcement
� ensures that policy is followed
Policies and Enforcement may possess vulnerabilities...
![Page 37: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/37.jpg)
Enforcement
Enforcement
� ensures that policy is followed
Policies and Enforcement may possess vulnerabilities...
![Page 38: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/38.jpg)
Portability
� OS provides an abstraction of underlying HW
does not change as the hardware changes
� Portability is an important design criteria of the OS
� don’t want to reimplement everything when HW changes
� e.g. iOS was derived from the MacOS X code base
![Page 39: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/39.jpg)
Portability
� OS provides an abstraction of underlying HW
does not change as the hardware changes
� Portability is an important design criteria of the OS
� don’t want to reimplement everything when HW changes
� e.g. iOS was derived from the MacOS X code base
![Page 40: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/40.jpg)
Portability
� OS provides an abstraction of underlying HW
does not change as the hardware changes
� Portability is an important design criteria of the OS
� don’t want to reimplement everything when HW changes
� e.g. iOS was derived from the MacOS X code base
![Page 41: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/41.jpg)
Portability
� OS provides an abstraction of underlying HW
does not change as the hardware changes
� Portability is an important design criteria of the OS
� don’t want to reimplement everything when HW changes
� e.g. iOS was derived from the MacOS X code base
![Page 42: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/42.jpg)
Portability
� OS provides an abstraction of underlying HW
does not change as the hardware changes
� Portability is an important design criteria of the OS
� don’t want to reimplement everything when HW changes
� e.g. iOS was derived from the MacOS X code base
![Page 43: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/43.jpg)
Portability
� Lifetime of successful operating systems measured in decades
� Windows 10 based on Windows NT (started 1988)
� MS/DOS introduced 1981 - phased out ˜2000
� First Linux release 1991
![Page 44: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/44.jpg)
Portability
� Lifetime of successful operating systems measured in decades
� Windows 10 based on Windows NT (started 1988)
� MS/DOS introduced 1981 - phased out ˜2000
� First Linux release 1991
![Page 45: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/45.jpg)
Portability
� Lifetime of successful operating systems measured in decades
� Windows 10 based on Windows NT (started 1988)
� MS/DOS introduced 1981 - phased out ˜2000
� First Linux release 1991
![Page 46: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/46.jpg)
Portability
� Lifetime of successful operating systems measured in decades
� Windows 10 based on Windows NT (started 1988)
� MS/DOS introduced 1981 - phased out ˜2000
� First Linux release 1991
![Page 47: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/47.jpg)
Portability
� An OS must be designed to support
� applications that have not yet been written and
� hardware that has not been developed
� Simple, standard way for applications to interact with OS
� API
� memory access model
� instructions that can be executed
![Page 48: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/48.jpg)
Hardware Abstraction Layer
� in older/more theoretical literature sometimes “Abstract Virtual Machine”
� provides fixed point across which both application and hardware can develop independently
Example for a highly portable OS?
![Page 49: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/49.jpg)
Hardware Abstraction Layer
� in older/more theoretical literature sometimes “Abstract Virtual Machine”
� provides fixed point across which both application and hardware can develop independently
Example for a highly portable OS?
![Page 50: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/50.jpg)
Performance
� Performance associated with application - OS design can affect the percieved performance
� OS decides when app can run how much memory it gets etc.
� Performance can be measured in different ways
Overhead / Efficiency
� Added (lack of) cost of implementing an abstraction presented to applications
![Page 51: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/51.jpg)
Performance aspects
Fairness: resource allocation can impact performance
� divide resources equally - or treat some differently?
� How to decide which task gets priority?
![Page 52: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/52.jpg)
Performance aspects
Response Time
� (delay) how long does it take for a single task to run from the time it starts to the time it
completes
� (move the mouse . . . mouse pointer reflects movement)
Throughput
� Rate at which system completes tasks
![Page 53: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/53.jpg)
Performance aspects
Predictability
� whether systems response time is consistent over time
� can be more important than average performance
![Page 54: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/54.jpg)
Adoption
� Success of an OS depends on adoption
� SWEB may be the best - but if no application providers support it, it remains doomed for
the this exercise
Network Effect
� value of technology does not depend only on its intrinsic capabilities but on the number of
people adopting it
![Page 55: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/55.jpg)
Adoption
� Success of an OS depends on adoption
� SWEB may be the best - but if no application providers support it, it remains doomed for
the this exercise
Network Effect
� value of technology does not depend only on its intrinsic capabilities but on the number of
people adopting it
![Page 56: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/56.jpg)
Adoption
� App and HW-vendors focus on OS with most users
� users favor OS with best applications or cheapest hardware
� Goal for OS: take advantage of network effect
� make it easy to accommodate new hardware
� make it easy for applications to be ported across different versions of the OS
![Page 57: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/57.jpg)
Adoption
� API and OS source code - open? proprietary?
� Windows, MacOS: proprietary
� Linux: open
� All are widely used
![Page 58: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/58.jpg)
Adoption
� API and OS source code - open? proprietary?
� Windows, MacOS: proprietary
� Linux: open
� All are widely used
![Page 59: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/59.jpg)
Adoption
� API and OS source code - open? proprietary?
� Windows, MacOS: proprietary
� Linux: open
� All are widely used
![Page 60: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/60.jpg)
Design Criteria for OS
Design Criteria for Operating Systems
� Reliability and Availability
� Security
� Portability
� Performance
� Adoption
![Page 61: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/61.jpg)
Balance between these goals
� Improving portability can make the system less reliable and less secure
� e.g. preserving legacy interfaces
� Improving performance may add complexity and hurt reliability
![Page 62: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/62.jpg)
Tradeoff
� Research OS in the 1980’s: use type-safe language to reduce programmer errors
� For speed: frequently used routines implemented in assembly
� Optimized sequence of instructions - but which would sometimes break if the OS exceeded
a specific size
� Initially nowhere near this limitation
� After a few years in production: random crashes occured
� Problem search took weeks ....
� Was it worth it?
![Page 63: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/63.jpg)
History
The first computers were so called “mainframes” that had no operating systems.
![Page 64: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/64.jpg)
Multics
� first collection of compatible utility programs (Multics [1])
� assemblers, compilers, debugging tools
� standard routines for input and output
� buffers to “spool” printer and tape output
� utilities designed to load sequence (or “batch”) of programs into memory
� automate some of the reconfiguration performed by human operators
![Page 65: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/65.jpg)
Multics
� first collection of compatible utility programs (Multics [1])
� assemblers, compilers, debugging tools
� standard routines for input and output
� buffers to “spool” printer and tape output
� utilities designed to load sequence (or “batch”) of programs into memory
� automate some of the reconfiguration performed by human operators
![Page 66: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/66.jpg)
Multics
� first collection of compatible utility programs (Multics [1])
� assemblers, compilers, debugging tools
� standard routines for input and output
� buffers to “spool” printer and tape output
� utilities designed to load sequence (or “batch”) of programs into memory
� automate some of the reconfiguration performed by human operators
![Page 67: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/67.jpg)
Multics
� first collection of compatible utility programs (Multics [1])
� assemblers, compilers, debugging tools
� standard routines for input and output
� buffers to “spool” printer and tape output
� utilities designed to load sequence (or “batch”) of programs into memory
� automate some of the reconfiguration performed by human operators
![Page 68: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/68.jpg)
Multics
� first collection of compatible utility programs (Multics [1])
� assemblers, compilers, debugging tools
� standard routines for input and output
� buffers to “spool” printer and tape output
� utilities designed to load sequence (or “batch”) of programs into memory
� automate some of the reconfiguration performed by human operators
![Page 69: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/69.jpg)
Multics
� first collection of compatible utility programs (Multics [1])
� assemblers, compilers, debugging tools
� standard routines for input and output
� buffers to “spool” printer and tape output
� utilities designed to load sequence (or “batch”) of programs into memory
� automate some of the reconfiguration performed by human operators
![Page 70: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/70.jpg)
Multics Concepts
still valid today in modern operating systems
� paged and segmented memory
� hierarchical file systems
� multiple processor support
� shared memory
� modularized structure
� written in a high level programming language
� and no longer in assembly.
![Page 71: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/71.jpg)
Multics Concepts
still valid today in modern operating systems
� paged and segmented memory
� hierarchical file systems
� multiple processor support
� shared memory
� modularized structure
� written in a high level programming language
� and no longer in assembly.
![Page 72: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/72.jpg)
Multics Concepts
still valid today in modern operating systems
� paged and segmented memory
� hierarchical file systems
� multiple processor support
� shared memory
� modularized structure
� written in a high level programming language
� and no longer in assembly.
![Page 73: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/73.jpg)
Multics Concepts
still valid today in modern operating systems
� paged and segmented memory
� hierarchical file systems
� multiple processor support
� shared memory
� modularized structure
� written in a high level programming language
� and no longer in assembly.
![Page 74: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/74.jpg)
Multics Concepts
still valid today in modern operating systems
� paged and segmented memory
� hierarchical file systems
� multiple processor support
� shared memory
� modularized structure
� written in a high level programming language
� and no longer in assembly.
![Page 75: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/75.jpg)
Multics Concepts
still valid today in modern operating systems
� paged and segmented memory
� hierarchical file systems
� multiple processor support
� shared memory
� modularized structure
� written in a high level programming language
� and no longer in assembly.
![Page 76: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/76.jpg)
Multics Concepts
still valid today in modern operating systems
� paged and segmented memory
� hierarchical file systems
� multiple processor support
� shared memory
� modularized structure
� written in a high level programming language
� and no longer in assembly.
![Page 77: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/77.jpg)
UNIX et al.
� Multics never gained critical mass in the market place
� Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX
� by programmers - for programmers
� originally in assembly language
� rewritten in C
� portable operating system!
![Page 78: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/78.jpg)
UNIX et al.
� Multics never gained critical mass in the market place
� Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX
� by programmers - for programmers
� originally in assembly language
� rewritten in C
� portable operating system!
![Page 79: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/79.jpg)
UNIX et al.
� Multics never gained critical mass in the market place
� Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX
� by programmers - for programmers
� originally in assembly language
� rewritten in C
� portable operating system!
![Page 80: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/80.jpg)
UNIX et al.
� Multics never gained critical mass in the market place
� Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX
� by programmers - for programmers
� originally in assembly language
� rewritten in C
� portable operating system!
![Page 81: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/81.jpg)
UNIX et al.
� Multics never gained critical mass in the market place
� Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX
� by programmers - for programmers
� originally in assembly language
� rewritten in C
� portable operating system!
![Page 82: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/82.jpg)
UNIX et al.
� Multics never gained critical mass in the market place
� Ken Thompson and Dennis Ritchie started working on an OS for microcomputers: UNIX
� by programmers - for programmers
� originally in assembly language
� rewritten in C
� portable operating system!
![Page 83: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/83.jpg)
IBM’s OS/360
Pugh et al. [2]
� concept that the OS keeps track of all of the system resources that are used, including
� program and data space allocation in main memory
� file space in secondary storage
� file locking during update
![Page 84: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/84.jpg)
Evolution of Operating Systems
Phase Idea
Open shop operating systems
Batch processing tape batching, first-in/first -out scheduling
Multiprogramming processor multiplexing, atomic operations, demand paging, I/O
spooling, priority scheduling, remote job entry
Timesharing simultaneous user interactions, on-line file systems
Concurrent programming hierarchical systems, extensible kernels, parallel programming
Personal Computing graphical user interface
Distributed Systems remote servers
![Page 85: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/85.jpg)
Personal Computing
1968: First devices named “personal computer” (actually a calculator)
![Page 86: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/86.jpg)
PCs
1973: Xerox Alto, first computer with mouse, desktop, and GUI
![Page 87: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/87.jpg)
PC Operating Systems
� Different requirements: only one user
� CP/M, DOS, Apple-DOS
� Windows
� OS-2, Windows-XP, OS-X, Linux....
![Page 88: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/88.jpg)
![Page 89: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/89.jpg)
Booting
![Page 90: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/90.jpg)
Starting in Real Mode
� 16 bit mode
� Address space: 1 MB
� How is that possible?
� CS register has a 20-bit base address
� actually only 4 bit, but shifted by 16 bits to the left
→ 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address
![Page 91: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/91.jpg)
Starting in Real Mode
� 16 bit mode
� Address space: 1 MB
� How is that possible?
� CS register has a 20-bit base address
� actually only 4 bit, but shifted by 16 bits to the left
→ 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address
![Page 92: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/92.jpg)
Starting in Real Mode
� 16 bit mode
� Address space: 1 MB
� How is that possible?
� CS register has a 20-bit base address
� actually only 4 bit, but shifted by 16 bits to the left
→ 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address
![Page 93: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/93.jpg)
Starting in Real Mode
� 16 bit mode
� Address space: 1 MB
� How is that possible?
� CS register has a 20-bit base address
� actually only 4 bit, but shifted by 16 bits to the left
→ 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address
![Page 94: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/94.jpg)
Starting in Real Mode
� 16 bit mode
� Address space: 1 MB
� How is that possible?
� CS register has a 20-bit base address
� actually only 4 bit, but shifted by 16 bits to the left
→ 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address
![Page 95: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/95.jpg)
Starting in Real Mode
� 16 bit mode
� Address space: 1 MB
� How is that possible?
� CS register has a 20-bit base address
� actually only 4 bit, but shifted by 16 bits to the left
→ 4 bits (base/prefix) + 16 bits (address/offset) = 20 bit address
![Page 96: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/96.jpg)
Booting x86 Intel
![Page 97: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/97.jpg)
Booting in Real Mode
� Address: 0xFFFFFFF0
� How is that possible?
� CS register also has a 32-bit base address (initialized to 0xFFFF0000)
� What if I have < 4GB RAM?
� physical address space 6= RAM directly mapped
![Page 98: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/98.jpg)
Booting in Real Mode
� Address: 0xFFFFFFF0
� How is that possible?
� CS register also has a 32-bit base address (initialized to 0xFFFF0000)
� What if I have < 4GB RAM?
� physical address space 6= RAM directly mapped
![Page 99: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/99.jpg)
Booting in Real Mode
� Address: 0xFFFFFFF0
� How is that possible?
� CS register also has a 32-bit base address (initialized to 0xFFFF0000)
� What if I have < 4GB RAM?
� physical address space 6= RAM directly mapped
![Page 100: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/100.jpg)
Booting in Real Mode
� Address: 0xFFFFFFF0
� How is that possible?
� CS register also has a 32-bit base address (initialized to 0xFFFF0000)
� What if I have < 4GB RAM?
� physical address space 6= RAM directly mapped
![Page 101: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/101.jpg)
Booting in Real Mode
� Address: 0xFFFFFFF0
� How is that possible?
� CS register also has a 32-bit base address (initialized to 0xFFFF0000)
� What if I have < 4GB RAM?
� physical address space 6= RAM directly mapped
![Page 102: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/102.jpg)
Physical Address Space
00000000-007fffff (prio 0, RW): alias ram-below-4g (this is our RAM)
000a0000-000bffff (prio 1, RW): vga-lowmem (remember for later)
000c0000-000dffff (prio 1, RW): pc.rom
000e0000-000fffff (prio 1, R-): alias isa-bios
fd000000-fdffffff (prio 1, RW): vga.vram
febc0000-febdffff (prio 1, RW): e1000-mmio
febf0400-febf041f (prio 0, RW): vga ioports remapped
febf0500-febf0515 (prio 0, RW): bochs dispi interface
febf0600-febf0607 (prio 0, RW): qemu extended regs
fffc0000-ffffffff (prio 0, R-): pc.bios (ahhh!)
...
![Page 103: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/103.jpg)
BIOS
� BIOS initializes hardware platform
� Switch to protected mode (32 bit)
� Select a device to boot from
� Load MBR from device into memory
� Execute code from MBR
![Page 104: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/104.jpg)
BIOS
� BIOS initializes hardware platform
� Switch to protected mode (32 bit)
� Select a device to boot from
� Load MBR from device into memory
� Execute code from MBR
![Page 105: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/105.jpg)
BIOS
� BIOS initializes hardware platform
� Switch to protected mode (32 bit)
� Select a device to boot from
� Load MBR from device into memory
� Execute code from MBR
![Page 106: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/106.jpg)
BIOS
� BIOS initializes hardware platform
� Switch to protected mode (32 bit)
� Select a device to boot from
� Load MBR from device into memory
� Execute code from MBR
![Page 107: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/107.jpg)
BIOS
� BIOS initializes hardware platform
� Switch to protected mode (32 bit)
� Select a device to boot from
� Load MBR from device into memory
� Execute code from MBR
![Page 108: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/108.jpg)
Booting x86 Intel (Illustration)
![Page 109: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/109.jpg)
GRUB
� Boot loader for Linux, SWEB, . . .
� Loads the OS image from disk and starts OS
GRUBOne of the important features in GRUB is flexibility; GRUB understands file-systems and kernel
executable formats, so you can load an arbitrary operating system the way you like, without
recording the physical position of your kernel on the disk. Thus you can load the kernel just by
specifying its file name and the drive and partition where the kernel resides.
![Page 110: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/110.jpg)
Booting
![Page 111: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/111.jpg)
Booting the OS
� Prepare hardware
� Start device drivers and initialize devices
� Start initial processes (e.g. init-process)
![Page 112: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/112.jpg)
Booting the OS
� Prepare hardware
� Start device drivers and initialize devices
� Start initial processes (e.g. init-process)
![Page 113: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/113.jpg)
Booting the OS
� Prepare hardware
� Start device drivers and initialize devices
� Start initial processes (e.g. init-process)
![Page 114: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/114.jpg)
Booting the OS (SWEB)
Kernel is a compiled binary (e.g. an ELF binary)
% readelf -a kernel.x | grep Entry
Entry point address: 0x801001ba
% objdump -S kernel.x | less
801001ba <entry>:
801001ba: 55 push %ebp
801001bb: 89 e5 mov %esp,%ebp
801001bd: 83 ec 10 sub $0x10,%esp
801001c0: 89 1d 00 90 14 00 mov %ebx,0x149000
Wait, that’s C-Code!
![Page 115: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/115.jpg)
Booting the OS (SWEB)
Kernel is a compiled binary (e.g. an ELF binary)
% readelf -a kernel.x | grep Entry
Entry point address: 0x801001ba
% objdump -S kernel.x | less
801001ba <entry>:
801001ba: 55 push %ebp
801001bb: 89 e5 mov %esp,%ebp
801001bd: 83 ec 10 sub $0x10,%esp
801001c0: 89 1d 00 90 14 00 mov %ebx,0x149000
Wait, that’s C-Code!
![Page 116: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/116.jpg)
Booting the OS (SWEB)
Kernel is a compiled binary (e.g. an ELF binary)
% readelf -a kernel.x | grep Entry
Entry point address: 0x801001ba
% objdump -S kernel.x | less
801001ba <entry>:
801001ba: 55 push %ebp
801001bb: 89 e5 mov %esp,%ebp
801001bd: 83 ec 10 sub $0x10,%esp
801001c0: 89 1d 00 90 14 00 mov %ebx,0x149000
Wait, that’s C-Code!
![Page 117: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/117.jpg)
Booting the OS (SWEB)
Kernel is a compiled binary (e.g. an ELF binary)
% readelf -a kernel.x | grep Entry
Entry point address: 0x801001ba
% objdump -S kernel.x | less
801001ba <entry>:
801001ba: 55 push %ebp
801001bb: 89 e5 mov %esp,%ebp
801001bd: 83 ec 10 sub $0x10,%esp
801001c0: 89 1d 00 90 14 00 mov %ebx,0x149000
Wait, that’s C-Code!
![Page 118: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/118.jpg)
Booting the OS (SWEB)
extern "C" void entry()
{
asm("mov %ebx,multi_boot_structure_pointer - BASE");
PRINT("Booting...\n");
![Page 119: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/119.jpg)
Booting the OS (SWEB)
PRINT("Clearing Framebuffer...\n");
memset((char*) 0xB8000, 0, 80 * 25 * 2);
PRINT("Clearing BSS...\n");
char* bss_start = TRUNCATE(&bss_start_address);
memset(bss_start, 0, TRUNCATE(&bss_end_address) - bss_start);
PRINT("Initializing Kernel Paging Structures...\n");
//...
![Page 120: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/120.jpg)
Booting the OS (SWEB)
PRINT("Enable PSE and PAE...\n");
asm("mov %cr4,%eax\n"
"or $0x20, %eax\n"
"mov %eax,%cr4\n");
PRINT("Setting CR3 Register...\n");
asm("mov %[pd],%%cr3" : : [pd]"r"(TRUNCATE(kernel_page_map_level_4)));
PRINT("Enable EFER.LME and EFER.NXE...\n");
asm("mov $0xC0000080,%ecx\n"
"rdmsr\n"
"or $0x900,%eax\n"
"wrmsr\n");
//...
PRINT("Enable Paging...\n");
asm("mov %cr0,%eax\n"
"or $0x80000001,%eax\n"
"mov %eax,%cr0\n");
![Page 121: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/121.jpg)
Booting the OS (SWEB)
PRINT("Setup TSS...\n");
TSS* g_tss_p = (TSS*) TRUNCATE(&g_tss);
g_tss_p->ist0_h = -1U;
g_tss_p->ist0_l = (uint32) TRUNCATE(boot_stack) | 0x80004000;
g_tss_p->rsp0_h = -1U;
g_tss_p->rsp0_l = (uint32) TRUNCATE(boot_stack) | 0x80004000;
![Page 122: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/122.jpg)
Booting the OS (SWEB)
![Page 123: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/123.jpg)
Booting the OS (SWEB)
![Page 124: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/124.jpg)
Booting the OS (SWEB)
static void setSegmentDescriptor(uint32 index, uint32 baseH, uint32 baseL, uint32
limit, uint8 dpl, uint8 code, uint8 tss);
PRINT("Setup Segments...\n");
setSegmentDescriptor(1, 0, 0, 0, 0, 1, 0);
setSegmentDescriptor(2, 0, 0, 0, 0, 0, 0);
setSegmentDescriptor(3, 0, 0, 0, 3, 1, 0);
setSegmentDescriptor(4, 0, 0, 0, 3, 0, 0);
setSegmentDescriptor(5, -1U, (uint32) TRUNCATE(&g_tss) | 0x80000000,
sizeof(TSS) - 1, 0, 0, 1);
PRINT("Loading Long Mode GDT...\n");
struct GDT32Ptr gdt32_ptr;
gdt32_ptr.limit = sizeof(gdt) - 1;
gdt32_ptr.addr = (uint32) TRUNCATE(gdt);
asm("lgdt %[gdt_ptr]" : : [gdt_ptr]"m"(gdt32_ptr));
// ...
![Page 125: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/125.jpg)
Booting the OS (SWEB)
PRINT("Setting Long Mode Segment Selectors...\n");
asm("mov %%ax, %%ds\n"
"mov %%ax, %%es\n"
"mov %%ax, %%ss\n"
"mov %%ax, %%fs\n"
"mov %%ax, %%gs\n"
: : "a"(KERNEL_DS));
PRINT("Calling entry64()...\n");
asm("ljmp %[cs],$entry64-BASE\n" : : [cs]"i"(KERNEL_CS));
PRINT("Returned from entry64()? This should never happen.\n");
asm("hlt");}
![Page 126: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/126.jpg)
Booting the OS (SWEB)
PRINT("Setting Long Mode Segment Selectors...\n");
asm("mov %%ax, %%ds\n"
"mov %%ax, %%es\n"
"mov %%ax, %%ss\n"
"mov %%ax, %%fs\n"
"mov %%ax, %%gs\n"
: : "a"(KERNEL_DS));
PRINT("Calling entry64()...\n");
asm("ljmp %[cs],$entry64-BASE\n" : : [cs]"i"(KERNEL_CS));
PRINT("Returned from entry64()? This should never happen.\n");
asm("hlt");}
![Page 127: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/127.jpg)
Booting the OS (SWEB)
extern "C" void entry64()
{
PRINT("Parsing Multiboot Header...\n");
parseMultibootHeader();
PRINT("Initializing Kernel Paging Structures...\n");
initialisePaging();
PRINT("Setting CR3 Register...\n");
asm("mov %%rax, %%cr3" : : "a"(VIRTUAL_TO_PHYSICAL_BOOT(ArchMemory::
getRootOfKernelPagingStructure())));
PRINT("Switch to our own stack...\n");
asm("mov %[stack], %%rsp\n"
"mov %[stack], %%rbp\n" : : [stack]"i"(boot_stack + 0x4000));
![Page 128: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/128.jpg)
Booting the OS (SWEB)
PRINT("Loading Long Mode Segments...\n");
gdt_ptr.limit = sizeof(gdt) - 1;
gdt_ptr.addr = (uint64)gdt;
asm("lgdt (%%rax)" : : "a"(&gdt_ptr));
asm("mov %%ax, %%ds\n"
"mov %%ax, %%es\n"
"mov %%ax, %%ss\n"
"mov %%ax, %%fs\n"
"mov %%ax, %%gs\n"
: : "a"(KERNEL_DS));
asm("ltr %%ax" : : "a"(KERNEL_TSS));
PRINT("Calling startup()...\n");
asm("jmp *%[startup]" : : [startup]"r"(startup));
while (1);
}
![Page 129: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/129.jpg)
Booting the OS (SWEB)
extern "C" void startup()
{
writeLine2Bochs("Removing Boot Time Ident Mapping...\n");
removeBootTimeIdentMapping();
system_state = BOOTING;
PageManager::instance();
writeLine2Bochs("PageManager and KernelMemoryManager created \n");
main_console = ArchCommon::createConsole(1);
writeLine2Bochs("Console created \n");
// ...
![Page 130: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/130.jpg)
Booting the OS (SWEB)
Scheduler::instance();
//needs to be done after scheduler and terminal, but prior to enableInterrupts
kprintf_init();
debug(MAIN, "Threads init\n");
ArchThreads::initialise();
debug(MAIN, "Interupts init\n");
ArchInterrupts::initialise();
ArchInterrupts::setTimerFrequency(IRQ0_TIMER_FREQUENCY);
![Page 131: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/131.jpg)
Booting the OS (SWEB)
ArchCommon::initDebug();
vfs.initialize();
debug(MAIN, "Mounting DeviceFS under /dev/\n");
DeviceFSType *devfs = new DeviceFSType();
vfs.registerFileSystem(devfs);
default_working_dir = vfs.root_mount("devicefs", 0);
debug(MAIN, "Block Device creation\n");
BDManager::getInstance()->doDeviceDetection();
debug(MAIN, "Block Device done\n");
for (BDVirtualDevice* bdvd : BDManager::getInstance()->device_list_)
{
debug(MAIN, "Detected Device: %s :: %d\n", bdvd->getName(), bdvd->
getDeviceNumber());
}
![Page 132: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/132.jpg)
Booting the OS (SWEB)
// initialise global and static objects
extern ustl::list<FileDescriptor*> global_fd;
new (&global_fd) ustl::list<FileDescriptor*>();
extern Mutex global_fd_lock;
new (&global_fd_lock) Mutex("global_fd_lock");
// ...
debug(MAIN, "Timer enable\n");
ArchInterrupts::enableTimer();
KeyboardManager::instance();
ArchInterrupts::enableKBD();
![Page 133: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/133.jpg)
Booting the OS (SWEB)
debug(MAIN, "Adding Kernel threads\n");
Scheduler::instance()->addNewThread(main_console);
Scheduler::instance()->addNewThread(new ProcessRegistry(new FileSystemInfo(*default_working_dir), user_progs /*see user_progs.h*/));
Scheduler::instance()->printThreadList();
kprintf("Now enabling Interrupts...\n");
system_state = RUNNING;
ArchInterrupts::enableInterrupts();
Scheduler::instance()->yield();
//not reached
assert(false);}
![Page 134: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/134.jpg)
Booting
![Page 135: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/135.jpg)
Process and Thread
Fundamentals
![Page 136: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/136.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 137: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/137.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 138: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/138.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 139: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/139.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 140: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/140.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 141: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/141.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 142: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/142.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 143: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/143.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 144: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/144.jpg)
Program, Process, Thread
� A program: a binary file containing code and data
� actions: write, compile, install, load
� resources: file
� A thread: an execution context
� actions: run, interrupt, stop
� resources: CPU time, stack, registers
� A process: a container for threads and memory contents of a program
� actions: create, start, terminate
� resources: threads, memory, program
![Page 145: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/145.jpg)
![Page 146: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/146.jpg)
Abstractions
� Process: abstraction of a computer
� File: abstraction of a disk or a device
� Socket: abstraction of a network connection
� Window: abstraction of a display
→ Abstractions hide many details but provide the required capabilities
![Page 147: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/147.jpg)
Abstractions
� Process: abstraction of a computer
� File: abstraction of a disk or a device
� Socket: abstraction of a network connection
� Window: abstraction of a display
→ Abstractions hide many details but provide the required capabilities
![Page 148: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/148.jpg)
Abstractions
� Process: abstraction of a computer
� File: abstraction of a disk or a device
� Socket: abstraction of a network connection
� Window: abstraction of a display
→ Abstractions hide many details but provide the required capabilities
![Page 149: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/149.jpg)
Abstractions
� Process: abstraction of a computer
� File: abstraction of a disk or a device
� Socket: abstraction of a network connection
� Window: abstraction of a display
→ Abstractions hide many details but provide the required capabilities
![Page 150: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/150.jpg)
Abstractions
� Process: abstraction of a computer
� File: abstraction of a disk or a device
� Socket: abstraction of a network connection
� Window: abstraction of a display
→ Abstractions hide many details but provide the required capabilities
![Page 151: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/151.jpg)
Abstractions
� Process: abstraction of a computer
� File: abstraction of a disk or a device
� Socket: abstraction of a network connection
� Window: abstraction of a display
→ Abstractions hide many details but provide the required capabilities
![Page 152: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/152.jpg)
CPU vs. Process
![Page 153: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/153.jpg)
CPU vs. Process
![Page 154: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/154.jpg)
Processes
Implemented by the kernel
![Page 155: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/155.jpg)
Implementation?
� We have “one hardware”
� We have many “processes”
� How do we solve this?
![Page 156: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/156.jpg)
The Process Abstraction
![Page 157: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/157.jpg)
Program, Process, Thread
� Once a program is loaded in memory, OS can start it(s first thread) by
� setting up a stack and setting the stack pointer and
� setting the instruction pointer (of the first thread) to the programs first instruction
� Process is an instance of a program
� Kernel must organize running code of multiple processes
� Must be able to switch from one process to another
� OS keeps a list of process data structure (aka the “PCB”)
![Page 158: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/158.jpg)
Program, Process, Thread
� Once a program is loaded in memory, OS can start it(s first thread) by
� setting up a stack and setting the stack pointer and
� setting the instruction pointer (of the first thread) to the programs first instruction
� Process is an instance of a program
� Kernel must organize running code of multiple processes
� Must be able to switch from one process to another
� OS keeps a list of process data structure (aka the “PCB”)
![Page 159: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/159.jpg)
Program, Process, Thread
� Once a program is loaded in memory, OS can start it(s first thread) by
� setting up a stack and setting the stack pointer and
� setting the instruction pointer (of the first thread) to the programs first instruction
� Process is an instance of a program
� Kernel must organize running code of multiple processes
� Must be able to switch from one process to another
� OS keeps a list of process data structure (aka the “PCB”)
![Page 160: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/160.jpg)
Program, Process, Thread
� Once a program is loaded in memory, OS can start it(s first thread) by
� setting up a stack and setting the stack pointer and
� setting the instruction pointer (of the first thread) to the programs first instruction
� Process is an instance of a program
� Kernel must organize running code of multiple processes
� Must be able to switch from one process to another
� OS keeps a list of process data structure (aka the “PCB”)
![Page 161: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/161.jpg)
Program, Process, Thread
� Once a program is loaded in memory, OS can start it(s first thread) by
� setting up a stack and setting the stack pointer and
� setting the instruction pointer (of the first thread) to the programs first instruction
� Process is an instance of a program
� Kernel must organize running code of multiple processes
� Must be able to switch from one process to another
� OS keeps a list of process data structure (aka the “PCB”)
![Page 162: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/162.jpg)
Program, Process, Thread
� Once a program is loaded in memory, OS can start it(s first thread) by
� setting up a stack and setting the stack pointer and
� setting the instruction pointer (of the first thread) to the programs first instruction
� Process is an instance of a program
� Kernel must organize running code of multiple processes
� Must be able to switch from one process to another
� OS keeps a list of process data structure (aka the “PCB”)
![Page 163: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/163.jpg)
Program, Process, Thread
� Once a program is loaded in memory, OS can start it(s first thread) by
� setting up a stack and setting the stack pointer and
� setting the instruction pointer (of the first thread) to the programs first instruction
� Process is an instance of a program
� Kernel must organize running code of multiple processes
� Must be able to switch from one process to another
� OS keeps a list of process data structure (aka the “PCB”)
![Page 164: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/164.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 165: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/165.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 166: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/166.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 167: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/167.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 168: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/168.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 169: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/169.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 170: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/170.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 171: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/171.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 172: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/172.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 173: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/173.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 174: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/174.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 175: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/175.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 176: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/176.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 177: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/177.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 178: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/178.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 179: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/179.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 180: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/180.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 181: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/181.jpg)
Process List (aka PCB)
Process list stores
� where program is loaded in memory
� where image is on disk
� which user asked to execute
� what privileges the process has
� etc.
� Process ID
� User ID
� Process status
� Scheduling information
� I/O resources
Process can have multiple threads
� same program code and data
� own stack
� own registers (including instruction pointer)
![Page 182: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/182.jpg)
Process Protection Mechanisms
![Page 183: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/183.jpg)
How can it be safe to run untrusted software on your hardware?
Challenges:
� Threads of a process run code
� What code?
� Do we trust that code?
� Maybe buggy?
Malicious?
� We want to give the program restricted privileges
� How can we do that?
![Page 184: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/184.jpg)
How can it be safe to run untrusted software on your hardware?
Challenges:
� Threads of a process run code
� What code?
� Do we trust that code?
� Maybe buggy?
Malicious?
� We want to give the program restricted privileges
� How can we do that?
![Page 185: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/185.jpg)
How can it be safe to run untrusted software on your hardware?
Challenges:
� Threads of a process run code
� What code?
� Do we trust that code?
� Maybe buggy?
Malicious?
� We want to give the program restricted privileges
� How can we do that?
![Page 186: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/186.jpg)
How can it be safe to run untrusted software on your hardware?
Challenges:
� Threads of a process run code
� What code?
� Do we trust that code?
� Maybe buggy?
Malicious?
� We want to give the program restricted privileges
� How can we do that?
![Page 187: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/187.jpg)
How can it be safe to run untrusted software on your hardware?
Challenges:
� Threads of a process run code
� What code?
� Do we trust that code?
� Maybe buggy?
Malicious?
� We want to give the program restricted privileges
� How can we do that?
![Page 188: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/188.jpg)
How can it be safe to run untrusted software on your hardware?
Challenges:
� Threads of a process run code
� What code?
� Do we trust that code?
� Maybe buggy? Malicious?
� We want to give the program restricted privileges
� How can we do that?
![Page 189: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/189.jpg)
How can it be safe to run untrusted software on your hardware?
Challenges:
� Threads of a process run code
� What code?
� Do we trust that code?
� Maybe buggy? Malicious?
� We want to give the program restricted privileges
� How can we do that?
![Page 190: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/190.jpg)
Privileged and unprivileged instructions
� Most instructions cannot do any harm
� Some instructions can
asm("cli");asm("hlt");
![Page 191: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/191.jpg)
Privileged and unprivileged instructions
� Most instructions cannot do any harm
� Some instructions can
asm("cli");asm("hlt");
![Page 192: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/192.jpg)
Privileged and unprivileged instructions
� Most instructions cannot do any harm
� Some instructions can
asm("cli");asm("hlt");
![Page 193: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/193.jpg)
Examples for Privileged Instructions (Intel)
� LGDT: Load GDT register
� LLDT: Load LDT register
� LTR: Load task register
� LIDT: Load IDT register
� MOV (control registers): Load and store control registers
� LMSW: Load machine status word
� CLTS: Clear task-switched flag in register CR0
� MOV (debug registers): Load and store debug registers
� INVD: Invalidate cache, without writeback
� WBINVD: Invalidate cache, with writeback
� INVLPG: Invalidate TLB entry
� HLT: Halt processor
� RDMSR: Read Model-Specific Registers
� WRMSR: Write Model-Specific Registers
![Page 194: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/194.jpg)
Dual-mode operation
� User-mode: limited privileges
� Kernel-mode: complete privileges
Recall: DPL defined in segment descriptor
� User-mode: DPL = 3
� Kernel-mode: DPL = 0
→ hardware-assisted control mechanisms
![Page 195: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/195.jpg)
Dual-mode operation
� User-mode: limited privileges
� Kernel-mode: complete privileges
Recall: DPL defined in segment descriptor
� User-mode: DPL = 3
� Kernel-mode: DPL = 0
→ hardware-assisted control mechanisms
![Page 196: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/196.jpg)
Dual-mode operation
� User-mode: limited privileges
� Kernel-mode: complete privileges
Recall: DPL defined in segment descriptor
� User-mode: DPL = 3
� Kernel-mode: DPL = 0
→ hardware-assisted control mechanisms
![Page 197: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/197.jpg)
Hardware Support: Dual-Mode
Kernel Mode: User Mode:
![Page 198: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/198.jpg)
Hardware Support: Dual-Mode
Kernel Mode:
� OS runs in kernel modeUser Mode:
� User programs run in user mode
![Page 199: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/199.jpg)
Hardware Support: Dual-Mode
Kernel Mode:
� OS runs in kernel mode
� Full privileges for hardware accesses
User Mode:
� User programs run in user mode
� Limited privileges
![Page 200: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/200.jpg)
Hardware Support: Dual-Mode
Kernel Mode:
� OS runs in kernel mode
� Full privileges for hardware accesses
� Read/write to any memory
User Mode:
� User programs run in user mode
� Limited privileges
� Some instructions and memory regions are not
accessible
![Page 201: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/201.jpg)
Hardware Support: Dual-Mode
Kernel Mode:
� OS runs in kernel mode
� Full privileges for hardware accesses
� Read/write to any memory
� Access to any I/O-device
User Mode:
� User programs run in user mode
� Limited privileges
� Some instructions and memory regions are not
accessible
� If tried anyway: exception is raised by the CPU.
![Page 202: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/202.jpg)
Hardware Support: Dual-Mode
Kernel Mode:
� OS runs in kernel mode
� Full privileges for hardware accesses
� Read/write to any memory
� Access to any I/O-device
� Access to all disk content
User Mode:
� User programs run in user mode
� Limited privileges
� Some instructions and memory regions are not
accessible
� If tried anyway: exception is raised by the CPU.
� Need something user mode can’t do?
![Page 203: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/203.jpg)
Hardware Support: Dual-Mode
Kernel Mode:
� OS runs in kernel mode
� Full privileges for hardware accesses
� Read/write to any memory
� Access to any I/O-device
� Access to all disk content
� Access to all network traffic
User Mode:
� User programs run in user mode
� Limited privileges
� Some instructions and memory regions are not
accessible
� If tried anyway: exception is raised by the CPU.
� Need something user mode can’t do?
→ ask operating system for help
![Page 204: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/204.jpg)
Hardware Support: Dual-Mode
Kernel Mode:
� OS runs in kernel mode
� Full privileges for hardware accesses
� Read/write to any memory
� Access to any I/O-device
� Access to all disk content
� Access to all network traffic
User Mode:
� User programs run in user mode
� Limited privileges
� Some instructions and memory regions are not
accessible
� If tried anyway: exception is raised by the CPU.
� Need something user mode can’t do?
→ “call” operating system for help
![Page 205: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/205.jpg)
Hardware Support: Dual-Mode
Kernel Mode:
� OS runs in kernel mode
� Full privileges for hardware accesses
� Read/write to any memory
� Access to any I/O-device
� Access to all disk content
� Access to all network traffic
User Mode:
� User programs run in user mode
� Limited privileges
� Some instructions and memory regions are not
accessible
� If tried anyway: exception is raised by the CPU.
� Need something user mode can’t do?
→ “call” operating system for help
→ system call
![Page 206: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/206.jpg)
Hardware Support: Dual-Mode Implementation
� mode stored in EFLAGS register
� segment descriptors
� paging structures
� . . .
![Page 207: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/207.jpg)
IA-32 Ring Structure
![Page 208: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/208.jpg)
How to change from ring to ring . . .
� change from kernel mode (lower level ring) to user mode (higher level ring) not a problem
![Page 209: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/209.jpg)
How to change from ring to ring . . .
� change from kernel mode (lower level ring) to user mode (higher level ring) not a problem
� change from user mode (higher level ring) to kernel mode (lower level ring) must be a
controlled procedure
![Page 210: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/210.jpg)
How to change from ring to ring . . .
� change from kernel mode (lower level ring) to user mode (higher level ring) not a problem
� change from user mode (higher level ring) to kernel mode (lower level ring) must be a
controlled procedure
→ Otherwise there would be no protection
![Page 211: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/211.jpg)
How to change from ring to ring . . .
� change from ring 0 to ring 3 not a problem
� change from user mode (higher level ring) to kernel mode (lower level ring) must be a
controlled procedure
→ Otherwise there would be no protection
![Page 212: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/212.jpg)
How to change from ring to ring . . .
� change from ring 0 to ring 3 not a problem
� change from ring 3 to ring 0 through controlled procedure
→ Otherwise there would be no protection
![Page 213: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/213.jpg)
How to change from ring to ring . . .
� change from ring 0 to ring 3 through special return instruction (iret)
� change from ring 3 to ring 0 through controlled procedure
→ Otherwise there would be no protection
![Page 214: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/214.jpg)
How to change from ring to ring . . .
� change from ring 0 to ring 3 through special return instruction (iret)
� change from ring 3 to ring 0 through int 0x80, sysenter, or syscall
→ Otherwise there would be no protection
![Page 215: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/215.jpg)
Interrupts
� either generated by the software (e.g. syscall)
� or by the hardware
� timer
� I/O-devices
� exceptions (divide-by-zero, page fault, etc.)
![Page 216: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/216.jpg)
Interrupts
� either generated by the software (e.g. syscall)
� or by the hardware
� timer
� I/O-devices
� exceptions (divide-by-zero, page fault, etc.)
![Page 217: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/217.jpg)
Interrupts
� either generated by the software (e.g. syscall)
� or by the hardware
� timer
� I/O-devices
� exceptions (divide-by-zero, page fault, etc.)
![Page 218: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/218.jpg)
Interrupts
� either generated by the software (e.g. syscall)
� or by the hardware
� timer
� I/O-devices
� exceptions (divide-by-zero, page fault, etc.)
![Page 219: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/219.jpg)
Interrupts
� either generated by the software (e.g. syscall)
� or by the hardware
� timer
� I/O-devices
� exceptions (divide-by-zero, page fault, etc.)
![Page 220: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/220.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 221: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/221.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 222: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/222.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 223: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/223.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 224: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/224.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 225: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/225.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 226: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/226.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 227: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/227.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 228: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/228.jpg)
Interrupts and Stacks
� Interrupts switch stack to a kernel stack
� Why?
� Security and stability
� Who knows where the users SP points
� Maybe SP points to illegal address
� Would raises an page fault exception (in the kernel)
� Some register values are pushed to stack by the CPU during a context switch
� How many stacks do we actually need?
� Do we need multiple stacks for the kernel?
![Page 229: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/229.jpg)
Stacks
![Page 230: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/230.jpg)
Stacks
![Page 231: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/231.jpg)
Stacks
User
Sta
ck
Kerr
nel Sta
ck
main
procedure 1
procedure 2
main
procedure 1
procedure 2
CPU state
user mode
main
procedure 1
procedure 2
system call
CPU state
user mode
Syscall
handler
I/O-Driver
Running Ready to Run Blocked
![Page 232: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/232.jpg)
Context Switches
![Page 233: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/233.jpg)
Threads. Multiple Threads.
� one CPU / core: one active thread at any point in time
� how to switch between threads?
� how do we let a CPU / core execute a different function?
� change the instruction pointer?
how?
![Page 234: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/234.jpg)
Threads. Multiple Threads.
� one CPU / core: one active thread at any point in time
� how to switch between threads?
� how do we let a CPU / core execute a different function?
� change the instruction pointer?
how?
![Page 235: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/235.jpg)
Threads. Multiple Threads.
� one CPU / core: one active thread at any point in time
� how to switch between threads?
� how do we let a CPU / core execute a different function?
� change the instruction pointer?
how?
![Page 236: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/236.jpg)
Threads. Multiple Threads.
� one CPU / core: one active thread at any point in time
� how to switch between threads?
� how do we let a CPU / core execute a different function?
� change the instruction pointer?
how?
![Page 237: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/237.jpg)
Threads. Multiple Threads.
� one CPU / core: one active thread at any point in time
� how to switch between threads?
� how do we let a CPU / core execute a different function?
� change the instruction pointer?
how?
![Page 238: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/238.jpg)
Threads. Multiple Threads.
� one CPU / core: one active thread at any point in time
� how to switch between threads?
� how do we let a CPU / core execute a different function?
� change the instruction pointer? how?
![Page 239: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/239.jpg)
Threads. Multiple Threads.
Changing the instruction pointer
asm("jmp *%[other_thread_function]"
:
: [other_thread_function]"r"(other_thread_function));
does this work? Yes, but . . .
� what if the thread is in another process?
� scheduling thread slices: how do we later restore the state we came from?
� what if we’re coming from kernelspace?
![Page 240: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/240.jpg)
Threads. Multiple Threads.
Changing the instruction pointer
asm("jmp *%[other_thread_function]"
:
: [other_thread_function]"r"(other_thread_function));
does this work? Yes, but . . .
� what if the thread is in another process?
� scheduling thread slices: how do we later restore the state we came from?
� what if we’re coming from kernelspace?
![Page 241: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/241.jpg)
Threads. Multiple Threads.
Changing the instruction pointer
asm("jmp *%[other_thread_function]"
:
: [other_thread_function]"r"(other_thread_function));
does this work?
Yes, but . . .
� what if the thread is in another process?
� scheduling thread slices: how do we later restore the state we came from?
� what if we’re coming from kernelspace?
![Page 242: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/242.jpg)
Threads. Multiple Threads.
Changing the instruction pointer
asm("jmp *%[other_thread_function]"
:
: [other_thread_function]"r"(other_thread_function));
does this work? Yes, but . . .
� what if the thread is in another process?
� scheduling thread slices: how do we later restore the state we came from?
� what if we’re coming from kernelspace?
![Page 243: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/243.jpg)
Threads. Multiple Threads.
Changing the instruction pointer
asm("jmp *%[other_thread_function]"
:
: [other_thread_function]"r"(other_thread_function));
does this work? Yes, but . . .
� what if the thread is in another process?
� scheduling thread slices: how do we later restore the state we came from?
� what if we’re coming from kernelspace?
![Page 244: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/244.jpg)
Threads. Multiple Threads.
Changing the instruction pointer
asm("jmp *%[other_thread_function]"
:
: [other_thread_function]"r"(other_thread_function));
does this work? Yes, but . . .
� what if the thread is in another process?
� scheduling thread slices:
how do we later restore the state we came from?
� what if we’re coming from kernelspace?
![Page 245: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/245.jpg)
Threads. Multiple Threads.
Changing the instruction pointer
asm("jmp *%[other_thread_function]"
:
: [other_thread_function]"r"(other_thread_function));
does this work? Yes, but . . .
� what if the thread is in another process?
� scheduling thread slices: how do we later restore the state we came from?
� what if we’re coming from kernelspace?
![Page 246: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/246.jpg)
Threads. Multiple Threads.
Changing the instruction pointer
asm("jmp *%[other_thread_function]"
:
: [other_thread_function]"r"(other_thread_function));
does this work? Yes, but . . .
� what if the thread is in another process?
� scheduling thread slices: how do we later restore the state we came from?
� what if we’re coming from kernelspace?
![Page 247: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/247.jpg)
Context Switch: Old Thread → New Thread
� Caused only by an interrupt → privilege level change
� CPU pushes to stack: ss, rsp, rflags, cs, rip
� Store register values (→ next slide)
� Old thread executes Scheduler (code to switch to a new thread)
� Context switch to a new thread
![Page 248: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/248.jpg)
Context Switch: Old Thread → New Thread
� Caused only by an interrupt → privilege level change
� CPU pushes to stack: ss, rsp, rflags, cs, rip
� Store register values (→ next slide)
� Old thread executes Scheduler (code to switch to a new thread)
� Context switch to a new thread
![Page 249: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/249.jpg)
Context Switch: Old Thread → New Thread
� Caused only by an interrupt → privilege level change
� CPU pushes to stack: ss, rsp, rflags, cs, rip
� Store register values (→ next slide)
� Old thread executes Scheduler (code to switch to a new thread)
� Context switch to a new thread
![Page 250: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/250.jpg)
Context Switch: Old Thread → New Thread
� Caused only by an interrupt → privilege level change
� CPU pushes to stack: ss, rsp, rflags, cs, rip
� Store register values (→ next slide)
� Old thread executes Scheduler (code to switch to a new thread)
� Context switch to a new thread
![Page 251: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/251.jpg)
Context Switch: Old Thread → New Thread
� Caused only by an interrupt → privilege level change
� CPU pushes to stack: ss, rsp, rflags, cs, rip
� Store register values (→ next slide)
� Old thread executes Scheduler (code to switch to a new thread)
� Context switch to a new thread
![Page 252: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/252.jpg)
Context Switch: Old Thread → New Thread
� Caused only by an interrupt → privilege level change
� CPU pushes to stack: ss, rsp, rflags, cs, rip
� Store register values (→ next slide)
� Old thread executes Scheduler (code to switch to a new thread)
� Context switch to a new thread
![Page 253: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/253.jpg)
Context Switch: Store register values
1. Push all CPU register values on the stack
� No modification of instruction pointer / stack pointer:
� rip and rsp were already pushed to the stack by CPU
2. Pop all CPU register values into a struct
3. Set currentThreadInfo, etc. to kernel thread
![Page 254: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/254.jpg)
Context Switch: Store register values
1. Push all CPU register values on the stack
� No modification of instruction pointer / stack pointer:
� rip and rsp were already pushed to the stack by CPU
2. Pop all CPU register values into a struct
3. Set currentThreadInfo, etc. to kernel thread
![Page 255: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/255.jpg)
Context Switch: Store register values
1. Push all CPU register values on the stack
� No modification of instruction pointer / stack pointer:
� rip and rsp were already pushed to the stack by CPU
2. Pop all CPU register values into a struct
3. Set currentThreadInfo, etc. to kernel thread
![Page 256: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/256.jpg)
Context Switch: Store register values
1. Push all CPU register values on the stack
� No modification of instruction pointer / stack pointer:
� rip and rsp were already pushed to the stack by CPU
2. Pop all CPU register values into a struct
3. Set currentThreadInfo, etc. to kernel thread
![Page 257: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/257.jpg)
Context Switch: Store register values
1. Push all CPU register values on the stack
� No modification of instruction pointer / stack pointer:
� rip and rsp were already pushed to the stack by CPU
2. Pop all CPU register values into a struct
3. Set currentThreadInfo, etc. to kernel thread
![Page 258: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/258.jpg)
Context Switch: Store register values
1. Push all CPU register values on the stack
� No modification of instruction pointer / stack pointer:
� rip and rsp were already pushed to the stack by CPU
2. Pop all CPU register values into a struct
3. Set currentThreadInfo, etc. to kernel thread
![Page 259: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/259.jpg)
currentThreadRegisters
struct ArchThreadRegisters
{
uint64 rip; // 0
uint64 cs; // 8
uint64 rflags; // 16
uint64 rax; // 24
uint64 rcx; // 32
uint64 rdx; // 40
uint64 rbx; // 48
uint64 rsp; // 56
uint64 rbp; // 64
uint64 rsi; // 72
uint64 rdi; // 80
uint64 r8; // 88
uint64 r9; // 96
uint64 r10; // 104
uint64 r11; // 112
uint64 r12; // 120
uint64 r13; // 128
uint64 r14; // 136
uint64 r15; // 144
uint64 ds; // 152
uint64 es; // 160
uint64 fs; // 168
uint64 gs; // 176
uint64 ss; // 184
uint64 dpl; // 192
uint64 rsp0; // 200
uint64 ss0; // 208
uint64 cr3; // 216
uint32 fpu[28]; // 224
};
![Page 260: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/260.jpg)
Context Switch: Old Thread → New Thread
1. “Restore” CPU register values
1.1 iretq (interrupt return) expects ss, rsp, rflags, cs, rip on the stack
1.2 iretq pops values from stack into the registers
2. Instruction pointer has a new value, execution continues there
![Page 261: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/261.jpg)
Context Switch: Old Thread → New Thread
1. “Restore” CPU register values
1.1 iretq (interrupt return) expects ss, rsp, rflags, cs, rip on the stack
1.2 iretq pops values from stack into the registers
2. Instruction pointer has a new value, execution continues there
![Page 262: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/262.jpg)
Context Switch: Old Thread → New Thread
1. “Restore” CPU register values
1.1 iretq (interrupt return) expects ss, rsp, rflags, cs, rip on the stack
1.2 iretq pops values from stack into the registers
2. Instruction pointer has a new value, execution continues there
![Page 263: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/263.jpg)
Context Switch: Old Thread → New Thread
1. “Restore” CPU register values
1.1 iretq (interrupt return) expects ss, rsp, rflags, cs, rip on the stack
1.2 iretq pops values from stack into the registers
2. Instruction pointer has a new value, execution continues there
![Page 264: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/264.jpg)
Context Switch
Looks identical for 64 bits
![Page 265: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/265.jpg)
Context Switch: New Thread/First Thread
Act as if:
� Thread was running already
� We are returning from an interrupt
![Page 266: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/266.jpg)
Context Switch: New Thread/First Thread
Act as if:
� Thread was running already
� We are returning from an interrupt
![Page 267: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/267.jpg)
Context Switch: New Thread/First Thread
1. Push stored register values to stack (modifies registers)
2. “Restore” CPU register values as before
3. Instruction pointer has a new value, execution continues there
![Page 268: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/268.jpg)
Context Switch: New Thread/First Thread
1. Push stored register values to stack (modifies registers)
2. “Restore” CPU register values as before
3. Instruction pointer has a new value, execution continues there
![Page 269: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/269.jpg)
Context Switch: New Thread/First Thread
1. Push stored register values to stack (modifies registers)
2. “Restore” CPU register values as before
3. Instruction pointer has a new value, execution continues there
![Page 270: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/270.jpg)
Context Switch: New Thread/First Thread
1. Push stored register values to stack (modifies registers)
2. “Restore” CPU register values as before
3. Instruction pointer has a new value, execution continues there
![Page 271: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/271.jpg)
Context Switches: When and why?
Interrupts:
� clock
� device
� system call (syscall / sysenter / int 0x80)
� cpu fault (trap / interrupt)
� executing privileged instruction
� divide by 0
� integer overflow
� bad memory access
![Page 272: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/272.jpg)
Context Switches: When and why?
Interrupts:
� clock
� device
� system call (syscall / sysenter / int 0x80)
� cpu fault (trap / interrupt)
� executing privileged instruction
� divide by 0
� integer overflow
� bad memory access
![Page 273: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/273.jpg)
Context Switches: When and why?
Interrupts:
� clock
� device
� system call (syscall / sysenter / int 0x80)
� cpu fault (trap / interrupt)
� executing privileged instruction
� divide by 0
� integer overflow
� bad memory access
![Page 274: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/274.jpg)
Context Switches: When and why?
Interrupts:
� clock
� device
� system call (syscall / sysenter / int 0x80)
� cpu fault (trap / interrupt)
� executing privileged instruction
� divide by 0
� integer overflow
� bad memory access
![Page 275: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/275.jpg)
Context Switches: When and why?
Interrupts:
� clock
� device
� system call (syscall / sysenter / int 0x80)
� cpu fault (trap / interrupt)
� executing privileged instruction
� divide by 0
� integer overflow
� bad memory access
![Page 276: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/276.jpg)
Context Switches: When and why?
Interrupts:
� clock
� device
� system call (syscall / sysenter / int 0x80)
� cpu fault (trap / interrupt)
� executing privileged instruction
� divide by 0
� integer overflow
� bad memory access
![Page 277: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/277.jpg)
Context Switches: When and why?
Interrupts:
� clock
� device
� system call (syscall / sysenter / int 0x80)
� cpu fault (trap / interrupt)
� executing privileged instruction
� divide by 0
� integer overflow
� bad memory access
![Page 278: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/278.jpg)
Context Switches: When and why?
Interrupts:
� clock
� device
� system call (syscall / sysenter / int 0x80)
� cpu fault (trap / interrupt)
� executing privileged instruction
� divide by 0
� integer overflow
� bad memory access
![Page 279: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/279.jpg)
Process and Thread Organization
![Page 280: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/280.jpg)
Program, Process, Thread
� Program: a binary file containing code and data
� a mold for a process
� Thread: an execution context
� a sequence of instructions
� if part of a process: restricted to the boundaries of a process
� Process: a container for threads and memory contents of a program
� an instance of a program
� restricted to its own boundaries and rights
![Page 281: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/281.jpg)
Program, Process, Thread
� Program: a binary file containing code and data
� a mold for a process
� Thread: an execution context
� a sequence of instructions
� if part of a process: restricted to the boundaries of a process
� Process: a container for threads and memory contents of a program
� an instance of a program
� restricted to its own boundaries and rights
![Page 282: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/282.jpg)
Program, Process, Thread
� Program: a binary file containing code and data
� a mold for a process
� Thread: an execution context
� a sequence of instructions
� if part of a process: restricted to the boundaries of a process
� Process: a container for threads and memory contents of a program
� an instance of a program
� restricted to its own boundaries and rights
![Page 283: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/283.jpg)
Program, Process, Thread
� Program: a binary file containing code and data
� a mold for a process
� Thread: an execution context
� a sequence of instructions
� if part of a process: restricted to the boundaries of a process
� Process: a container for threads and memory contents of a program
� an instance of a program
� restricted to its own boundaries and rights
![Page 284: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/284.jpg)
Program, Process, Thread
� Program: a binary file containing code and data
� a mold for a process
� Thread: an execution context
� a sequence of instructions
� if part of a process: restricted to the boundaries of a process
� Process: a container for threads and memory contents of a program
� an instance of a program
� restricted to its own boundaries and rights
![Page 285: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/285.jpg)
Program, Process, Thread
� Program: a binary file containing code and data
� a mold for a process
� Thread: an execution context
� a sequence of instructions
� if part of a process: restricted to the boundaries of a process
� Process: a container for threads and memory contents of a program
� an instance of a program
� restricted to its own boundaries and rights
![Page 286: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/286.jpg)
Program, Process, Thread
� Program: a binary file containing code and data
� a mold for a process
� Thread: an execution context
� a sequence of instructions
� if part of a process: restricted to the boundaries of a process
� Process: a container for threads and memory contents of a program
� an instance of a program
� restricted to its own boundaries and rights
![Page 287: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/287.jpg)
Program, Process, Thread
� Program: a binary file containing code and data
� a mold for a process
� Thread: an execution context
� a sequence of instructions
� if part of a process: restricted to the boundaries of a process
� Process: a container for threads and memory contents of a program
� an instance of a program
� restricted to its own boundaries and rights
![Page 288: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/288.jpg)
Process Resources
A process is a container.
� Process ID
� Filename
� Program file (Loader)
� (Open) file descriptors
� Address space (ArchMemory, CR3 register)
� Accounting
� Threads
� Child processes?
![Page 289: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/289.jpg)
Process Resources
A process is a container.
� Process ID
� Filename
� Program file (Loader)
� (Open) file descriptors
� Address space (ArchMemory, CR3 register)
� Accounting
� Threads
� Child processes?
![Page 290: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/290.jpg)
Process Resources
A process is a container.
� Process ID
� Filename
� Program file (Loader)
� (Open) file descriptors
� Address space (ArchMemory, CR3 register)
� Accounting
� Threads
� Child processes?
![Page 291: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/291.jpg)
Process Resources
A process is a container.
� Process ID
� Filename
� Program file (Loader)
� (Open) file descriptors
� Address space (ArchMemory, CR3 register)
� Accounting
� Threads
� Child processes?
![Page 292: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/292.jpg)
Process Resources
A process is a container.
� Process ID
� Filename
� Program file (Loader)
� (Open) file descriptors
� Address space (ArchMemory, CR3 register)
� Accounting
� Threads
� Child processes?
![Page 293: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/293.jpg)
Process Resources
A process is a container.
� Process ID
� Filename
� Program file (Loader)
� (Open) file descriptors
� Address space (ArchMemory, CR3 register)
� Accounting
� Threads
� Child processes?
![Page 294: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/294.jpg)
Process Resources
A process is a container.
� Process ID
� Filename
� Program file (Loader)
� (Open) file descriptors
� Address space (ArchMemory, CR3 register)
� Accounting
� Threads
� Child processes?
![Page 295: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/295.jpg)
Process Resources
A process is a container.
� Process ID
� Filename
� Program file (Loader)
� (Open) file descriptors
� Address space (ArchMemory, CR3 register)
� Accounting
� Threads
� Child processes?
![Page 296: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/296.jpg)
Thread Resources
A thread is a unit for execution.
� Thread ID
� Thread state (Running, Sleeping, . . . )
� A set of register values (defining the state of the execution of the userspace thread
function)
� Not all registers are different
� Some register values are process-specific and not thread-specific (e.g. CR3)
� A user stack
� A kernel stack (for syscalls)
� A second set of register values for the kernel (for syscalls)
![Page 297: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/297.jpg)
Thread Resources
A thread is a unit for execution.
� Thread ID
� Thread state (Running, Sleeping, . . . )
� A set of register values (defining the state of the execution of the userspace thread
function)
� Not all registers are different
� Some register values are process-specific and not thread-specific (e.g. CR3)
� A user stack
� A kernel stack (for syscalls)
� A second set of register values for the kernel (for syscalls)
![Page 298: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/298.jpg)
Thread Resources
A thread is a unit for execution.
� Thread ID
� Thread state (Running, Sleeping, . . . )
� A set of register values (defining the state of the execution of the userspace thread
function)
� Not all registers are different
� Some register values are process-specific and not thread-specific (e.g. CR3)
� A user stack
� A kernel stack (for syscalls)
� A second set of register values for the kernel (for syscalls)
![Page 299: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/299.jpg)
Thread Resources
A thread is a unit for execution.
� Thread ID
� Thread state (Running, Sleeping, . . . )
� A set of register values (defining the state of the execution of the userspace thread
function)
� Not all registers are different
� Some register values are process-specific and not thread-specific (e.g. CR3)
� A user stack
� A kernel stack (for syscalls)
� A second set of register values for the kernel (for syscalls)
![Page 300: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/300.jpg)
Thread Resources
A thread is a unit for execution.
� Thread ID
� Thread state (Running, Sleeping, . . . )
� A set of register values (defining the state of the execution of the userspace thread
function)
� Not all registers are different
� Some register values are process-specific and not thread-specific (e.g. CR3)
� A user stack
� A kernel stack (for syscalls)
� A second set of register values for the kernel (for syscalls)
![Page 301: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/301.jpg)
Thread Resources
A thread is a unit for execution.
� Thread ID
� Thread state (Running, Sleeping, . . . )
� A set of register values (defining the state of the execution of the userspace thread
function)
� Not all registers are different
� Some register values are process-specific and not thread-specific (e.g. CR3)
� A user stack
� A kernel stack (for syscalls)
� A second set of register values for the kernel (for syscalls)
![Page 302: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/302.jpg)
Thread Resources
A thread is a unit for execution.
� Thread ID
� Thread state (Running, Sleeping, . . . )
� A set of register values (defining the state of the execution of the userspace thread
function)
� Not all registers are different
� Some register values are process-specific and not thread-specific (e.g. CR3)
� A user stack
� A kernel stack (for syscalls)
� A second set of register values for the kernel (for syscalls)
![Page 303: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/303.jpg)
Thread Resources
A thread is a unit for execution.
� Thread ID
� Thread state (Running, Sleeping, . . . )
� A set of register values (defining the state of the execution of the userspace thread
function)
� Not all registers are different
� Some register values are process-specific and not thread-specific (e.g. CR3)
� A user stack
� A kernel stack (for syscalls)
� A second set of register values for the kernel (for syscalls)
![Page 304: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/304.jpg)
Process and Thread Interaction
Load program, create process, . . .
� 1 initial thread
� executes the main()-function
� it’s not a “main”-thread
� process may start further threads if required (how?)
![Page 305: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/305.jpg)
Process and Thread Interaction
Load program, create process, . . .
� 1 initial thread
� executes the main()-function
� it’s not a “main”-thread
� process may start further threads if required (how?)
![Page 306: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/306.jpg)
Process and Thread Interaction
Load program, create process, . . .
� 1 initial thread
� executes the main()-function
� it’s not a “main”-thread
� process may start further threads if required (how?)
![Page 307: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/307.jpg)
Process and Thread Interaction
Load program, create process, . . .
� 1 initial thread
� executes the main()-function
� it’s not a “main”-thread
� process may start further threads if required (how?)
![Page 308: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/308.jpg)
Process and Thread Interaction
Load program, create process, . . .
� 1 initial thread
� executes the main()-function
� it’s not a “main”-thread
� process may start further threads if required (how?)
![Page 309: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/309.jpg)
Threads
� “There is no such thing as a thread” at the CPU-level
� As illustrated before: works by creative and clever usage of interrupts
� Threads can be implemented with and without support of the operating system
� Pure Userspace Threading: lightweight, but many drawbacks
� Threads can be implemented with and without support of the CPU (int 0x80 vs.
sysenter/syscall)
![Page 310: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/310.jpg)
Threads
� “There is no such thing as a thread” at the CPU-level
� As illustrated before: works by creative and clever usage of interrupts
� Threads can be implemented with and without support of the operating system
� Pure Userspace Threading: lightweight, but many drawbacks
� Threads can be implemented with and without support of the CPU (int 0x80 vs.
sysenter/syscall)
![Page 311: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/311.jpg)
Threads
� “There is no such thing as a thread” at the CPU-level
� As illustrated before: works by creative and clever usage of interrupts
� Threads can be implemented with and without support of the operating system
� Pure Userspace Threading: lightweight, but many drawbacks
� Threads can be implemented with and without support of the CPU (int 0x80 vs.
sysenter/syscall)
![Page 312: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/312.jpg)
Threads
� “There is no such thing as a thread” at the CPU-level
� As illustrated before: works by creative and clever usage of interrupts
� Threads can be implemented with and without support of the operating system
� Pure Userspace Threading: lightweight, but many drawbacks
� Threads can be implemented with and without support of the CPU (int 0x80 vs.
sysenter/syscall)
![Page 313: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/313.jpg)
Threads
� “There is no such thing as a thread” at the CPU-level
� As illustrated before: works by creative and clever usage of interrupts
� Threads can be implemented with and without support of the operating system
� Pure Userspace Threading: lightweight, but many drawbacks
� Threads can be implemented with and without support of the CPU (int 0x80 vs.
sysenter/syscall)
![Page 314: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/314.jpg)
Pure Userspace Threads
� Kernel has no concept of threads and no idea they might exist
(that’s how it started)
� Implement threads in userspace as library
� can be implemented in all operating systems
![Page 315: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/315.jpg)
Pure Userspace Threads
� Kernel has no concept of threads and no idea they might exist (that’s how it started)
� Implement threads in userspace as library
� can be implemented in all operating systems
![Page 316: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/316.jpg)
Userspace Threads
![Page 317: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/317.jpg)
Userspace Threads
� process manages threads
� user-mode-runtime-system in libc!
� function that might block the thread
� call method in libc to check: thread going to block?
� YES: save registers in thread table
� choose other thread ready to run
� load chosen the thread’s registers from thread table
� change stack pointer and instruction pointer (this time jmp)
![Page 318: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/318.jpg)
Userspace Threads
� process manages threads
� user-mode-runtime-system in libc!
� function that might block the thread
� call method in libc to check: thread going to block?
� YES: save registers in thread table
� choose other thread ready to run
� load chosen the thread’s registers from thread table
� change stack pointer and instruction pointer (this time jmp)
![Page 319: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/319.jpg)
Userspace Threads
� process manages threads
� user-mode-runtime-system in libc!
� function that might block the thread
� call method in libc to check: thread going to block?
� YES: save registers in thread table
� choose other thread ready to run
� load chosen the thread’s registers from thread table
� change stack pointer and instruction pointer (this time jmp)
![Page 320: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/320.jpg)
Userspace Threads
� process manages threads
� user-mode-runtime-system in libc!
� function that might block the thread
� call method in libc to check: thread going to block?
� YES: save registers in thread table
� choose other thread ready to run
� load chosen the thread’s registers from thread table
� change stack pointer and instruction pointer (this time jmp)
![Page 321: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/321.jpg)
Userspace Threads
� process manages threads
� user-mode-runtime-system in libc!
� function that might block the thread
� call method in libc to check: thread going to block?
� YES: save registers in thread table
� choose other thread ready to run
� load chosen the thread’s registers from thread table
� change stack pointer and instruction pointer (this time jmp)
![Page 322: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/322.jpg)
Userspace Threads
� process manages threads
� user-mode-runtime-system in libc!
� function that might block the thread
� call method in libc to check: thread going to block?
� YES: save registers in thread table
� choose other thread ready to run
� load chosen the thread’s registers from thread table
� change stack pointer and instruction pointer (this time jmp)
![Page 323: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/323.jpg)
Userspace Threads
� process manages threads
� user-mode-runtime-system in libc!
� function that might block the thread
� call method in libc to check: thread going to block?
� YES: save registers in thread table
� choose other thread ready to run
� load chosen the thread’s registers from thread table
� change stack pointer and instruction pointer (this time jmp)
![Page 324: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/324.jpg)
Userspace Threads
� process manages threads
� user-mode-runtime-system in libc!
� function that might block the thread
� call method in libc to check: thread going to block?
� YES: save registers in thread table
� choose other thread ready to run
� load chosen the thread’s registers from thread table
� change stack pointer and instruction pointer (this time jmp)
![Page 325: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/325.jpg)
Advantages
� Advantages
� no system calls for thread handling
� thread-switches are very fast
� no change of memory configuration when switching threads
� can use specialized scheduling
![Page 326: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/326.jpg)
Advantages
� Advantages
� no system calls for thread handling
� thread-switches are very fast
� no change of memory configuration when switching threads
� can use specialized scheduling
![Page 327: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/327.jpg)
Advantages
� Advantages
� no system calls for thread handling
� thread-switches are very fast
� no change of memory configuration when switching threads
� can use specialized scheduling
![Page 328: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/328.jpg)
Advantages
� Advantages
� no system calls for thread handling
� thread-switches are very fast
� no change of memory configuration when switching threads
� can use specialized scheduling
![Page 329: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/329.jpg)
Advantages
� Advantages
� no system calls for thread handling
� thread-switches are very fast
� no change of memory configuration when switching threads
� can use specialized scheduling
![Page 330: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/330.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 331: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/331.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 332: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/332.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 333: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/333.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 334: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/334.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 335: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/335.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 336: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/336.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 337: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/337.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 338: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/338.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 339: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/339.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 340: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/340.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 341: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/341.jpg)
Disadvantages
� threads are not allowed to make direct syscalls since they might block
� ... one could make system-calls non-blocking
� ... but since this should work with unchanged (and unaware) OSs...
� sometimes you can find out if a syscall would block
� e.g. select-Systemcall
� before read is called: call select
� should read block: switch threads and check again later
� not very efficient and elegant
� Sometimes not
� Page faults
� if page not in memory, process will block
� if thread has an endless loop and does not free CPU...
![Page 342: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/342.jpg)
Implementation
Two and a half options:
� Userspace
� Kernelspace
� Mixed
![Page 343: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/343.jpg)
kernel mode threads
� No runtime system needed
� less code the user can break
� thread management in kernel
� more or less as in userspace
� but: kernel programmers by definition only write safe code
� thread creation and management via syscall
� takes longer than before
� thread-recycling
![Page 344: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/344.jpg)
kernel mode threads
� No runtime system needed
� less code the user can break
� thread management in kernel
� more or less as in userspace
� but: kernel programmers by definition only write safe code
� thread creation and management via syscall
� takes longer than before
� thread-recycling
![Page 345: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/345.jpg)
kernel mode threads
� No runtime system needed
� less code the user can break
� thread management in kernel
� more or less as in userspace
� but: kernel programmers by definition only write safe code
� thread creation and management via syscall
� takes longer than before
� thread-recycling
![Page 346: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/346.jpg)
kernel mode threads
� No runtime system needed
� less code the user can break
� thread management in kernel
� more or less as in userspace
� but: kernel programmers by definition only write safe code
� thread creation and management via syscall
� takes longer than before
� thread-recycling
![Page 347: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/347.jpg)
kernel mode threads
� No runtime system needed
� less code the user can break
� thread management in kernel
� more or less as in userspace
� but: kernel programmers by definition only write safe code
� thread creation and management via syscall
� takes longer than before
� thread-recycling
![Page 348: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/348.jpg)
kernel mode threads
� No runtime system needed
� less code the user can break
� thread management in kernel
� more or less as in userspace
� but: kernel programmers by definition only write safe code
� thread creation and management via syscall
� takes longer than before
� thread-recycling
![Page 349: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/349.jpg)
kernel mode threads
� No runtime system needed
� less code the user can break
� thread management in kernel
� more or less as in userspace
� but: kernel programmers by definition only write safe code
� thread creation and management via syscall
� takes longer than before
� thread-recycling
![Page 350: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/350.jpg)
kernel mode threads
� No runtime system needed
� less code the user can break
� thread management in kernel
� more or less as in userspace
� but: kernel programmers by definition only write safe code
� thread creation and management via syscall
� takes longer than before
� thread-recycling
![Page 351: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/351.jpg)
Kernel mode threads
![Page 352: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/352.jpg)
Hybrid solution
![Page 353: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/353.jpg)
Thread states
![Page 354: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/354.jpg)
Threads states
![Page 355: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/355.jpg)
Thread states
![Page 356: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/356.jpg)
Thread states
![Page 357: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/357.jpg)
Thread states
![Page 358: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/358.jpg)
Thread states
![Page 359: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/359.jpg)
Thread states
![Page 360: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/360.jpg)
Context Switching
![Page 361: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/361.jpg)
Process Creation
� at boot time (kernel threads, init processes)
� at request of a user (how?)
� also: start of a scheduled batch job (cronjob, how?)
![Page 362: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/362.jpg)
Process Creation
� at boot time (kernel threads, init processes)
� at request of a user (how?)
� also: start of a scheduled batch job (cronjob, how?)
![Page 363: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/363.jpg)
Process Creation
� at boot time (kernel threads, init processes)
� at request of a user (how?)
� also: start of a scheduled batch job (cronjob, how?)
![Page 364: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/364.jpg)
Process Creation at request of a user
via Syscall!
� UNIX/Linux: fork (exact copy)
� Windows: CreateProcess (new image)
� SWEB: fork (as soon as you have implemented it)
![Page 365: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/365.jpg)
Process Creation at request of a user
via Syscall!
� UNIX/Linux: fork (exact copy)
� Windows: CreateProcess (new image)
� SWEB: fork (as soon as you have implemented it)
![Page 366: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/366.jpg)
Process Creation at request of a user
via Syscall!
� UNIX/Linux: fork (exact copy)
� Windows: CreateProcess (new image)
� SWEB: fork (as soon as you have implemented it)
![Page 367: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/367.jpg)
What does the fork do?
Check http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html!!
![Page 368: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/368.jpg)
Process Creation via fork (on Unix / Linux / SWEB)
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html:
pid_t fork(void);
The fork() function shall create a new process. The new process (child process) shall be an
exact copy of the calling process (parent process) except as detailed below:
� unique PID
� copy of file descriptors
� semaphore state is copied
� shall be created with a single thread. If a multi-threaded process calls fork(), the new
process shall contain a replica of the calling thread and its entire address space, possibly
including the states of mutexes and other resources.
� parent and the child processes shall be capable of executing independently before either
one terminates.
� . . .
![Page 369: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/369.jpg)
fork/exec Return Value
http://pubs.opengroup.org/onlinepubs/9699919799/functions/fork.html:
pid_t fork(void);
Upon successful completion, fork() shall return 0 to the child process and shall return the
process ID of the child process to the parent process. Both processes shall continue to execute
from the fork() function. Otherwise, -1 shall be returned to the parent process, no child
process shall be created, and errno shall be set to indicate the error.
![Page 370: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/370.jpg)
Fork
pid_t child_pid;
child_pid = fork();
if (child_pid == -1) {
printf("fork failed\n");
} else if (child_pid == 0) {
printf("i’m the child\n");
} else {
printf("i’m the parent\n");
waitpid(child_pid,0,0); // wait
for child to die
}
� child does not know the parent
� parent knows the child
� parent waits for child to die (waitpid)
![Page 371: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/371.jpg)
Fork
pid_t child_pid;
child_pid = fork();
if (child_pid == -1) {
printf("fork failed\n");
} else if (child_pid == 0) {
printf("i’m the child\n");
} else {
printf("i’m the parent\n");
waitpid(child_pid,0,0); // wait
for child to die
}
� child does not know the parent
� parent knows the child
� parent waits for child to die (waitpid)
![Page 372: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/372.jpg)
Fork
pid_t child_pid;
child_pid = fork();
if (child_pid == -1) {
printf("fork failed\n");
} else if (child_pid == 0) {
printf("i’m the child\n");
} else {
printf("i’m the parent\n");
waitpid(child_pid,0,0); // wait
for child to die
}
� child does not know the parent
� parent knows the child
� parent waits for child to die (waitpid)
![Page 373: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/373.jpg)
Process Termination
� Normal exit (return value: zero)
� Error exit (return value: non-zero)
� Fatal error (e.g. segmentation fault)
� Killed by another process
![Page 374: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/374.jpg)
Process Termination
� Normal exit (return value: zero)
� Error exit (return value: non-zero)
� Fatal error (e.g. segmentation fault)
� Killed by another process
![Page 375: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/375.jpg)
Process Termination
� Normal exit (return value: zero)
� Error exit (return value: non-zero)
� Fatal error (e.g. segmentation fault)
� Killed by another process
![Page 376: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/376.jpg)
Process Termination
� Normal exit (return value: zero)
� Error exit (return value: non-zero)
� Fatal error (e.g. segmentation fault)
� Killed by another process
![Page 377: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/377.jpg)
Process Hierarchies
Some operating systems have hierarchies:
� implicit hierarchy from forking
� process groups in UNIX/Linux
� doesn’t exist in Windows
Implicit parent-child hierarchy on Unix/Linux:
� when parent dies,
all children, grand-children, grand-grand-children, . . . , die aswell
� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.
![Page 378: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/378.jpg)
Process Hierarchies
Some operating systems have hierarchies:
� implicit hierarchy from forking
� process groups in UNIX/Linux
� doesn’t exist in Windows
Implicit parent-child hierarchy on Unix/Linux:
� when parent dies,
all children, grand-children, grand-grand-children, . . . , die aswell
� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.
![Page 379: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/379.jpg)
Process Hierarchies
Some operating systems have hierarchies:
� implicit hierarchy from forking
� process groups in UNIX/Linux
� doesn’t exist in Windows
Implicit parent-child hierarchy on Unix/Linux:
� when parent dies,
all children, grand-children, grand-grand-children, . . . , die aswell
� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.
![Page 380: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/380.jpg)
Process Hierarchies
Some operating systems have hierarchies:
� implicit hierarchy from forking
� process groups in UNIX/Linux
� doesn’t exist in Windows
Implicit parent-child hierarchy on Unix/Linux:
� when parent dies,
all children, grand-children, grand-grand-children, . . . , die aswell
� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.
![Page 381: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/381.jpg)
Process Hierarchies
Some operating systems have hierarchies:
� implicit hierarchy from forking
� process groups in UNIX/Linux
� doesn’t exist in Windows
Implicit parent-child hierarchy on Unix/Linux:
� when parent dies,
all children, grand-children, grand-grand-children, . . . , die aswell
� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.
![Page 382: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/382.jpg)
Process Hierarchies
Some operating systems have hierarchies:
� implicit hierarchy from forking
� process groups in UNIX/Linux
� doesn’t exist in Windows
Implicit parent-child hierarchy on Unix/Linux:
� when parent dies, all children, grand-children, grand-grand-children, . . . , die aswell
� UNIX/Linux also cheats a bit: parent process typically inherits a processes’ children, etc.
![Page 383: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/383.jpg)
Process/Thread State
git grep TODO | sort
![Page 384: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/384.jpg)
Process/Thread State
git grep TODO | sort
� sort has to wait for input
![Page 385: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/385.jpg)
Process/Thread State
git grep TODO | sort
� sort has to wait for input
� what does the sort do in the meantime?
![Page 386: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/386.jpg)
Process/Thread State
git grep TODO | sort
� sort has to wait for input
� what does the sort do in the meantime?
� loop and check (busy wait)
![Page 387: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/387.jpg)
Process/Thread State
git grep TODO | sort
� sort has to wait for input
� what does the sort do in the meantime?
� loop and check (busy wait)
� sleep and get woken up
![Page 388: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/388.jpg)
Process/Thread State
git grep TODO | sort
� sort has to wait for input
� what does the sort do in the meantime?
� loop and check (busy wait)
� sleep and get woken up
� blocking the process makes sense
![Page 389: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/389.jpg)
Process/Thread State
git grep TODO | sort
� sort has to wait for input
� what does the sort do in the meantime?
� loop and check (busy wait)
� sleep and get woken up
� blocking the process makes sense
� do we actually block the process?
![Page 390: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/390.jpg)
Using Processes and Threads
![Page 391: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/391.jpg)
Processes vs. Threads
� Threads are more lightweight than processes
� Less independent than processes
� No protection
![Page 392: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/392.jpg)
Processes vs. Threads
� Threads are more lightweight than processes
� Less independent than processes
� No protection
![Page 393: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/393.jpg)
Processes vs. Threads
� Threads are more lightweight than processes
� Less independent than processes
� No protection
![Page 394: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/394.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 395: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/395.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 396: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/396.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 397: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/397.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 398: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/398.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 399: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/399.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 400: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/400.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 401: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/401.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 402: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/402.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 403: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/403.jpg)
Why threads?
� Things should happen in parallel - even within one application
� Example: text processing
� typing
� spell checking
� formatting on screen
� automatically saving
� Some of these things may block
� wait for mouse-click / keyboard press
� wait for disk
� etc.
![Page 404: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/404.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 405: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/405.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 406: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/406.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes
� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 407: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/407.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 408: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/408.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 409: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/409.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 410: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/410.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 411: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/411.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 412: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/412.jpg)
Why threads
� Make programming easier
� Split tasks in different blocks
� Like with processes� But they cooperate easily because of the shared address space
� Consumes less memory
� Attention: do not confuse shared memory (between processes) with shared address space
(between threads)
� Switching between threads can be faster
� No need to reconfigure memory
� May achieve better performance
![Page 413: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/413.jpg)
Example
![Page 414: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/414.jpg)
Example
while (TRUE)
{
get_next_request(&buf);
handoff_work(&buf);
}
while (TRUE)
{
wait_for_work(&buf);
look_for_page_in_cache(&buf,&page);
if (page_not_in_cache(&page))
read_page_from_disk(&buf,&page);
return_page(&page);
}
![Page 415: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/415.jpg)
Without Threads
Without threads,
� just one thread
� complicated program structure
� read content from disk may block process
� non-blocking read (polling!) decreases performance
![Page 416: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/416.jpg)
Without Threads
Without threads,
� just one thread
� complicated program structure
� read content from disk may block process
� non-blocking read (polling!) decreases performance
![Page 417: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/417.jpg)
Without Threads
Without threads,
� just one thread
� complicated program structure
� read content from disk may block process
� non-blocking read (polling!) decreases performance
![Page 418: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/418.jpg)
Without Threads
Without threads,
� just one thread
� complicated program structure
� read content from disk may block process
� non-blocking read (polling!) decreases performance
![Page 419: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/419.jpg)
Non-Blocking Read
while (TRUE) { // VERY simplified
get_next_event(&buf);
if (is_request_event(&buf)) {
if (page_not_in_cache(&page)) {
request_page_from_disk(&buf,&page);
save_request_in_table(&buf);
} else {
return_page(&page);
}
} else if (is_disk_event(&buf)) {
find_request_in_table(&buf);
mark_requeust_as_done(&buf);
return_page(&page);
} else if (is_...
![Page 420: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/420.jpg)
Non-Blocking Read
� Finite-state-machine!
� Actually simulates threads
� Better: use multithreading
![Page 421: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/421.jpg)
Non-Blocking Read
� Finite-state-machine!
� Actually simulates threads
� Better: use multithreading
![Page 422: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/422.jpg)
Non-Blocking Read
� Finite-state-machine!
� Actually simulates threads
� Better: use multithreading
![Page 423: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/423.jpg)
Take Aways
� Processes divide resources amongst themselves (except processor time)
� Threads divide processor time amongst themsevles (and a few resources)
� Building block of modern multi-threading are context switches
� Operating system creates illusions
� for the hardware: there is only 1 thread and a lot of interrupts
� for the userspace: we can have an arbitrary number of threads
![Page 424: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/424.jpg)
Take Aways
� Processes divide resources amongst themselves (except processor time)
� Threads divide processor time amongst themsevles (and a few resources)
� Building block of modern multi-threading are context switches
� Operating system creates illusions
� for the hardware: there is only 1 thread and a lot of interrupts
� for the userspace: we can have an arbitrary number of threads
![Page 425: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/425.jpg)
Take Aways
� Processes divide resources amongst themselves (except processor time)
� Threads divide processor time amongst themsevles (and a few resources)
� Building block of modern multi-threading are context switches
� Operating system creates illusions
� for the hardware: there is only 1 thread and a lot of interrupts
� for the userspace: we can have an arbitrary number of threads
![Page 426: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/426.jpg)
Take Aways
� Processes divide resources amongst themselves (except processor time)
� Threads divide processor time amongst themsevles (and a few resources)
� Building block of modern multi-threading are context switches
� Operating system creates illusions
� for the hardware: there is only 1 thread and a lot of interrupts
� for the userspace: we can have an arbitrary number of threads
![Page 427: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/427.jpg)
Take Aways
� Processes divide resources amongst themselves (except processor time)
� Threads divide processor time amongst themsevles (and a few resources)
� Building block of modern multi-threading are context switches
� Operating system creates illusions
� for the hardware: there is only 1 thread and a lot of interrupts
� for the userspace: we can have an arbitrary number of threads
![Page 428: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/428.jpg)
Take Aways
� Processes divide resources amongst themselves (except processor time)
� Threads divide processor time amongst themsevles (and a few resources)
� Building block of modern multi-threading are context switches
� Operating system creates illusions
� for the hardware: there is only 1 thread and a lot of interrupts
� for the userspace: we can have an arbitrary number of threads
![Page 429: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/429.jpg)
![Page 430: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/430.jpg)
Bibliography
[1] Thomas Haigh. Multicians.org and the history of operating systems. online, 9 2002. URL
http://www.cbi.umn.edu/iterations/haigh.html.
[2] Emerson Pugh, Lyle Johnson, and John Palmer. IBM’s 360 and Early 370 Systems (History
of Computing). The MIT Press, 2003.
![Page 431: Operating Systems - Introduction, Processes, Threads](https://reader033.vdocument.in/reader033/viewer/2022060400/6294ace27a87e3350851f527/html5/thumbnails/431.jpg)