Lec2 Proc
Lec2 Proc
Lec2 Proc
OS: A Reminder
OS
Manages resources
Involves asynchronous and sometimes parallel
activities
Operating Systems 2
Users, Programs, Processes
Users have accounts on the system
Users write programs, then launch
programs
Different users may launch same program
One user may launch many instances of the
same program
Process:
= an executing program
= a program in action
= a running program
Operating Systems 3
Analogy
Program: steps for attending the lecture
1. walk to Siebel Center Building
2. enter 1404 Lecture Room
3. find a seat
4. listen and take notes
Process: instance of above program in action
= attending the lecture
Action
Right now, you are in the middle of this process!
You have a “condition” that changes every second!
Different processes from the same program may behave
differently
Operating Systems 4
An OS runs many processes
simultaneously
Listing all running processes
Windows: Task Manager
Unix/Linux> “ps –all”
PID TTY TIME CMD
1625 pts/6 0:02 tcsh
17246 pts/1 0:35 pine
1272 pts/7 0:00 ps
8865 pts/3 0:01 emacs
Operating Systems 5
Concretely! Can we define the
term “process”?
OK, a process consists of:
1. The program code (does not change, ideally)
2. The Data
3. The Program Counter
4. The Stack
5. The Register values
R1 R2 R3 R4
PC (hex): 00d0
Operating Systems 7
Hmm… how does one “play”
around with a process?
A process is an abstraction for sequence of
operations that implement a computation.
There are 2 types of processes:
OS processes executing system code
User processes executing user code
A user process may be given kernel privilege sometimes
(e.g., once it executes a TRAP)
Regardless of type, a process may be manipulated,
suspended, scheduled and terminated
By whom? By the OS, as well as by other processes!
Operating Systems 8
Process Management
Operating Systems 10
Forks
PID: 837
int parentpid;
int childpid;
Operating Systems 11
Forks: create a child!
PID: 837 PID: 839 : New Process!
int parentpid; int parentpid;
int childpid; int childpid;
Operating Systems 12
Forks: create a child!
PID: 837 PID: 839 : New Process!
int parentpid; int parentpid;
int childpid; int childpid;
PID 837
PID 839
Operating Systems 16
Process Creationism
When creating a process, it needs resources such as CPU,
memory files, I/O devices
Process can get resources from the OS or from the parent
process (e.g., CPU, disk)
When getting resources from a parent, the child process is
restricted to a subset of parent resources
Prevents many processes from overloading system
When creating a new process, execution possibilities are
Parent waits until child has terminated. $emacs
Parent continues concurrently with child. $ emacs&
When creating a new process, address space possibilities:
Child process is duplicate of parent process
Child process has a new, different program loaded into it, e.g.,
execve() system call
Operating Systems 17
Vs. The Terminator
When a process finishes last statement, it asks OS
to delete it
Child process may return output to parent process,
and then all child’s resources are de-allocated.
Other termination possibilities: Abort invoked by
parent process
Child has exceeded its usage of some resources
Task assigned to child is no longer required
Parent is exiting, and OS does not allow child to continue
without parent
Windows also kills parent when child is killed (not in Unix/Linux)
Operating Systems 18
Process: A Manipulatable
Object
OS manipulates a process as if it is an
object with a condition
Operating Systems 20
Process State
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
blocked: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
terminated: The process has finished execution
Operating Systems 21
Diagram of Process State
Operating Systems 22
The Process Model
(as seen in memory)
(as seen by CPU)
Operating Systems 23
A process’ condition consists of
Process State
new, ready, running, waiting, halted;
Program Counter
the address of the next instruction to be executed for this process;
CPU Registers
index registers, stack pointers, general purpose registers;
CPU Scheduling Information
process priority and pointer;
Memory Management Information All present in
base/limit information; OS-maintained data
Accounting Information structure called
PCB=process control
time limits, process number; owner
block,
I/O Status Information for each process that
list of I/O devices allocated to the process;
has not been terminated
Operating Systems 24
Process Control Block (PCB)
Application
Kernel space/level
Portable OS Layer
Machine-dependent layer
Operating Systems 26
Main
Memory
Main Memory/ 1GB
Set aside
for OS/kernel
One (common) use only
approach
Kernel is high memory
User is low memory
PID 901
What restrictions (code,data,
apply? stack)
PID 902
(code,data,
stack)
read(f, buf, nbytes)
0
Operating Systems 27
Process Address Space
Program segments
PID 901 0xffff….
Text=static (code,data,
stack) Kernel space
Data=static data
Heap=dynamic data Stack
Stack=dynamic
Lots of flexibility
Allows stack growth
Allows heap growth Heap
Code & Data
No predetermined division
0x0000…
Why is this needed?
Operating Systems 28
Remember system call libraries?
Process Scheduling
Objective of multiprogramming – maximal
CPU utilization, i.e., have always a process
running
Objective of time-sharing – switch CPU
among processes frequently enough so that
different users can use the single CPU as if
they had their own
Need Context Switching
Operating Systems 29
Context Switch
Switch CPU from one process to another
Context of a process represented in the PCB
Performed by scheduler
It does the following:
1. saves PCB of the old process;
2. loads PCB of the new process;
3. flushes memory cache;
4. change memory mapping (TLB)
Context switch is expensive(1-1000 microseconds)
No useful work is done (pure overhead)
Can become a bottleneck
Real life analogy?
Your switching back and forth between HW’s for different courses
Needs hardware support
Operating Systems 30
Process Context Switch
OS jobs PCB-1
PCB-0
Interrupt/system call
exec
Save PCB-0
idle
idle
exec
Operating Systems 31
“Process”? That’s it!
It’s one executing instance of a “program”
It’s separate from other instances
It can started/manipulated/killed by other
processes
It can be started/manipulated/killed by by
them
Also covered
Process model, fork, PCB, context switch
Operating Systems 32
Threads for this lecture
What is a thread?
Examples of using threads
Thread implementations
Operating Systems 33
Hmm, what was that
“Process” again?
It’s one executing instance of a “program”
Consists of code, data, heap, stack, PC, regs, and a
little other information in the PCB
It can start/manipulate/kill other processes
What’s in a process?
Process control block
Pointers to Code (text), data, stack, heap
Process state, priority, accounting
Program counter, register variables, stack pointers, etc
Open files and devices
Operating Systems 34
Threads
Processes do not share resources very much (or not at
all), therefore context switching cost is very high.
Inefficient!
Concept of Thread:
A process can consist of many “threads”
Thread is a light-weight process
A Thread comprises
Thread ID
Program counter
Register set
Stack space
Threads within same process share
Code section
Data section
OS resources such as open files, signals belonging to the task
Operating Systems 35
Threads: Lightweight
Processes
execution
Environment (resource)
Operating Systems 37
Threads have their own stacks
Operating Systems 38
Thread Model: Context Switch
Context Switching between threads within same
process inexpensive
Don’t need to read in/out PB parts on code, data, file,
signals, etc.
Thread context switch still requires
Register set switch, PC change, stack change, state
But no memory management related work !
Efficient (partly at least)!
Operating Systems 40
Use pthread library
to create and kill
threads within a process
A example program
#include ``csapp.h''
void *thread(void *vargp);
int main() {
phtread_t tid; // stores the new thread ID
3 pthread_join(tid, NULL); //main thread waits for the other thread to terminate
exit(0); /* main thread exits */
}
Operating Systems 44
Tradeoffs
Three ways to construct a server
Operating Systems 45
Blocking Vs. non-blocking System
Calls
Blocking system call
Usually I/O related: read(), fread(), getc(), write()
Doesn’t return until the call completes
The process/thread is switched to blocked state
When the I/O completes, the process/thread becomes ready
Simple
Real life example: attending a lecture
Using non-blocking system call for I/O
Asynchronous I/O
Complicated
The call returns once the I/O is initiated, and the caller can
immediately continue
Once the I/O completes, an interrupt is delivered to the OS,
which informs the calling process/thread
Real life example: applying for job
Operating Systems 46
Benefits of Threads
Responsiveness
Multi-threading allows applications to run even if
part of it is blocked
Resource sharing
Sharing of memory, files and other resources of
the process to which the threads belong
Economy
Much more costly and time consuming to create
and manage processes than threads
Utilization of multiprocessor architectures
Each thread can run in parallel on a different
processor
Operating Systems 47
Implementing Threads in User Space
User-level Threads
Operating Systems 48
User-level Threads
Advantages
Fast Context Switching:
User level threads are implemented using user level thread
libraries, rather than system calls, hence no call to OS and no
interrupts to kernel
When a thread is finished running for the moment, it can call
thread_yield. This instruction (a) saves the thread information in
the thread table itself, and (b) calls the thread scheduler to pick
another thread to run.
The procedure that saves the local thread state and the scheduler
are local procedures, hence no trap to kernel, no context switch,
no memory switch, and this makes the thread scheduling very fast.
Customized Scheduling possible by user
Operating Systems 49
User level Threads
Disadvantages
Blocking
A user level thread executing a blocking system call
can block the entire process
No Protection
There is no protection between threads, since the
threads share memory space
Operating Systems 50
Implementing Threads in the Kernel
OS knows about
individual threads within
each process
•Thread table maintained
by kernel
Kernel-level Threads
Operating Systems 51
Hybrid Implementations (e.g.,
Solaris)
Operating Systems 52
Pop-Up Threads (e.g, servers)
Operating Systems 53
Multi-threading Models
Many-to-One Model – many user threads are mapped to one kernel
thread
Advantage:
Thread management is done in user space, so it is efficient
Disadvantage:
Entire process will block if a thread makes a blocking call to the kernel
Because only one thread can access kernel at a time, no parallelism on
multiprocessors is possible
One-to-One Model – each user thread maps to a separate kernel thread
Advantage:
More concurrency than in many-to-one model
Multiple threads can run in parallel on multi-processors
Disadvantage:
Creating a user thread requires creating the corresponding kernel thread. There
is an overhead related with creating kernel thread which can be burden on the
performance.
Operating Systems 54
Multi-threading Models
Many-to-Many Model – many user
threads are multiplexed onto a smaller or
equal set of kernel threads.
Advantage:
Application can create as many user threads as
needed
Kernel threads run in parallel on multiprocessors
When a thread blocks, another thread can still run
Operating Systems 55
Making Single-Threaded Code Multithreaded:
Pitfalls
•But clumsy: need libraries to create a separate global space for each thread
Operating Systems 57
Summary
A process may comprise multiple threads
Threads in a process share a lot, so easier to
context switch
User level versus kernel level threads
Former is more efficient to manage, but
problematic if one thread blocks
Reading: Section 2.1 - 2.2
Next: Section 2.3
Operating Systems 58