Module 2
Module 2
CONCEPTS
Module 2
PROCESS MANAGEMENT
CONCEPT OF A PROCESS
An operating system executes a variety of programs in Batch system, Time-
shared systems etc
Process – a program in execution; process execution must progress in
sequential fashion
A process includes:
Process no/ Id
program counter – address of the next instruction
data
PROCESS MANAGEMENT
Process is a program in execution
Difference b/w
Process Program
A process is an active entity. Source code is stored in
Translated object code is secondary memory
stored in main memory. Program is a passive entity.
A process is a program in A program is a set of
execution. instructions
The process is dynamic. The program is static.
Process State
As a process executes, it changes state
New: The process is being created
PID
Address of
Next Instruction
CONTEXT SWITCHING
Switching the CPU to another process requires performing a state save of the
Current Process and a state restore of a different process.
SCHEDULERS
Long-term scheduler:
(job scheduler) – selects
which processes should be
brought into the ready
queue
0 24 27 30
P2 P3 P1
0 3 6 30
P4 P1 P3 P2
0 3 9 16 24
Average waiting time = (3 + 16 + 9 + 0) / 4 = 7
Average Turn around Time = (3+9+16+24)/4 = 13
SHORTEST JOB FIRST WITH ARRIVAL TIME
Now we add the concepts of varying arrival times and preemption to the
analysis
Process Arrival Time Burst Time
Waiting time = Completion time – Burst Time –
P1 0 8 Arrival Time
P2 1 4 Turn around time = Completion time – Arrival Time
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
0 1 6 16 18 19
0 4 7 10 14 18 22 26 30
Asymmetric multiprocessing – only one processor accesses the system data structures, for
data sharing
b) Shared
memory
IPC
Message Passing:
The function of message system is to allow processes to
communicate with one another without the need to share data. It
is used as a method of communication in microkernels.
If processes P and Q wants to communicate they need a
communication link to send and receive messages.
IPC
Shared Memory:
Shared Memory is a memory shared between two processes. Each
process that wants to communicate with another process can do so only
using Inter process communication techniques.
It is the fastest IPC mechanism.
The OS maps an address space to read and write without calling
operating system functions.
Steps:
1. Request a memory segment that can be shared between processes
to the operating system.
2. Associate it with the address space of the calling process.
THREADS
ØA thread is a basic unit of CPU utilization.
ØIt consists of a thread ID, program counter, a stack, and a set of registers.
ØTraditional processes have a single thread of control. It is also called as heavyweight
process. There is one program counter, and one sequence of instructions that can be
carried out at any given time.
ØA multi-threaded application have multiple threads within a single process, each
having their own program counter, stack and set of registers, but sharing common code,
data, and certain structures such as open files. Such process are called as lightweight
process.
ADVANTAGES
n Responsiveness – may allow continued execution if part of process is
blocked, especially important for user interfaces
n Resource Sharing – threads share resources of process, easier than shared
memory or message passing
n Economy – cheaper than process creation, thread switching lower
overhead than context switching
n Scalability – process can take advantage of multiprocessor architectures
TYPES OF THREADS
1) User Threads are above the kernel and are managed without kernel support.
2) Kernel Threads-managed by the operating system itself
There are 3 types of relationships b/w user threads and kernel threads.
USER LEVEL THREADS
Each thread is represented by a PC, registers, stack and a small control block, all stored in the user
process address space.
Thread management done by user-level threads library.
Three primary thread libraries: POSIX Pthreads ,Win32 threads ,Java threads
User-level threads implement in user-level libraries, rather than via systems calls, so
thread switching does not need to call operating system and to cause interrupt to the kernel.
Fast and Efficient: Thread switching is not much more expensive than a procedure call.
Simple Management: Creating a thread, switching between threads and synchronization between
threads can all be done without intervention of the kernel.
KERNEL LEVEL THREADS
Instead of thread table in each process, the kernel has a thread table that keeps
track of all threads in the system.
The kernel-level threads are slow and inefficient. For instance, threads operations are
hundreds of times slower than that of user-level threads.
Kernel must manage and schedule threads as well as processes. It requires a full
thread control block (TCB) for each thread to maintain information about threads.
As a result there is significant overhead and increased in kernel complexity.
MULTITHREADED PROCESSES
MANY-TO-ONE MODEL
Many user-level threads are mapped to single
kernel thread.
Multiple threads may not run in parallel on muticore
system because only one may be in kernel at a time.
Thread management is done by thread library in
user space.
If one of the thread makes a blocking system call
then the entire process will be blocked.
Few systems currently use this model
Examples:
Solaris Green Threads
GNU Portable Threads
ONE-TO-ONE MODEL
Each user-level thread maps to one kernel
thread
Creating a user-level thread creates a kernel
thread
Allows multiple threads to run in parallel on
multiprocessors.
Number of threads per process sometimes
restricted due to overhead
Examples
Windows
Linux
Solaris 9 and later
MANY-TO-MANY MODEL
Allows many user level threads to be
mapped to many kernel threads
Allows the operating system to create a
sufficient number of kernel threads
Example:
Solaris prior to version 9
Windows with the ThreadFiber package
MULTICORE PROGRAMMING