Module 2 Operating System
Module 2 Operating System
Module 2
PROCESS
MANAGEMENT
1. Process concept:
A process can be thought of as a program in execution. A process will
need certain resources-such as CPU time, memory, files, and I/O devices to
accomplish its task. These resources are allocated to the process either when it
is created or while it is executing.
The operating system is responsible for the following activities in
connection with process and thread management: the creation and deletion of
both user and system processes; the scheduling of processes; and the provision
of mechanisms for synchronization, communication, and deadlock handling for
processes. The terms job and process are used almost interchangeably.
1.1 Process:
A process is more than the program code, which is sometimes known as
the text section. It also includes the current activity, as represented by the
value of the program counter and the contents of the processor's registers. A
process generally also includes the process stack, which contains temporary
data (such as function parameters, return addresses, and local variables), and
CIT,MANDYA Page 1
Operating System Concepts BCS303
a data section, which contains global variables. A process may also include a
heap, which is memory that is dynamically allocated during process run
time.
Process in memory
CIT,MANDYA Page 2
Operating System Concepts BCS303
CIT,MANDYA Page 3
Operating System Concepts BCS303
CIT,MANDYA Page 4
Operating System Concepts BCS303
The PCB simply serves as the repository for any information that may
vary from process to process.
1.4 Threads:
The process model discussed so far has implied that a process is a program
that performs a single thread of execution. For example, when a process is
running a word-processor program, a single thread of instructions is being executed.
This single thread of control allows the process to perform only one
task at one time.
CIT,MANDYA Page 5
Operating System Concepts BCS303
2. Process scheduling :
Process Scheduler selects an available process for program execution on
the CPU. In a multiprocessor system - one process will be under execution and
the rest of the processes have to wait until the CPU is free and can be
rescheduled. The main objective of process scheduling is to keep the CPU busy at
all times.
• All processes admitted to the system are stored in the job queue.
• Processes in main memory and ready to execute are placed in the ready
queue.
• Processes waiting for a device to become available are placed in device
queues. There is generally a separate device queue for each device.
CIT,MANDYA Page 6
Operating System Concepts BCS303
A new process is initially put in the ready queue. It waits in the ready
queue until it is selected for execution and is given the CPU. Once the process
is allocated the CPU and is executing, one of several events could occur:
• The process could issue an I/O request, and then be placed in an I/O
queue.
• The process could create a new subprocess and wait for its termination.
CIT,MANDYA Page 7
Operating System Concepts BCS303
In the first two cases, the process eventually switches from the waiting
state to the ready state, and is then put back in the ready queue. A process
continues this cycle until it terminates, at which time it is removed from all
queues.
2.2 Schedulers:
A process migrates among the various scheduling queues throughout its
lifetime. The operating system must select, for scheduling purposes,
processes from these queues in some fashion. The selection process is carried
out by the appropriate scheduler.
Schedulers are software which selects an available program to be assigned
to CPU.
A long-term scheduler or Job scheduler – selects jobs from the
job pool (of secondary memory, disk) and loads them into the memory.
If more processes are submitted, than that can be executed
immediately, such
processes will be in secondary memory. It runs infrequently, and can take time
to select the next process.
CIT,MANDYA Page 8
Operating System Concepts BCS303
CIT,MANDYA Page 9
Operating System Concepts BCS303
Interrupts cause the operating system to change a CPU from its current task
and to run a kernel routine. Such operations happen frequently on general-
purpose systems. When an interrupt occurs, the system needs to save the current
of the process running on the CPU so that it can restore that context when its
processing is done, essentially suspending the process and then resuming it. The
context is represented in the PCB of the process; it includes the value of the CPU
registers, the process state.
CIT,MANDYA Page 10
Operating System Concepts BCS303
Context switch time is an overhead, as the system does not do useful work while
switching.
3. Operations on processing:
The processes in most systems can execute concurrently, and they may be
created and deleted dynamically. Thus, these systems must provide a mechanism
for process creation and termination.
CIT,MANDYA Page 11
Operating System Concepts BCS303
On typical Solaris systems, the process at the top of the tree is the ‘sched’
process with PID of 0. The ‘sched’ process creates several children processes –
init, pageout and fsflush. Pageout and fsflush are responsible for managing
memory and file systems. The init process with a PID of 1, serves as a parent
process for all user processes.
A process will need certain resources (CPU time, memory, files, I/O
devices) to accomplish its task.
When a process creates a new process, two possibilities exist in terms of
execution:
The parent continues to execute concurrently with its children.
The parent waits until some or all of its children have terminated.
There are also two possibilities in terms of the address space of the new
process:
The child process is a duplicate of the parent process (it has the
same program and data as the parent).
The child process has a new program loaded into it.
To illustrate these differences, let's first consider the UNIX operating system.
In UNIX, as we've seen, each process is identified by its process identifier,
#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
int main ()
{
pid_t pid;
CIT,MANDYA Page 12
Operating System Concepts BCS303
In UNIX OS, a child process can be created by fork() system call. The fork
system call, if successful, returns the PID of the child process to its parents
and returns a zero to the child process. If failure, it returns -1 to the parent.
Process IDs of current process or its direct parent can be accessed using the
getpid( ) and getppid( ) system calls respectively.
The parent waits for the child process to complete with the wait() system
call. When the child process completes, the parent process resumes and
completes its execution.
CIT,MANDYA Page 13
Operating System Concepts BCS303
Process Creation
In windows the child process is created using the function createprocess( ).
The createprocess( ) returns 1, if the child is created and returns 0, if the child
is not created.
CIT,MANDYA Page 14
Operating System Concepts BCS303
4. Interprocess communication:
Processes executing may be either co-operative or independent processes.
Independent Processes – processes that cannot affect other processes
or be affected by other processes executing in the system.
Cooperating Processes – processes that can affect other processes
or be affected by other processes executing in the system.
CIT,MANDYA Page 15
Operating System Concepts BCS303
CIT,MANDYA Page 16
Operating System Concepts BCS303
There are two types of buffers into which information can be put –
• Unbounded buffer
• Bounded buffer
With Unbounded buffer, there is no limit on the size of the buffer, and so
on the data produced by producer. But the consumer may have to wait for new
items.
CIT,MANDYA Page 17
Operating System Concepts BCS303
4.2.1 Naming:
The processes that want to communicate should have a way to refer each
other. (Using some identity).
Direct Communication – Direct communication the
sender and receiver must explicitly know each other’s name. The
syntax for send() and receive() functions are as follows-
send (P, message) – send a message to process P
receive(Q, message) – receive a message from process
Q
CIT,MANDYA Page 18
Operating System Concepts BCS303
CIT,MANDYA Page 19
Operating System Concepts BCS303
4.2.2 Synchronization:
The send and receive messages can be implemented as either
blocking or non-blocking.
• Blocking (synchronous) send - sending process is blocked
(waits) until the message is received by receiving process or the mailbox.
• Non-blocking (asynchronous) send - sends the
message and continues (does not wait)
• Blocking (synchronous) receive - The receiving process is
blocked until a message is available
• Non-blocking (asynchronous) receive - receives the
message without block. The received message may be a valid
message or null.
4.2.3 Buffering:
When messages are passed, a temporary queue is created. Such
queue can be of three capacities:
• Zero capacity – The buffer size is zero (buffer does not exist).
Messages are not stored in the queue. The senders must block until receivers
accept the messages.
• Bounded capacity- The queue is of fixed size(n). Senders must
block
if the queue is full. After sending ‘n’ bytes the sender is blocked.
• Unbounded capacity - The queue is of infinite capacity. The
sender never blocks.
CIT,MANDYA Page 20
Operating System Concepts BCS303
5. Multithreading
models:
Introduction:
A thread is a basic unit of CPU utilization; it comprises a thread ID, a
program counter, a register set, and a stack. It shares with other threads
belonging to the same process its code section, data section, and other
operating- system resources, such as open files and signals. A traditional (or
heavyweight) process has a single thread of control. If a process has multiple
threads of control, it can perform more than one task at a time.
CIT,MANDYA Page 21
Operating System Concepts BCS303
Benefits:
The benefits of multithreaded programming can be broken down into four
major categories:
1. Responsiveness: Multithreading an interactive application may allow a
program to continue rU1U1ing even if part of it is blocked or is performing a
lengthy operation, thereby increasing responsiveness to the user. For instance, a
multithreaded web browser could still allow user interaction in one thread while
an image was being loaded in another thread.
2. Resource sharing: By default, threads share the memory and the
resources of the process to which they belong. The benefit of sharing code and
data is that it allows an application to have several different threads of activity
within the same
address space.
3. Economy: Allocating memory and resources for process creation is
costly. Because threads share resources of the process to which they belong, it is
more economical to create and context-switch threads. Empirically
gauging the
difference in overhead can be difficult, but in general it is much more time
consuming to create and manage processes than threads. In Solaris, for example,
creating a process is about thirty times slower than is creating a thread, and
context switching is about five times slower.
4. Utilization of multiprocessor architectures: The benefits of
multithreading
can be greatly increased in a multiprocessor architecture, where threads may be
running in parallel on different processors. A single-threaded process can only
run on one CPU, no matter how many are available. Multithreading on a multi-
CPU machine increases concurrency.
CIT,MANDYA Page 22
Operating System Concepts BCS303
Multithreading Models:
1. Many-to-One Model – The many-to-one model maps many user-level
threads to one kernel thread. Thread management is done by the thread
library in user space, so it is efficient; but the entire process will block if a
thread makes a blocking system call. Also, because only one thread can
access the kernel at a time, multiple threads are unable to run in parallel
on multiprocessors. Green threads-a thread library available for Solaris-
uses this model, as does GNU Portable Threads.
Many-to-one model
CIT,MANDYA Page 23
Operating System Concepts BCS303
One-to-one model
Many-to-many model
CIT,MANDYA Page 24
Operating System Concepts BCS303
THREAD LIBRARIES
It provides the programmer with an API for the creation and management of threads. Two
ways of implementation:
1) First Approach Provides a library entirely in user space with no kernel support. All
code and data structures for the library exist in the user space.
2) Second Approach Implements a kernel-level library supported directly by the OS. Code
and data structures for the library exist in kernel space.
Pthreads:
• This is a POSIX standard API for thread creation and synchronization.
• This is a specification for thread-behavior, not an implementation.
• OS designers may implement the specification in any way they wish.
• Commonly used in: UNIX and Solaris.
Java Threads
• Threads are the basic model of program-execution in Java program and Java language.
• The API provides a rich set of features for the creation and management of threads.
• All Java programs comprise at least a single thread of control.
CIT,MANDYA Page 25
Operating System Concepts BCS303
}
THREADING ISSUES
***Discuss any 3 threading issues that come with multi-threaded programs
1. System call fork() and exec() in multi-thread programming
2. Thread cancellation
3. Signal handling
4. Thread pools
[Link] call fork() is used to create a separate, duplicate process. If one thread in a program
calls fork(), then some systems duplicates all threads and other systems duplicate only the
thread that invoked the fork(). If a thread invokes the exec(), the program specified in the
parameter to exec() will replace the entire process including all threads.
[Link] Cancellation: This is the task of terminating a thread before it has completed.
Target thread is the thread that is to be canceled. Thread cancellation occurs in two different
cases: 3) Asynchronous cancellation: One thread immediately terminates the target thread. 4)
Deferred cancellation: The target thread periodically checks whether it should be terminated.
3)Signal Handling: In UNIX, a signal is used to notify a process that a particular event has
occurred.
All signals follow this pattern:
1) Synchronous signals Delivered to the same process that performed the operation
causing the signal. E.g. illegal memory access and division by 0.
CIT,MANDYA Page 26
Operating System Concepts BCS303
1) A Default Signal Handler Run by the kernel when handling the signal.
2) A User-defined Signal Handler Overrides the default signal handler.
4)Thread Pools: The basic idea is to create a no. of threads at process-startup and place the
threads into a pool (where they sit and wait for work). Procedure:
1. When a server receives a request, it awakens a thread from the pool.
2. If any thread is available, the request is passed to it for service. Once the service is
completed, the thread returns to the pool.
Advantages:
1) Servicing a request with an existing thread is usually faster than waiting to create a thread.
2) The pool limits the no. of threads that exist at any one point. No. of threads in the pool can
be based on factors such as: no. of CPUs, amount of memory and expected no. of concurrent
client-requests.
CIT,MANDYA Page 27
Operating System Concepts BCS303
PROCESS SCHEDULING
Basic Concepts: In a single-processor system, only one process may run at a time and other
processes must wait until the CPU is rescheduled. The main objective of multiprogramming is
to have some process running at all times, in order to maximize CPU utilization.
CPU-I/O Burst Cycle: Process execution consists of a cycle of CPU execution and an I/O wait
as shown in below figure. Process execution begins with a CPU burst, followed by an I/O burst,
then another CPU burst, etc… Finally, a CPU burst ends with a request to terminate execution.
An I/O- bound program typically has many short CPU bursts. A CPU-bound program might
have a few long CPU bursts.
CPU SCHEDULER:
CPU scheduler selects a waiting-process from the ready-queue and allocates CPU to the
waiting- process. The ready-queue could be a FIFO, priority queue, tree and list. The records in
the queues are generally process control blocks (PCBs) of the processes.
CPU SCHEDULING Four situations under which CPU scheduling decisions take place:
1. When a process switches from the running state to the waiting state. For ex; I/O request.
2. When a process switches from the running state to the ready state. For ex: when an
interrupt occurs.
3. When a process switches from the waiting state to the ready state. For ex: completion of I/O.
4. When a process terminates.
Dispatcher :
It gives control of the CPU to the process selected by the short-term scheduler. The function
involves:
1) Switching context
2) Switching to user mode &
3) Jumping to the proper location in the user program to restart that program.
It should be as fast as possible, since it is invoked during every process switch.
Dispatch latency :means the time taken by the dispatcher to stop one process and to start
another process to run.
[Link] Utilization: We must keep the CPU as busy as possible. In a real system, it ranges from
40% to 90%.
2. Throughput: The number of processes completed per time unit. For long processes, throughput may
be 1 process per hour; For short transactions, throughput might be 10 processes per second.
3. Turnaround Time: The interval from the time of submission of a process to the time of
completion. Turnaround time is the sum of the periods spent in waiting to get into memory,
CIT,MANDYA Page 29
Operating System Concepts BCS303
waiting in the ready-queue, executing on the CPU and doing I/O.
4. Waiting Time: The amount of time that a process spends waiting in the ready-queue.
5. Response Time: The time from the submission of a request until the first response is
produced.
SCHEDULING ALGORITHMS :CPU scheduling deals with the problem of deciding which
of the processes in the ready queue is to be allocated the CPU.
Following are some scheduling algorithms:
1) FCFS scheduling (First Come First Served)
2) Round Robin scheduling
3) SJF scheduling (Shortest Job First)
4)Priority scheduling
FCFS SCHEDULING The process that requests the CPU first is allocated the CPU first. That
means process which arrives the ready-queue first, get scheduled first if the CPU is free. This is
a non-preemptive scheduling concept. The implementation is easily done using a FIFO queue.
Procedure:
1) When a process enters the ready-queue, its PCB is linked onto the tail of the queue.
2) When the CPU is free, the CPU is allocated to the process at the queue‘s head.
3) The running process is then removed from the queue.
Advantage: Code is simple to write & understand.
Disadvantages:
1) Convoy effect: All other processes wait for one big process to get off the CPU.
2) Non-preemptive (a process keeps the CPU until it releases it).
3) Not good for time-sharing systems.
4) The average waiting time is generally not minimal.
CIT,MANDYA Page 30
Operating System Concepts BCS303
SJF SCHEDULING:
SJF is a CPU scheduling algorithm that selects the process with the shortest CPU burst time next.
ADVANTAGE:
SJF gives the best possible average waiting time among all scheduling algorithms.
2. High throughput
More processes are completed in less time because short jobs are executed first.
DISADVANTAGE:
The exact length of each job must be known in advance, which is usually not possible.
PRIORITY: Priority Scheduling is a CPU scheduling algorithm in which each process is assigned
a priority value, and the CPU is allocated to the process with the highest priority (or lowest
numerical value, depending on the system).
If two processes have the same priority, another scheduling method like FCFS is used to break the
tie.
ADVANTAGES:
DISADVANTAGE:
Low-priority jobs may never get CPU if high-priority jobs keep arriving.
2. Priority inversion
ROUND ROBIN: Round Robin (RR) is a CPU scheduling algorithm in which each process is
assigned a fixed time quantum (time slice). The CPU executes each process for at most one time
quantum in a circular order.
If a process does not finish within its time quantum, it is preempted and placed at the end of the
ready queue, and the next process is scheduled.
This ensures fairness, as every process gets an equal share of CPU time.
Every process gets an equal share of CPU time due to the fixed time quantum.
CIT,MANDYA Page 32
Operating System Concepts BCS303
2. Good for time-sharing systems
DISADVANTAGES:
Threads are managed in user space by the thread library, not by the OS.
The kernel is unaware of these threads.
Scheduling decisions are made by the user-level thread library (e.g., POSIX threads).
Key Points:
CIT,MANDYA Page 33
Operating System Concepts BCS303
The kernel schedules each thread independently.
Key Points:
Additional Classification:
EG:I f thread T1 is running and T2 arrives with higher priority , os stops T1 and runs T2.
EG: IF thread T1 is running and T2 arrives with high priority, still T1 continue to execute.
Multiprocessor thread scheduling refers to how the operating system assigns threads to run on
multiple CPUs or processor cores.
It decides which thread runs on which processor and ensures load balancing, efficiency, and
minimal overhead.
TYPES:
Only one processor (master) does all scheduling and system tasks.
CIT,MANDYA Page 34
Operating System Concepts BCS303
ADVANTAGES: Simple design
CIT,MANDYA Page 35
Operating System Concepts BCS303
CIT,MANDYA Page 36
Operating System Concepts BCS303
CIT,MANDYA Page 37
Operating System Concepts BCS303
CIT,MANDYA Page 38