Processes and Threads
Processes and Threads
Applications
Operating System
CPU Hardware
Disk RAM
2
Program
• Program is a file describing a computation
– Executable code (machine instructions)
– Data (info manipulated by the instructions
3
Process
• The process is the OS abstraction for execution
– It is the unit of execution
– It is the unit of scheduling
4
Process = Program ???
• A program is passive
– Code + data
• A process is alive:
– Code + data + stack +registers + PC…
• Same program can be run simultaneously
– 2 processes
• Why processes?
5
Process Components
• A process contains all the state for a program in execution
– An address space containing
• Static memory:
– The code and input data for the executing program
• Dynamic memory:
– The memory allocated by the executing program
– An execution stack encapsulating the state of procedure calls
– Control registers such as the program counter (PC)
– A set of general-purpose registers with current values
– A set of operating system resources
• Open files, network connections, etc.
6
Address Space (memory abstraction)
0xFFFFFFFF
Stack
SP
Dynamic
Address Heap
Space (Dynamic Memory Alloc)
Static Data
(Data Segment)
Static
Code PC
(Text Segment)
0x00000000
7
Process Execution State
• A process is born, executes for a while, and then dies
8
Execution state (cont’d)
• As a process executes, it moves from state to state
– Unix: “ps -x”: STAT column indicates execution state
– What state do you think a process is in most of the time?
– How many processes can a system support?
9
Execution State Graph
Create
Process
New Ready
I/O Done
Unschedule Schedule
Process Process Waiting
I/O, Page
Terminated Running Fault, etc.
Process
Exit
10
How does the OS support this model?
We will discuss three issues:
1. How does the OS represent a process in the kernel?
– The OS data structure representing each process is called
the Process Control Block (PCB)
11
PCB Data Structure
• PCB is also where OS keeps all of a process’ hardware execution
state when the process is not running
• Process ID (PID)
• Execution state
• Hardware state: PC, SP, regs
• Location in memory
• Scheduling info
• User info
• Pointers for state queues
• Etc.
• This state is everything that is needed to restore the hardware to
the same configuration it was in when the process was switched
out of the hardware
12
xv6/proc.h: struct proc
13
How to pause/resume a process?
• When a process is running, its dynamic state is in memory and
some hardware registers
– Hardware registers include program counter, stack pointer, control registers, data
registers, …
– To stop and restart a process, we need to completely restore this state
16
xv6: swtch()
• Kernel/swtch.S
17
How does the OS track process states?
• The OS maintains a collection of queues that represent the state
of all processes in the system
18
State Queues
Ready Queue Firefox PCB X Server PCB Outlook PCB
Console Queue
Sleep Queue There may be many wait queues,
. one for each type of wait (disk,
. console, timer, network, etc.)
.
19
Process System Call APIs
• Process creation: how to create a new process?
• Other
– e.g., set quotas or priorities, examine usage, …
20
Process Creation
• A process is created by another process
– Parent is creator, child is created (Unix: ps “PPID” field)
– What creates the first process (Unix: init (PID 1))?
• After creating a child, the parent may either wait for it to finish
its task or continue in parallel (or both)
21
Process Creation: Windows
• The system call on Windows for creating a process is called,
surprisingly enough, CreateProcess:
BOOL CreateProcess(char *prog, char *args) (simplified)
• CreateProcess
– Creates and initializes a new PCB
– Creates and initializes a new address space
– Loads the program specified by “prog” into the address space
– Copies “args” into memory allocated in address space
– Initializes the saved hardware context to start execution at main (or
wherever specified in the file)
– Places the PCB on the ready queue
22
Process Creation: Unix
• In Unix, processes are created using fork(): int fork()
• fork()
– Creates and initializes a new PCB
– Creates a new address space
– Initializes the address space with a copy of the entire contents of the
address space of the parent
• Child has the same memory and registers: same SP & PC
– Initializes the kernel resources to point to the resources used by parent
(e.g., open files)
– Places the PCB on the ready queue
24
Example Output
[well ~]$ gcc t.c
[well ~]$ ./a.out
My child is 486
Child of a.out is 486
25
Duplicating Address Spaces
Parent Child
26
Divergence
PC printf(“parent”); printf(“parent”);
} }
Parent Child
27
Example Continued
[well ~]$ gcc t.c
[well ~]$ ./a.out
My child is 486
Child of a.out is 486
[well ~]$ ./a.out
Child of a.out is 498
My child is 498
28
Why fork()?
• Simple approach to enable concurrent execution
• Useful when the child…
– Is cooperating with the parent
– Relies upon the parent’s data to accomplish its task
• exec()
– Stops the current process
– Loads the program “prog” into the process’ address space
– Initializes hardware context and args for the new program
– Places the PCB onto the ready queue
– Note: It does not create a new process
30
Process Termination
• All good processes must come to an end. But how?
– Unix: exit(int status), Windows: ExitProcess(int status)
31
wait() a second…
• Often it is convenient to pause until a child process has finished
– Think of executing commands in a shell
33
Some issues with processes
• Creating a new process is costly because of new address space
and data structures that must be allocated and initialized
– Recall struct proc in xv6
34
Process-associated overheads
• Context switch: high Process 1 Process n
CPU CPU
– CPU state: low State State
– Memory / IO state: high ….
Mem. Mem.
State State
• Process creation: high
I/O I/O
• Protection State State
– CPU: yes
– Memory / IO: yes
• Sharing overhead: high OS
scheduler
– At least one context switch
35
Parallel Programs
• Recall our Web server example that forks off copies of itself to
handle multiple simultaneous requests
36
Rethinking Processes
• What is similar in these cooperating processes?
– They all share the same code and data (address space)
– They all share the same privileges
– They all share the same resources (files, sockets, etc.)
37
Threads
• Separate execution and resource container roles
– The thread defines a sequential execution stream within a process
(PC, SP, registers)
– The process defines the address space, resources, and general process
attributes (everything but threads)
– CPU: yes
– Memory / IO: no
OS
• Sharing overhead: low scheduler
– Due to low thread switch
39
Thread-associated overheads
Process 1 Process 1
• Context switch: low Threads
– CPU: yes
– Memory / IO: no
OS
• Sharing overhead: low scheduler
– Due to low thread switch
40
Recap: Process Address Space
0xFFFFFFFF
Stack
SP
Address Heap
Space (Dynamic Memory Alloc)
Static Data
(Data Segment)
Code PC
(Text Segment)
0x00000000
41
Threads in a Process
Heap
Static Data
PC (T3)
PC (T2) Code
PC (T1)
42
Threads: Concurrent Servers
• Using fork() to create new processes to handle requests in
parallel is overkill for such a simple task
while (1) {
int sock = accept();
if ((child_pid = fork()) == 0) {
Handle client request
Close socket and exit
} else {
Close socket
}
}
43
Threads: Concurrent Servers
• Instead, we can create a new thread for each request
web_server() {
while (1) {
int sock = accept();
thread_fork(handle_request, sock);
}
}
handle_request(int sock) {
Process request
close(sock);
}
44
Sample Thread Interface
• thread_fork(procedure_t)
– Create a new thread of control
– Also thread_create(), thread_setstate()
• thread_stop()
– Stop the calling thread; also thread_block
• thread_start(thread_t)
– Start the given thread
• thread_yield()
– Voluntarily give up the processor
• thread_exit()
– Terminate the calling thread; also thread_destroy
45
Implementing threads
• Kernel-Level Threads
– All thread operations are implemented in the kernel
– The OS schedules all of the threads in the system
• Requires a full thread control block (TCB) for each thread
– Don’t have to separate from processes
46
Alternative: User-Level Threads
• Implement and manage threads entirely at user level
– Kernel knows nothing about user-level threads
47
KLT and ULT combined
48
Summary KLT vs. ULT
• Kernel-level threads
– Integrated with OS (informed scheduling)
– Slow to create, manipulate, synchronize
• User-level threads
– Fast to create, manipulate, synchronize
– Not integrated with OS (uninformed scheduling)
49
Simultaneous multithreading &
hyperthreading
• Traditional multithreading can start execution of instructions from only
a single thread at a given cycle
– Low utilization if not enough instructions from a thread to dispatch in one
cycle
– Bad for machines with multiple execution units (i.e., superscalar architecture)
• Idea: dispatch instructions from multiple threads in the same cycle to
keep multiple execution units utilized
– Hirata et al., “An elementary processor architecture with simultaneous instruction issuing from multiple
threads,” ISCA 1992
– Tullsen et al., “Simultaneous Multithreading: maximizing on-chip parallelism,” ISCA 1995
50
Multithreading
Time
• Intra-thread dependencies
• Single thread performance suffers
51
SMT
Time
52
SMT scalability
53
Commercial SMT implementations
• Intel Pentium 4 (Hyperthreading)
• IBM POWER5
• Intel Nehalem
54
Intel Pentium 4 HT