0% found this document useful (0 votes)
18 views55 pages

Chap 2 Process Mangement

Uploaded by

nameistushar12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views55 pages

Chap 2 Process Mangement

Uploaded by

nameistushar12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Chapter 2:

Processes and Threads


Chapter 2: Processes and Threads

 Process Concept – The processes,


Process states, Process control block.
 Process Scheduling – Scheduling
queues, Schedulers, context switch
 Operations on Process – Process
creation with program using fork(),
Process termination
 Thread Scheduling- Threads, benefits,
Multithreading Models, Thread
Libraries.
Introduction

 A process is a smallest unit of work that is


scheduled by O.S
 A Process needs resources such as CPU time,
memory, files and I/O devices, to accomplish its
task. These resources are allocated either when the
program is created, or when it is executing.
 In multiprogramming environment many processes
are present in memory
 To select the process for execution some
management is required . Hence process
management is done by O.S
 It Includes switching the CPU between the
processes, select process for running and so on.
Process Concept

 An operating system executes a variety of programs:


 Batch system – jobs
 Time-shared systems – user programs or tasks
 Textbook uses the terms job and process almost
interchangeably
 Process is a program in execution; process
execution must progress in sequential fashion
 A process includes following section:
 program counter – includes current activity
 Stack –Which contains temporary data (e.g function
parameters, return addresses and local variables
 data section – contains global variables, static
variable
 Text – program code
Process in Memory

Function stack –
Contains temporary
data ,
function parameter,
- Heap grow upside Local variable,
- stack grow downside Return variable

Dynamic Memory
allocated variable
Global variable /
static variable

Code Segment-
Instruction of
program
Process Concept

 Program by itself is not a process. A program is a


passive entity such as a file containing a list of
instructions stored on a disk also called as
executable file.
 Whereas a process is an active entity with a
program counter specifying the next instruction to
execute and a set of associated resources.
 A program written in any language in execution
state is called process
 A program becomes a process when an
executable file is loaded into memory
Process Concept

Two common technique for loading executable file are


1) Double-clicking an icon representing the executable
file
2) Entering the name of the executable file on the
command line.
Although two processes may be associated with the
same program, they are nevertheless considered two
separate executed sequence
e.g - Several user may be running different copies of
the mail program, or the same user may invoke many
copies of the web browser program. Each of these is a
separate process and although the text sections are
equivalent the data, heap, and stack sections vary.
Process State

 As a process executes, it changes state.


 The state of a process is defined in part by the
current activity of that process.
 Each process in an OS under goes change in state
during its lifetime.
 The change in a state of a process is known as state
transition of a process.
 A state diagram represents the different states in
which a process can be at different times, along with
the transitions from one state to another that are
possible in the Os
Process State

Each process may be in one of the following


state
 new: The process is being created

 running: Instructions are being executed

 waiting: The process is waiting for some

event to occur
Such as I/O completion or reception of a
signal.
 ready: The process is waiting to be assigned

to a processor
 terminated: The process has finished

execution
Terminat
e
exit
Secondary
Memory
New Main
Memory Ready
P1 P2 P3 running

P4 P5 P6
P1 P4 P7 CPU
Ready Queue
P7 P8 P9

I/O I/O Queue


Devic
e Waiting
Diagram of Process State
Process Control Block (PCB)
 In order to execute instructions the OS has to
create processes. Each process is represented in
the OS by the PCB also called as task control block
 The OS groups all information that needs about a
particular process into a data structure called a
PCB or Process Descriptor.
 When a process is created, OS creates a
corresponding PCB and released whenever the
process terminates.
 PCB stores descriptive information pertaining to a
process,
 The Basic Purpose of PCB is to indicate the so far
progress of a process.
 In simple words, the OS maintains the information
Process Control Block (PCB)
Process Control Block (PCB)
It is a DS to keep information of Process
Information associated with each process. Each process has its own PCB

 Process state
The state may be new, ready, waiting, halted and so on. This
information is kept in a codified fashion in the PCB. That means it assigns
some numbers to each state and it stores that number so it requires very less
space.

 Process number
At the time of process creation O.S allocates a number to the
process called process ID / number

 Program counter
It indicates the address of the next instruction to be executed for
this process.

 CPU registers
It vary in no of type depending on the computer architecture. They
include accumulators, index registers, stack pointers, any condition code
information. Along with the program counter the state information must be
saved when an interrupt occurs, to allow the process to be continued
Process Control Block (PCB)
 CPU scheduling information
