cs162 operating systems and systems...

16
CS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes, Threads, and Address Spaces January 27, 2014 Anthony D. Joseph http://inst.eecs.berkeley.edu/~cs162

Upload: others

Post on 28-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

CS162 Operating Systems andSystems Programming

Lecture 2 (extra)

Concurrency:Processes, Threads, and Address Spaces"

January 27, 2014!Anthony D. Joseph!

http://inst.eecs.berkeley.edu/~cs162!

Page 2: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.2!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Operating System Roles (Recap)"•  OS as a Traffic Cop:!

– Manages all resources!–  Settles conflicting requests for resources!–  Prevent errors and improper use of the computer!

•  OS as a facilitator (“useful” abstractions):!–  Provides facilities/services that everyone needs!–  Standard libraries, windowing systems!– Make application programming easier, faster, less error-prone!

•  OS as government:!–  Provides a share of expensive resources as needed!– Nudges users toward standard interfaces, allowing improved sharing!

Page 3: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.3!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Migration of OS Concepts and Features"

Page 4: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.4!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Definitions"•  Threads: "

– Unit of execution and scheduling!–  Independent Fetch/Decode/Execute loop!– Operate in an address space!

Page 5: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.5!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Definitions"•  Threads: "

– Unit of execution and scheduling!–  Independent Fetch/Decode/Execute loop!– Operate in an address space"

•  Uniprogramming: one thread at a time!•  Multiprogramming: more than one thread at a time!

Page 6: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.6!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Definitions"•  Threads: "

– Unit of execution and scheduling!–  Independent Fetch/Decode/Execute loop!– Operate in an address space"

•  Uniprogramming: one thread at a time!•  Multiprogramming: more than one thread at a time!•  Process: unit of resource allocation and execution!

– Owns memory (address space), file descriptors, file system context, …!

– Encapsulate one or more threads sharing process resources!

Page 7: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.7!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Definitions"•  Uniprogramming: one thread at a time!•  Multiprogramming: more than one thread at a time!

•  Virtual “CPU” structure holds:!– Program Counter (PC), Stack Pointer (SP)!– Registers (Integer, Floating point, others…?)!

•  Virtual “CPU” switch:!– Save PC, SP, and registers in current state block!– Load PC, SP, and registers from new state block!

Page 8: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.8!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Definitions"•  Uniprogramming: one thread at a time!•  Multiprogramming: more than one thread at a time!

•  Virtual “CPU” structure holds:!– Program Counter (PC), Stack Pointer (SP)!– Registers (Integer, Floating point, others…?)!

•  Virtual “CPU” switch:!– Save PC, SP, and registers in current state block!– Load PC, SP, and registers from new state block!

•  Cooperative multiprogramming:"– Switch on voluntary yield, I/O, other things!

•  Preemptive multiprogramming:"– Switch on timer, voluntary yield, I/O, other things!

Page 9: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.9!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Providing Illusion of Separate Address Space:Load new Translation Map on Switch"

Prog 1"Virtual"

Address"Space 1"

Prog 2"Virtual"

Address"Space 2"

Code"Data"Heap"Stack"

Code"Data"Heap"Stack"

Data 2"

Stack 1"

Heap 1"

OS heap & "Stacks"

Code 1"

Stack 2"

Data 1"

Heap 2"

Code 2"

OS code"

OS data"Translation Map 1" Translation Map 2"

Physical Address Space"

Page 10: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.10!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

5min Break"

Page 11: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.11!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Definitions"•  Traditional UNIX Process: “HeavyWeight Process”"

– A single, sequential stream of execution (one thread) in its own address space!

Process Control Block"

Page 12: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.12!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Definitions"•  Traditional UNIX Process: “HeavyWeight Process”"

– A single, sequential stream of execution (one thread) in its own address space!

•  Scheduling: Give out CPU time to different processes!

– Only one process “running” at a time!

•  Protection: Give pieces of resources to different processes !

– Controlled access to non-CPU resources!

Process Control Block"

Page 13: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.13!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

HeavyWeight Process Scheduling"

•  PCBs move from queue to queue as they change state!– Decisions about which order to remove from queues are

Scheduling decisions!– Many algorithms possible (few weeks from now)!

Page 14: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.14!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Inter-Process Communication"

•  Two mechanisms:!– Shared-Memory Mapping!

»  Accomplished by mapping addresses to common DRAM!» Read and Write through memory – complex but low overhead!

– Message Passing!» send() and receive() messages across communication channel!» Works across global network!

Proc 1" Proc 2" Proc 3"

Page 15: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.15!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Single and Multithreaded Processes"

•  Multithreading: a single program made up of a number of different concurrent activities !

•  “Active” threads encapsulate concurrency (“LightWeight Process”)!•  “Passive” address spaces encapsulate protection!•  Heavyweight Process ≡ Process with one thread!

!

Page 16: CS162 Operating Systems and Systems …inst.eecs.berkeley.edu/~cs162/sp14/Lectures/lec02-extra.pdfCS162 Operating Systems and Systems Programming Lecture 2 (extra) Concurrency: Processes,

Lec 2e.16!1/27/14! Anthony D. Joseph ! !CS162 ! ©UCB Spring 2014!

Classification"

•  Real operating systems have either!– One or many address spaces!– One or many threads per address space!

Mach, OS/2, HP-UX, Win NT to 8, Solaris, OS X, Android, iOS"

Embedded systems (Geoworks, VxWorks,

JavaOS,etc)"JavaOS, Pilot(PC)"

Traditional UNIX"MS/DOS, early Macintosh"

Many"

One"

# threads"Per AS:"

Many"One"

# of

add

r sp

aces

:"