Processes and Threads
1. PROCESSES
1.1. The Process Model 1.2. Process Creation 1.3. Process Termination 1.4 Process Hierarchies 1.5. Process States
1.7 Modeling Multiprogramming 2. THREADS
2.1. Thread Usage
2.2 The Classical Thread Model 2.4. Implement threads in user space 2.5 Implement threads in kernel space
3. SCHEDULING
1.1. The Process Model
• • •
Multiprogramming of four programs
Conceptual model of 4 independent, sequential Only one program active at any instant
1.1. The Process Model (cont)
• Foreground processes
– Associated with the user
Background processes •
– –
Daemons
Not associated with any user
1.2. Process Creation
Principal events that cause process creation
1. System initialization
– Boot time
2. Execution of a process creation system
– network date fetch – buffer - execute
3. 4.
User request to create a new process Initiation of a batch job
1.2. Process Creation (cont)
• Unix
– Fork()
• Creates copy of parent
• Then execve is called to change its content exectute
Windows
– Win32 function call CreateProcess() • Does creation and loading of program
to
1.3. Process Termination
Conditions which terminate processes
1. 2. 3. 4.
Normal exit (voluntary) Error exit (voluntary) Fatal error (involuntary)
1.4 Process Hierarchies
• Parent creates a child process, child can create its own process
Forms a hierarchy
– UNIX calls this a "process group"
processes •
• Windows has no
– all processes are
– Though in some • It can be passed
concept of process
created equal
cases handle() is used
to other processes
1.5. Process States
• Possible process states
– –
Running (actually using the CPU at that instant). Blocked (runnable; temporarily stopped to let another process run)
Ready (unable to run until some external event happens)
–
1.5. Process States (cont)
• Lowest layer of process-structured OS
– handles interrupts, scheduling
Above that layer are different sequential
Implementation of Processes (1)
Implementation of Processes (2)
Skeleton of what lowest level of OS does when interrupt occurs
2. THREADS
• is a light-weight process
• is the smallest sequence of instructions
programmed
The Thread Model
• •
Differences b/w process and thread
• processes are typically independent, while threads exist
of a process
as subsets
• processes carry considerably more state information than threads, whereas multiple threads within a process share process state as well as memory and other resources
• processes have separate
their address space
Differences b/w process and thread
• processes interact only through system-provided inter-process communication mechanisms
• context switching between threads in the same process is typically
between processes.
Threads
The Thread Model (1)
The Thread Model (2)
Thread Usage (1)
Thread Usage (2)
Thread Usage (3)
• Rough outline of code for previous
(a) Dispatcher thread (b) Worker thread
Thread Usage (4)
Implementing Threads in User Space
• Own private thread table
– – –
TCB ( thread control block)
Analogous to PCB or process table It is managed by Run-time system
• State is saved in this TCB
Implementing Threads in User Space
• When thread does something which cause it to blocked
– Calls run-time, and run-time sees if it needs to be blocked
It stores thread registers in thread table (TCB) Finds next thread to run in ready queue
– – – –
Reload register’s with new thread’s New thread starts running
saved values
• Key features
Implementing Threads in User Space
Implementing Threads in the Kernel
Single Thread vs. Multithread
• Single Threaded Server –
– – –
Main loop of web-server gets a request Examines it
Complete If waiting
• Server
it before next one for disk
is idle
• Does not process incoming requests
As result, only few requests/sec can be processed
–
• Multithread
– Each thread has its own work
Finite-State Machine Server
Non-Blocking system call •
– When a request come in one & only thread
• Can be found from cache
• If not, non-blocking disk operation is started
Server records the state of current request Fetches next event
• May be a new request
• Reply from disk about previous request
Process next request
• New request started
examines it
– –
–
• If reply from disk, relevant recorded state is fetched table and reply is processed
from the
Finite State Machine
A design in which each computation has a saved state and there exists some set of
•
events that can occur to change the state called a finite-state machine.
Uses non-blocking system call
is
•
• Best to use when be processed.
large amount of data is to
– – –
Reads a block Process it
Write it back
Scheduling
• Problem
– One or more processes are in ready state
– Decide which one to proceed for processing
Scheduling •
– That part of Operating System which decides which process runs next
The algorithm it uses called Scheduling Algorithm
Scheduling
Introduction to Scheduling (1)
• Bursts of CPU usage alternate with periods of I/O
– a CPU-bound process
– an I/O bound process
When to Schedule
• When a process is
– – – –
Created Exits
Scheduling
Non-Preemptive •
– Algorithm picks a process and lets it run until it blocks ( either on I/O or waiting for
process )
Or it voluntarily releases the CPU It cant be forcibly suspended
another
– –
• Preemptive
– Algorithm picks a process and let it maximum of some fixed time.
run for a
Introduction to Scheduling (2)
First Come First Serve
• The process that requests the CPU first, gets the CPU first
• New job queue
is added to the end of the job
• Non-preemptive
Shortest Job First
• Non-Preemptive
Shortest Job First
An example of shortest job first scheduling
• Average turnaround time
– (a)
– (b)
Shortest Remaining Time Next
Pre-emptive Scheduling
•
• Chooses process whose remaining is short
run time
• Every time new process comes in, current process and new process run-time are
Scheduling in Interactive Systems
• • • • • • • Round-Robin Scheduling Priority Scheduling Multiple QueuesShortest Process Next Guaranteed Scheduling Lottery Scheduling
Round-Robin
Scheduling
• Used in interactive systems
• Quantum
Round-Robin Scheduling
• Round Robin Scheduling
– –
(a) (b)
list of list of
runnable processes
Round-Robin Scheduling
• If is
a process blocks or finished before quantum elapsed then process switching is done.
• It is easy to implement.
Round-Robin Scheduling
• Setting the quantum
– too short causes too many process switches lowers the CPU efficiency
and
– too long may cause poor response to short interactive requests.
• Quantum around 20-50 msec is recommended
Priority Queue
Some processes are more important than others. •
• •
This leads to assigning priority to each process. E.g. University hierarchy
Priority Queue
Each process is assigned apriority, and the
•
run-nable process with the highest priority allowed to run.
is
• A process with high priority may run indefinitely without intervention.
• To avoid this:
– 1) Decrease priority of process on each clock
– 2) Fix a max quantum on higher priority task
cycle
Priority Queue
• Can be assigned
– Statically
– Dynamically
• Statically
– Fix priority or time on each task by judging them statically ( whatever information is
provided)
E.g. background processes 20msec and foreground processes is given 40msec
Priority Queue
• Dynamically
– Better algorithm is to use formula 1/f where ‘f’ is fraction of the last quantum used by the process.
Examples: •
– Process that used only 1 msec of its
50
50 msec quantum would get priority
– Process that ran
2,
25 msec before blocking would get priority
– Process that used the whole quantum would get
Priority Queue
• Dynamically
– Better algorithm is to use formula 1/f where ‘f’ is fraction of the last quantum used by the process.
Examples: •
– Process that used only 1 msec of its
50
50 msec quantum would get priority
– Process that ran
2,
25 msec before blocking would get priority
– Process that used the whole quantum would get
Scheduling in Interactive Systems
Priority Queue
Multiple Queues
Every class has a priority and quantum And each class has number of processes
Once a process is run in a certain class, change its class to lower one so that it could give
change to other processes to run. Example: • • • • – –
A process with 100msec time needed to execute
Multiple Queues
Shortest Process Next
Similar to SJF •
• Shortest Job First response
(SJF) has minimum average
• Needs to runnable
figure out which of the currently processes is the shortest one
Guaranteed Scheduling
Make a promise of CPU time and fulfill each process.
• it to
• If there are N users logged including you then you will receive about 1/N of the CPU power.
Guaranteed Scheduling
• Tracking of CPU time utilized by each process is required.
Lottery Scheduling
Give processes lottery tickets for various system resources specially CPU time. Lets say 20 msec lottery prize.
•
• When a scheduling decision is to made, one lottery ticket is chosen at random and the process holding the ticket gets the resource.
• So some processes can be given more tickets to increase their chance of winning.
Lottery Scheduling
• Simpler to implement compared to Guaranteed scheduling
• Highly responsive as if one process blocks it can transfer it lottery tickets to other