It includes a process priority, pointer to scheduling queues and any
other scheduling parameters.

 Memory-management information
It includes information as the value of the base and limit registers,
the page tables or the segment tables, depending on the memory system used
by the O.S

 Accounting information
It includes the amount real of CPU and time used time limits,
account numbers, job or process numbers and so on.

 I/O status information


It includes list of I/O devices allocated to the process, a list of open
files and so on.
 File Management
 Pointer
 Priority
Process Scheduling
 The objective of multiprogramming is to keep CPU busy
for maximum time and achieve maximum CPU
Utilization
 In uni-processor system only one process is there and
any time it may get the attention of the CPU
 But in multiprogramming as only one process may get
attention of the CPU at a time other processes have to
wait.
 So the process scheduling is necessary and it is
done by O.S
Scheduling Queues
 As processes enter the system they are put into a job queue,
which consists of all processes in the system
 The processes that are residing in Main Memory(MM) and are
ready and waiting to execute are kept in a list called the ready
queue.
 This queue is stored as a linked list
 A ready queue header contains pointers to the first and final
PCB’s in the list
 Each PCB includes a pointer field that points to the next PCB in
the ready queue.
Header PCB 2 PCB 9
PCB 7
Tail

 A ready queue need not behave (FIFO) always as a queue in


the data structures. A ready queue may be maintained as a
priority queue, stack, tree or linked list
Ready Queue And Various I/O Device Queues
Scheduling Queues
 In the system , the processes waiting for CPU are kept in ready
queue and the processes waiting for a device are kept in the
device queue.
 A process may have an I/O request and the device requested may
be busy
 In such case the I/O request is maintained in the device queue.
 Each device has its own device queue.
 In case of dedicated devices the device queue will never have
more process in it
 In case of sharable devices several processes may be in the
ready queue.
 Different types of queues :
Job queue – set of all processes in the system
Ready queue – set of all processes residing in main memory,
ready and waiting to execute
Device queues – set of processes waiting for an I/O device

Processes migrate among the various queues


Representation of Process Scheduling
Queuing Diagram
Scheduling Queues
 CPU scheduling is represented by a queuing
diagram
 Each rectangular box represents a queue.
 Two types of queue are present – the ready queue
and device queue
 Circles represents the resources that serves the
queue and the arrow indicated the flow of
processes in the system
 A process that enters the system from outside
world is placed in ready queue. There it waits for
CPU
 After the CPU is utilized by the process it may
finish its execution and leave the system or it
Scheduling Queues

 Once the process is allocated the CPU and is


executing, one of several events could occur.

1. The process could issue an I/O request and then


be placed in an I/O queue

2. The process could create a new sub-process and


wait for the sub-processes termination(fork())

3. The process could be removed forcibly from the


CPU as a result of an interrupt and be put back
in the ready queue. (interrupt)
Functions of Process Scheduling

 Process Scheduling must perform the following


function-

1.Keep track of the status (running, ready or waiting)


of all the process. This is done by traffic controller
2.The process is selected from ready queue and decided
by the scheduler how long it executes
3.When running process requires any I/O resources or
an interrupt occurs or it exceeds its time quantum the
processor de-allocates the process
Schedulers
 A process migrates among the various scheduling
queues throughout its lifetime.
 The O.S must select processes from these queue in
some way.
 The selected process is carried out by the
appropriate scheduler
 There are 3 different types of scheduler.
1.Long-term scheduler (or job scheduler/Admission
Scheduler) – selects which processes should be
brought into the ready queue
2.Short-term scheduler (or CPU scheduler/Process
Scheduler) – selects which process should be
executed next and allocates CPU
3.Medium term scheduling (Swapper) which
Addition of Medium Term Scheduling

Medium term scheduler

Short term
scheduler

New Job
Long term
scheduler
Job Needs
I/O

Job
finishes
with I/O
Long term Schedulers
 It selects the batch job or process to be executed from a
secondary storage device and loads them into memory for
execution.
 It is invoked when the process leaves the system.
 Because of the longer duration between executions LTS can
afford to take more time to decide which process should be
selected for execution.
 It also provide good process mix of I/O bound and CPU bound
processes to the Short term scheduler
 The I/O bound process means the process require more I/O
than CPU
 The CPU bound process means the process require more
CPU then I/O
 System with the best performance will have a combination of
CPU bound and I/O bound processes.
Long term Schedulers
 Functions of long term scheduler
