03 processes threads v2
TRANSCRIPT
-
8/13/2019 03 Processes Threads v2
1/55
Processesand
ThreadsProf. Van Renesse and Sirer
CS 4410
Cornell University
-
8/13/2019 03 Processes Threads v2
2/55
Fun Starts Here!
What involvesstarting a programor
running a program?
which are misnomers
How can I run multiple processes on one
computer?
Its all aboutdesign and efficientimplementation of abstractions
-
8/13/2019 03 Processes Threads v2
3/55
What is a Process?
A process is an abstractionof a computer
-
8/13/2019 03 Processes Threads v2
4/55
Abstractions
A file is an abstract disk
A socket is an abstract network endpoint
A window is an abstract screen
Abstractions hide implementation detailsbut expose (most of) the power
-
8/13/2019 03 Processes Threads v2
5/55
Process Abstraction
ENVIRONMENT ADDRESS
SPACE
REGISTERS
CPU
STATE
CONTROL
-
8/13/2019 03 Processes Threads v2
6/55
Process Interface
CreateProcess(initial state) processID
SendSignal(processID, Signal)
GetStatus(processID)
runningStatusTerminate(processID)
WaitFor(processID) completionStatus
ListProcesses() [ pid1, pid2, ]
-
8/13/2019 03 Processes Threads v2
7/55
Kernel implements processes!
P1
OS KERNEL
P2 P3
Supervisor Mode
User Mode
Kernel is only part of the operating system
-
8/13/2019 03 Processes Threads v2
8/55
Emulation
One option is for the hardware to simulate
multiple instances of the same or other
hardware
Useful for debugging, emulation of ancient
hardware, etc.
But too inefficient for modern-day daily use
-
8/13/2019 03 Processes Threads v2
9/55
CPU runs each process directly
But somehow each process has its own
Registers
Memory
I/O resources
thread of control
-
8/13/2019 03 Processes Threads v2
10/55
(Simplified) RAM Layout
0x0
0x80000000
KERNEL
P2
P1
P3
Base/Bound register
Supervisor mode
-
8/13/2019 03 Processes Threads v2
11/55
Typical Address Space Layout(similar for kernel and processes)
CODE
DATA
STACK
0
-
8/13/2019 03 Processes Threads v2
12/55
-
8/13/2019 03 Processes Threads v2
13/55
Abstract life of a process
New
Runnable
Zombie
interrupt ---
descheduling
dispatchRunning
Waiting
-
8/13/2019 03 Processes Threads v2
14/55
createProcess(initial state)
Allocate memory for address space
Initialize address space
program vs fork
program process
Allocate ProcessID
Allocate process control blockPut process control block on the run queue
-
8/13/2019 03 Processes Threads v2
15/55
How does a process terminate?
External:
Terminate(ProcessID)
SendSignal(signal) with no handler set up
Using up quota
Internal:
Exit(processStatus)
Executing an illegal instruction
Accessing illegal memory addresses
-
8/13/2019 03 Processes Threads v2
16/55
For now: one process running
at a time (single core machine)
Kernel runs
Switch to process 1
Trap to kernelSwitch to another (or same) process
Trap to kernel
etc. Context-switches
P1 K P2 K P2 KK P1
-
8/13/2019 03 Processes Threads v2
17/55
Processor Status Word
Supervisor Bit or Level
Interrupt Priority Level or Enabled Bit
Condition Codes (result of compare ops)
Supervisor can update any part, but user can
only update condition codesHas to be saved and restored like any other register!
-
8/13/2019 03 Processes Threads v2
18/55
Time-Sharing
Illusion: multiple processes run at same
time
Reality: only one process runs at a time
For no more than a few 10s of milliseconds
But this can happen in parallel to another
process waiting for I/O!
Why time-share?
-
8/13/2019 03 Processes Threads v2
19/55
Kernel Operation (conceptual)
Initialize devices
InitializeFirst Process
For ever
while device interrupts pending
handle device interrupts
while system calls pending
handle system calls
if run queue is non-empty
select a runnable process and switch to it
otherwise
wait for device interrupt
-
8/13/2019 03 Processes Threads v2
20/55
Invariants
Supervisor mode PC points to kernel code
Equivalently: PC points to user code user mode
User code runs with interrupts enabled
For simplicity: Kernel code runs with interrupts
disabled (for now)
-
8/13/2019 03 Processes Threads v2
21/55
Dispatch: kernel process
Software:
CurProc := &PCB of current process
Set user base/bound register
Restore process registers
Execute ReturnFromInterrupt instruction
Hardware: Sets user mode
Enables interrupts
Restores program counter
-
8/13/2019 03 Processes Threads v2
22/55
Trap process kernel
Hardware:
Disables interrupts
Sets supervisor mode
Saves user PC and SP on kernel stack why not on process stack?
Sets kernel stack pointer
Sets PC to kernel-configured position
Software:
Save process registers in PCB of CurProc
Back to kernel main loop
-
8/13/2019 03 Processes Threads v2
23/55
Causes for traps
Clock interrupt
Device interrupt
System callPrivileged instruction
Divide by zero
Bad memory access
-
8/13/2019 03 Processes Threads v2
24/55
System calls
How does a process specify what system
call to invoke and what parameters to use?
How does the kernel protect itself and
other processes?
How does the kernel return a result to the
process?
How does the kernel prevent accidentally
returning privacy sensitive data?
-
8/13/2019 03 Processes Threads v2
25/55
Class Projects
Implement sleep(delay) system call
Implement a debugger
Implement SendSignal(pid, signal)
-
8/13/2019 03 Processes Threads v2
26/55
How Much To Abstract
Unix and Windows provide processes that look like
idealized machines, with nice looking file abstractions,
network abstractions, graphical windows, etc.
Xen, KVM, etc. provide processes that look just like real
hardware
virtualization
Requires different kinds of things from kernels
Unix/Windows: implement files, network protocols, windowmanagement
Xen/KVM/: emulate hardware
-
8/13/2019 03 Processes Threads v2
27/55
Virtual Machine Abstraction
Virtual Machine Monitor kernel
Unix Kernel Windows NT Kernel
P1 P2 P3 P4 P5
-
8/13/2019 03 Processes Threads v2
28/55
Things to emulate
Supervisor mode
Base/Bound registers
Device registers
Hardware can help Multi-level supervisor
Multi-level base/bound
FLASH / ROM
BLOCK OF RAM
BLOCK OF RAM
DEVICE REGISTERS
BITMAP / SCREEN
-
8/13/2019 03 Processes Threads v2
29/55
Processes Under Unix/Linux
Fork() system call to create a new process
Old process called parent, new process called child
int fork() clonesthe invoking process:
Allocates a new PCB and process ID
Allocates a new address space
copies the parents address space into the childs
in parent, fork() returns PID of child
in child, fork() returns a zero.int fork() returns TWICE!
-
8/13/2019 03 Processes Threads v2
30/55
-
8/13/2019 03 Processes Threads v2
31/55
Bizarre But Real
$ cc a.c
$ ./a.out
The child of 23873 is 23874My child is 23874
Parent
Child
Kernel
fork()
retsys
v0=0v0=23874
-
8/13/2019 03 Processes Threads v2
32/55
Exec()
Fork() gets us a new address space
int exec(char *programName) completes the picture
throws away the contents of the calling address space
replaces it with the program in file named by programName
starts executing at header.startPC
PCB remains the same otherwise (same PID)
Pros: Clean, simpleCon: duplicate operations
-
8/13/2019 03 Processes Threads v2
33/55
-
8/13/2019 03 Processes Threads v2
34/55
Preparing a Program
Source
files
compiler/
assembler
Object
files
Linker
PROGRAM
An executable file
in a standard format,
such as ELF on Linux,
Microsoft PE on Windows
Header
Code
Initialized data
BSS
Symbol table
Line numbers
Ext. refs
static
libraries
(libc)
-
8/13/2019 03 Processes Threads v2
35/55
Running a program
Every OS provides aloaderthat is capable of convertinga given program into an executing instance, a process
A program in execution is called a process
The loader: reads and interprets the executable file
Allocates memory for the new process and sets processs memory
to contain code & data from executable
pushesargc,argv,envpon the stack sets the CPU registers properly & jumps to the entry point
-
8/13/2019 03 Processes Threads v2
36/55
-
8/13/2019 03 Processes Threads v2
37/55
Process Termination, part 1
Process executes last statement and calls exit syscall
Processresources are deallocated by operating system
Parent may terminate execution of child process (kill)
Child has exceeded allocated resources
Task assigned to child is no longer required
If parent is exiting
Some OSes dont allow child to continue if parent terminates
All children terminated - cascading termination
-
8/13/2019 03 Processes Threads v2
38/55
Process Termination, part 2
Process first goes intozombie state
Parent can wait for zombie children
Syscall: wait() (pid, exit status)
After wait() returns, PCB of child is garbage
collected
-
8/13/2019 03 Processes Threads v2
39/55
Class Project
Write a simple command line interpreter
-
8/13/2019 03 Processes Threads v2
40/55
Multiple Cores
Modern computers often have several if not
dozens of cores
Each core has its own registers, but cores
share memory and devices
Cores can run user processes in parallel
Cores need to synchronize access to PCBsand devices
-
8/13/2019 03 Processes Threads v2
41/55
Multi-Core Architecture
BUS
CORE 1 CORE 2 CORE 3
RAM FLASH/RO
M
SCREEN
BUFFER
DISK
Abstraction
-
8/13/2019 03 Processes Threads v2
42/55
Abstraction
multi-threaded process
THREAD 1 THREAD 2 THREAD 3
ENVIRONMENT ADDRESS SPACE
(MEMORY)
CPU + registers
-
8/13/2019 03 Processes Threads v2
43/55
-
8/13/2019 03 Processes Threads v2
44/55
44
Processes and Address Spaces
What happens when Apache wants to
run multiple concurrent computations ?
Emacs Mail
Kernel
User
0x80000000
0xffffffff
Apache
0x00000000
0x7fffffff
0x00000000
0x7fffffff
0x00000000
0x7fffffff
-
8/13/2019 03 Processes Threads v2
45/55
45
Processes and Address Spaces
Two heavyweight address spaces for two
concurrent computations ?
Emacs Mail User
0x80000000
0xffffffff
Apache
0x00000000
0x7fffffff
0x00000000
0x7fffffff
0x00000000
0x7fffffff
Apache
Kernel
-
8/13/2019 03 Processes Threads v2
46/55
46
Processes and Address Spaces
We can eliminate duplicate addressspaces and place concurrent
computations in the same address space
Emacs Mail User
0x80000000
0xffffffff
Apache
0x00000000
0x7fffffff
0x00000000
0x7fffffff
0x00000000
0x7fffffff
Apache
Kernel
-
8/13/2019 03 Processes Threads v2
47/55
Architecture
Process consists of
One address space containing chunks of memory
Shared I/O resources
Multiple threads Each with its own registers, in particular PC and SP
Each has its own stack in the address space
Code and data is shared
Other terms for threads Lightweight Process
Thread of Control
Task
-
8/13/2019 03 Processes Threads v2
48/55
Memory Layout
CODE
DATA
STACK 1
STACK 3
STACK 2
SP
PC
-
8/13/2019 03 Processes Threads v2
49/55
-
8/13/2019 03 Processes Threads v2
50/55
Separation of Thread and
-
8/13/2019 03 Processes Threads v2
51/55
51
Separation of Thread and
Process conceptsConcurrency (multi-threading) is useful for:
improving program structure
handling concurrent events (e.g., web requests)
building parallel programs
So, multi-threading is useful even on a uniprocessor
To be useful, thread operations have to be fast
-
8/13/2019 03 Processes Threads v2
52/55
How to implement?
Two extreme solutions Kernel threads:
Allocate a separate PCB for each thread
Assign each PCB the same base/size registers
Also copy I/O resources, etc.
User threads:
Built a miniature O.S. in user space
User threads are (generally) more efficient
Why?
Kernel threads simplify system call handling and scheduling
Why?
-
8/13/2019 03 Processes Threads v2
53/55
-
8/13/2019 03 Processes Threads v2
54/55
-
8/13/2019 03 Processes Threads v2
55/55
Things to Think about
Scheduling
While runnable process / thread runs when?
Coordination
How do cores / threads synchronize access to
shared memory and devices?