Professional Documents
Culture Documents
Unit 2 - Process MGMT
Unit 2 - Process MGMT
UNIT 2
PROCESS MANAGEMENT: 8 HOURS
SYLLABUS CONTENT
2.1 Process concept:
Process Scheduling
Operation on process
Inter process communication
2.2 Multithreaded Programming:
Multithreading models
Thread libraries
Threading issues
2.3 Process Scheduling:
Basic concepts
Scheduling algorithms
Scheduling Criteria
Thread scheduling
DR. NEEPA SHAH 2
PROCESS CONCEPT
PROCESS IN MEMORY
PROCESS STATE
A data structure maintained by the OS for every process for storing information associated with each process
(also called task control block)
Identified by an integer process ID (PID).
Keeps all the information needed to keep track of a process with the help of process attributes (also known
as Context of the process).
Maintained for a process throughout its lifetime, and is deleted once the process terminates.
10
11
CONTEXT SWITCH
When CPU switches to another process, the system must save the state of the old process and load
the saved state for the new process via a context switch
Context of a process represented in the PCB
Context-switch time is pure overhead; the system does no useful work while switching
Time dependent on hardware support
Some hardware provides multiple sets of registers per CPU ➔ multiple contexts loaded at once
12
13
PROCESS SCHEDULING
14
15
16
17
SCHEDULERS
The CPU scheduler selects from among the processes in ready queue, and allocates a CPU core to one of them
Queue may be ordered in various ways
CPU scheduling decisions may take place when a process:
1. Switches from running to waiting state (e.g., I/O request)
2. Switches from running to ready state (e.g., interrupt)
3. Switches from waiting to ready (completion of I/O)
4. Terminates
For situations 1 and 4, there is no choice in terms of scheduling. A new process (if one exists in the ready queue) must
be selected for execution.
For situations 2 and 3, however, there is a choice.
18
When scheduling takes place only under circumstances 1 and 4, the scheduling scheme is
nonpreemptive.
Otherwise, it is preemptive.
Non-preemptive: Once the CPU has been allocated to a process, the process keeps the CPU until it
releases it either by terminating or by switching to the waiting state.
Preemptive: OS can force (preempt) a process from CPU at anytime to allocate CPU to another higher-priority
process
Virtually all modern operating systems including Windows, MacOS, Linux, and UNIX use preemptive
scheduling algorithms.
19
SCHEDULERS CONTD.
Short-term scheduler (or CPU scheduler) – selects which process should be executed next and allocates
CPU
Sometimes the only scheduler in a system
Short-term scheduler is invoked frequently (100 milliseconds) (must be fast)
Long-term scheduler (or job scheduler) – selects which processes should be brought into the ready queue
Long-term scheduler is invoked infrequently (seconds, minutes) (may be slow)
The long-term scheduler controls the degree of multiprogramming
20
SCHEDULERS CONTD.
21
Medium-term scheduler
Removes partially executed processes from
memory (reduces the degree of
multiprogramming)
Some time later, these processes can be
reintroduced into memory and execution
can be continued from where it left off. This
scheme is called swapping. Process is
swapped-in/ out by medium term scheduler
Swapping may help to improve process mix
or if memory has to be freed up
22
23
DISPATCHER
24
25
OPERATIONS ON PROCESSES
26
PROCESS CREATION
Parent process create children processes, which, in turn create other processes, forming a tree of processes
Resource sharing options
Parent and children share all resources
Children share subset of parent’s resources
Parent and child share no resources
Execution options
Parent and children execute concurrently
Parent waits until children terminate
Address space
Child duplicate of parent
Child has a program loaded into it
27
PROCESS TERMINATION
Process executes last statement and then asks the operating system to delete it using the exit() system call.
Returns status data from child to parent (via wait())
Process’ resources are deallocated by operating system
Parent may terminate the execution of children processes using the abort()/TerminateProcess()
system call. Some reasons for doing so:
Child has exceeded allocated resources
Task assigned to child is no longer required
The parent is exiting, and the operating systems does not allow a child to continue if its parent terminates (Cascading
termination)
28
INTERPROCESS COMMUNICATION
29
COMMUNICATIONS MODELS
(a) Message passing (b) shared memory
30
31
MULTITHREADING MODELS
THREAD LIBRARIES
THREADING ISSUES
32
MOTIVATION
33
MOTIVATION CONTD.
Web browser
1 thread displays images/ text, another thread retrieves data from n/w
Word processor
Thread for displaying graphics, responding to keystrokes, performing spelling and grammar check in background
Web server
Several clients access it
If server ran a traditional single threaded process, it would serve only 1 client at a time (increases wait time of client)
Solution used before threads: every time server receives a request, it creates a separate process to service that request
which is very time consuming and resource intensive
Hence create 1 process with multiple threads: When a request is made, thread will service the request and resume listening
for additional requests.
34
35
36
BENEFITS
Responsiveness
Allows a program to continue even if a part is blocked
Multithreaded browser allows user interaction in 1 thread while another loads an image
Resource Sharing
threads share memory and resources of process to which they belong easier than shared memory or message passing
Economy
In general, it is much more time consuming to create and manage processes than threads
More economical to create and context switch threads
Scalability
Multithreading on a multi-CPU machine increases parallelism
37
Parallelism implies a system can perform more than one task simultaneously
38
TYPES
User-level thread
Supported above kernel and implemented by thread library at user level
No support from kernel as kernel is unaware
Creation, scheduling, management is done in user space
User level threads are fast to create and manage
Kernel-Level Thread
Supported directly by kernel (OS)
Kernel performs Creation, scheduling, management in kernel space
Slower to create and manage
39
TYPES CONTD.
40
MULTITHREADING MODELS
41
MANY-TO-ONE
Many user-level threads mapped to single kernel thread
One thread blocking causes all to block
Multiple threads may not run in parallel on multicore system because only one may be in kernel at a time
Thread management is done by the thread library in user space
Few systems currently use this model
Examples:
Solaris Green Threads
GNU Portable Threads
42
ONE-TO-ONE
43
MANY-TO-MANY MODEL
Allows many user level threads to be mapped to smaller or equal number of kernel threads
Allows the operating system to create a sufficient number of kernel threads
Windows with the ThreadFiber package
44
TWO-LEVEL MODEL
45
THREAD LIBRARIES
Provides the programmer with API for creating and managing threads.
2 ways of implementing thread library
1. Provide a library in user space with no kernel support.
1. All code and data structures for library exists in user space.
2. Invoking a function in the library results in a local function call in user space and not a system call.
46
Win32
Kernel level library on windows
Java
Allows threads to be created and managed in JAVA programs
Since JVM runs on top of host OS, java thread API is implemented using thread library available on host system
47
THREADING ISSUES
48
49
SCHEDULING ALGORITHMS
1. First-Come, First-Served (FCFS) Scheduling (NP)
2. Shortest-Job-First (SJF) Scheduling / shortest next CPU burst (NP/P)
3. Priority Scheduling (P/NP)
4. Round Robin (RR) scheduling (P)
5. Multilevel Queue Scheduling P1 P2 P3 P1 P1
6. Multilevel Feedback Queue Scheduling 0
4 7 10 14 …….30
Turnaround time = Completion time – arrival time
Waiting time = turnaround time – burst time
50
P1 P2 P3
0 24 27 30
51
P2 P3 P1
0 3 6 30
52
Associate with each process the length of its next CPU burst
Use these lengths to schedule the process with the shortest time
SJF is optimal – gives minimum average waiting time for a given set of processes
Preemptive version called shortest-remaining-time-first
How do we determine the length of the next CPU burst?
Could ask the user
Estimate
53
EXAMPLE OF SJF
P4 P1 P3 P2
SJF scheduling chart
0 3 9 16 24
54
EXAMPLE OF SHORTEST-REMAINING-TIME-FIRST
Now we add the concepts of varying arrival times and preemption to the analysis
ProcessA arri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
55
PRIORITY SCHEDULING
The CPU is allocated to the process with the highest priority (smallest integer highest priority)
Preemptive
Nonpreemptive
SJF is priority scheduling where priority is the inverse of predicted next CPU burst time
Solution Aging – as time progresses increase the priority of the process (Priority Range: 127 (low) to 0 (high) )
56
57
Each process gets a small unit of CPU time (time quantum q), usually 10-100 milliseconds. After this time has
elapsed, the process is preempted and added to the end of the ready queue.
If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU
time in chunks of at most q time units at once. No process waits more than (n-1)q time units.
Timer interrupts every quantum to schedule next process
Performance
q large FIFO
q small q must be large with respect to context switch, otherwise overhead is too high
58
59
60
62
MULTILEVEL QUEUE
63
64
65
MQS has the advantage of low scheduling overhead, but it is inflexible (processes do not move from 1
queue to other, since they don’t change their nature)
In MFQS, a process can move between the various queues.
Multilevel-feedback-queue scheduler is defined by the following parameters:
Number of queues
Scheduling algorithms for each queue
Method used to determine when to upgrade a process
Method used to determine when to demote a process
Method used to determine which queue a process will enter when that process needs service
66
If processes uses too much CPU time, it will be moved to lower priority queue
I/O bound and interactive processes are in higher priority queues.
Aging can be implemented using multilevel feedback queue
Processes that waits for long in low priority queue may be moved to higher priority queue. This
form of aging prevents starvation
67
68
Three queues:
Q0 – RR with time quantum 8 milliseconds
Q1 – RR time quantum 16 milliseconds
Q2 – FCFS
Scheduling
A new process enters queue Q0 which is served in RR
When it gains CPU, the process receives 8 milliseconds
If it does not finish in 8 milliseconds, the process is moved to queue Q1
69
EXERCISE
70
71
72
73