1. It communicates directly with the job pool
2. It selects number of jobs from the pool and loads
them in the memory for execution. (degree of
multiprogramming is no. of programs, that can
reside in the memory of the computer at a time)
3. It need to be invoked only when a job leaves the
system. Therefore it is invoked less frequently as
compared to short term scheduler
4. It has the responsibility of selecting proper mix of
I/O bound and CPU bound jobs so that CPU
utilization is good.
Short term Schedulers

 It is supposed to select a job from ready queue


and submit it to CPU
 As the STS selects only one job at a time , it is
invoked very frequently
 In case of I/O bound jobs as the ready queue is
almost empty, Short term scheduler has very less
work to do.
 The system like time sharing do not have long
term scheduler.
 The jobs are placed directly in the ready queue
for the STS
 The dispatcher is the actual component that
gives control of the CPU to the process
Medium term Schedulers

Following are the situations in which medium


term scheduler is required
1. Sometimes it is required to remove some
number of jobs from memory temporarily and
reduce degree of multiprogramming.
2. Sometimes while executing the program, it is
found that memory requirements of the
program are changed. Therefore the job is to be
removed from the memory temporarily.
 Swapping is the process associated with the
medium term scheduler by which processes and
temporarily removed and then brought back to
the ready queue.
Context Switch

 Interrupt cause the O.S to change a CPU from its current task to
run a kernel routine.
 The process of switching the CPU from an old process to an
new process is called context switch.
 When interrupt occurs, the system needs to save the current
context of the process currently running on the CPU so that it can
restore that context when its processing is done, essentially
suspending the process and then resuming it.
 When a context switch occurs, the kernel saves the context of the
old process in its PCB and loads the saved context of the new
process scheduled to run.
 It speed varies from M/C to M/C depending on the memory speed,
the no. of registers that must be copied and the existence of
special instruction.
 Typical speeds are a few milliseconds.
 Its times are highly dependent on H/W support.
Context Switch

Process P0

CPU Execution Interrupt or


System call
occurs

Save its state


into PCB’s Process P2

CPU Execution
CPU Switch From Process to Process
Operations on Process

 Process creation with program using


fork(), Process termination
In the system , the process are
created and deleted dynamically . There are two
operations provided by O.S on processes:
1) Process creation
2) Process termination
Process Creation
 A process may create several new process through create process
system call during the course of execution
 The creating process is called a parent process and the new
process are called the children of that process.
 Each of these new processes in turn create other processes,
forming a tree of processes
 Generally, process identified and managed via a process
identifier (pid)
 In Unix a listing of process can be obtained using ps command
 A process will need certain resources (CPU time, memory, files, I/O
devices) to accomplish its task. When a process creates a sub
process that sub process may be able to obtain its resources
directly from the O.S
 When a process creates new process two possibility exist in terms
of execution.
1. The parent continues to execute concurrently with its children
2. The parent waits until some or all of its children terminated
A tree of processes on a typical Solaris
Process Creation

 UNIX examples
 fork system call creates new process
 If the child process is created the return code of

fork call is zero and when the child is returned to


parent the return code is non-zero.
 fork usually returns the identification of the child

to the parent
 execlp system call used after a fork to replace

the process’ memory space with a new program


Process Creation
C Program Forking Separate Process
int main()
{
pid_t pid;
/* fork another process */
pid = fork();
if (pid < 0) { /* error occurred */
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0) { /* child process */
execlp("/bin/ls", "ls", NULL);
}
else { /* parent process */
/* parent will wait for the child to complete */
wait (NULL);
printf ("Child Complete");
exit(0);
}
}
Process Termination
 A process terminates when it finishes executing
its final statement and ask the O.S to delete it by
using the exit() system call.
 At that point , the process may return a status
values to its parent process ( through the wait
system call)
 All the resources of the process including
physical and virtual memory open files and I/O
buffers are deallocated by the O.S
 The another system call is ABORT usually this
call can be invoked by only the parent of the
process that is to be terminated
Process Termination
 There are many reasons for terminating the
execution of child process by parent process
such as:-
1. The child has crossed the limit of usage of the
resources it has been allocated
2. The child process no longer required.
3. If the parent terminates then O.S does not
allows a child to continue execution

 A process may terminates normally or


abnormally . When the process terminates then
all its child must be terminated. This is called
cascading termination
Thread Scheduling-
 Threads, benefits,
 Multithreading Models,
 Thread Libraries.
Threads

Smallest unit of processing that can be


performed in an O.S.
It always exits within a process that means single
process may contain multiple threads.
Thread is a basic unit of CPU utilization, which
consists of thread Id, Program Counter , Registers
Set and Stack
□ A full process includes numerous things:
■ an address space (defining all the
code and data pages)
■ OS resources and accounting
information
■ a “thread of control”,

□ defines where the process is

currently executing
□ PC, registers, and stack

□ Creating a new process is costly


■ all of the structures (e.g., page tables)
that must be allocated
□ Communicating between processes is costly
■ most communication goes through the
OS
Threads and Processes

□ Most operating systems therefore support two entities:


 the process,
□ which defines the address space and general
process attributes
 the thread,
□ which defines a sequential execution stream

within a process
□ A thread is bound to a single process.
 For each process, however, there may be many
threads.
□ Threads are the unit of scheduling
□ Processes are containers in which threads execute
Benefits of Multithreading
Programming

□ Responsiveness
□ Resource Sharing
□ Economy
□ Utilization Multiprocessor Architecture
Single and Multithreaded
Processes
Threads vs. Processes

1. A process has code/data/heap


1. A thread has no data segment
& other segments
or heap
2. A process has at least one
2. A thread cannot live on its own,
thread
it must live within a process
3. Threads within a process share
3. There can be more than one
code/data/heap, share I/O, but
thread in a process, the first
each has its own stack &
thread calls main & has the
registers
process’s stack
4. Inexpensive creation
4. Expensive creation
5. Inexpensive context switching
5. Expensive context switching
6. If a thread dies, its stack
6. If a process dies, its resources
is reclaimed
are reclaimed & all threads die
7. No System call
7. System Call involved in process
Types of thread

1) User level threads-Supported above the


kernel and are managed without kernel support

2) Kernel level threads-Supported and managed


directly by the operating system.

There must exists a relationship between user


threads and kernel threads
User-Level vs. Kernel
Threads

User-Level Kernel-Level
□ Managed by application □ Managed by kernel
□ Kernel not aware of thread □ Consumes kernel resources
□ Context switching cheap □ Context switching expensive
□ Can create many more □ Number limited by kernel
□ Must be used with care resources
□ Simpler to use

Key issue: kernel threads provide virtual processors to user-level threads,


but if all of kernel threads block, then all user-level threads will
block even if the program logic allows them to proceed

4.1
Multithreading
Models

Multithreading allows the application to divide its task


into individual threads.
In multi-threads, the same process or task can be done
by the number of threads, or we can say that there is
more than one thread to perform the task in
multithreading.
With the use of multithreading, multitasking can be
achieved.

Mapping user threads to kernel threads:


□ Many-to-One
□ One-to-One
□ Many-to-Many

4.1
Many-to-One
□ Many user-level threads mapped to single kernel
thread
□ Threads Management is done by the thread library
in user space so it is efficient
Advantage
■ Fast - no system calls required
■ Few system dependencies;
■ portable
■Efficient : thread management is
done on the user level
Disadvantage:
 The entire process will block if a thread makes a
blocking system call
 Because only one thread can access the kernel at a
time multiple threads are unable to run in parallel on
multiprocessors
□ Examples:
■ Solaris Green Threads
■ GNU Portable Threads

4.1
One-to-One

□ Each user-level thread maps to kernel thread


□ Advantage
■ Provides More concurrency than the many to one models : As each user thread is

connected to different kernel , if any user thread makes a blocking system call,
the other user threads won’t be blocked
■ Better multiprocessor performance – allows multiple threads to run

in parallel on multiprocessors
• disadvantage
■ Each user thread requires creation of kernel thread
■ Because the overhead of creating kernel threads can burden the
performance of an application (most implementation of the model
restrict the number of threads supported by the system
■ Each thread requires kernel resources;
■ limits number of total threads
□ Examples
■ Windows NT/XP/2000
4.1
Many-to-Many
Model
□ Allows many user level threads to be
mapped to many kernel threads
□ The number of kernel threads may be
specific to either a particular application
or particular machine
□ When a thread performs a blocking
system call, the kernel can schedule
another thread for execution.
■ Allows the operating system to create a
sufficient number of kernel threads
■ If U < L, no benefits of multithreading
■ If U > L, some threads may have to wait
for an LWP to run
□ Examples:
■ Solaris prior to version 9
■ Windows NT/2000 with the ThreadFiber
package
Two-level
Model

□ Similar to M:M, except


that a user thread
may be bound to
kernel thread
□ Examples
■ IRIX
■ HP-UX
■ Tru64 UNIX
■ Solaris 8 and
earlier

4.1

You might also like