0% found this document useful (0 votes)
13 views53 pages

Unit 2

The document provides an overview of process management in operating systems, defining a process as a program in execution and detailing its structure, including stack, heap, text, and data sections. It discusses process attributes, operations, states, and the role of the Process Control Block (PCB) in managing process information. Additionally, it covers context switching, threads, and their advantages, along with different threading models such as user-level and kernel-level threads.

Uploaded by

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

Unit 2

The document provides an overview of process management in operating systems, defining a process as a program in execution and detailing its structure, including stack, heap, text, and data sections. It discusses process attributes, operations, states, and the role of the Process Control Block (PCB) in managing process information. Additionally, it covers context switching, threads, and their advantages, along with different threading models such as user-level and kernel-level threads.

Uploaded by

niggesh831
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PROCESS MANAGEMENT

PROCESS-DEFINITION
● A process is a program in execution.
● A process is defined as an entity which represents the basic unit of work to be implemented in
the system. To put it in simple terms, we write our computer programs in a text file and when we
execute this program, it becomes a process which performs all the tasks mentioned in the
program.
● When a program is loaded into the memory and it becomes a process, it can be divided into four
sections ─ stack, heap, text and data. The following image shows a simplified layout of a
process inside main memory −
Stack

The process Stack contains the temporary data such as method/function parameters, return address and
local variables.

Heap

This is dynamically allocated memory to a process during its run time.

Text

This includes the current activity represented by the value of Program Counter and the contents of the
processor's registers.

Data

1. Program contains a set of instructions designed to complete a specific task.
2. Process is an instance of an executing program. ...
3. Program is a passive entity as it resides in the secondary memory.
4. Process is a active entity as it is created during execution and loaded into the
main memory.
ATTRIBUTES OF A PROCESS

1. Process id
2. Program counter
3. Process state
4. Priority
5. General purpose register
6. List of open files
7. List of open device
8. protection
OPERATION OF PROCESS/STATE
The execution of a process is a complex activity. It involves various operations. Following are the operations
that are performed while execution of a process:
[Link]:
Once the process is created, it will be ready and come into the ready queue (main memory) and will be
ready for the execution.
[Link]
Out of the many processes present in the ready queue, the Operating system chooses one process and
start executing it. Selecting the process which is to be executed next, is known as scheduling.
[Link]
Once the process is scheduled for the execution, the processor starts executing it. Process may come to
the blocked or wait state during the execution then in that case the processor starts executing the other
processes.
[Link]/Killing
Once the purpose of the process gets over then the OS will kill the process. The Context of the process
(PCB) will be deleted and the process gets terminated by the Operating system.
Types of Process:

There is three type of process:

1. CPU - Bound: Process needs more service by CPU than I/O service.
2. I/O - Bound: Process needs more I/O service than CPU service.
3. Interactive: Process spends more time in waiting queue than CPU burst time.
STATES OF PROCESS
1. NEW
2. READY
3. RUNNING
4. BLOCK or WAIT
5. TERMINATION
6. SUSPEND READY
7. SUSPEND WAIT OR SUSPEND BLOCK
There are four types of process model:

(i) Two state process model


(ii) Five state process model:
(iii) Six state process model:
(iv) Seven state process model:
Where

R = Running,

W = Waiting,

T = Terminated,

SB = Suspended block, and

SR = Suspended ready state.

Preemption, dispatch, schedule , suspend, created, active, terminated etc. are actions that performed by operating system.
Process Transitions

● Transition from new state to ready state occurs when a process is created.
● Transition from ready state to running state occurs when CPU decides(using process
scheduling algorithm) which process will run next.
● Transition from running state to ready state occurs when process preempted.
● Transition from running state to waiting state occurs when process required I/O event.
● Transition from waiting state to ready state occurs when I/O event has completed.
● Transition from running state to terminated state occurs if process has finished its
execution.
[Link]: A program which is going to be picked up by the OS into the main memory is called a new process

[Link]: Whenever a process is created, it directly enters in the ready state, in which, it waits for the CPU to
be assigned. The OS picks the new processes from the secondary memory and put all of them in the main
memory.

The processes which are ready for the execution and reside in the main memory are called ready state processes.
There can be many processes present in the ready state.

[Link]:One of the processes from the ready state will be chosen by the OS depending upon the scheduling
algorithm. Hence, if we have only one CPU in our system, the number of running processes for a particular time
will always be one. If we have n processors in the system then we can have n processes running simultaneously.
[Link] OR WAIT:From the Running state, a process can make the transition to the block or wait
state depending upon the scheduling algorithm or the intrinsic behavior of the process.

When a process waits for a certain resource to be assigned or for the input from the user then the OS
move this process to the block or wait state and assigns the CPU to the other processes.
[Link] OR TERMINATION:
When a process finishes its execution, it comes in the termination state. All the context of the process
(Process Control Block) will also be deleted the process will be terminated by the Operating system.
[Link] READY

A process in the ready state, which is moved to secondary memory from the main memory due to lack
of the resources (mainly primary memory) is called in the suspend ready state.

If the main memory is full and a higher priority process comes for the execution then the OS have to
make the room for the process in the main memory by throwing the lower priority process out into the
secondary memory. The suspend ready processes remain in the secondary memory until the main
memory gets available.

[Link] WAIT

Instead of removing the process from the ready queue, it's better to remove the blocked process which
is waiting for some resources in the main memory. Since it is already waiting for some resource to get
available hence it is better if it waits in the secondary memory and make room for the higher priority
process. These processes complete their execution once the main memory gets available and their wait
is finished.
Process Control Block
● A process control block (PCB) is a data structure used by computer operating systems to
store all the information about a process.

● It is also known as a process descriptor.

● When a process is created (initialized or installed), the operating system creates a


corresponding process control block.
PCB
● While creating a process the operating system performs several operations.
● To identify the processes, it assigns a process identification number (PID) to each process.
● As the operating system supports multi-programming, it needs to keep track of all the
processes. For this task, the process control block (PCB) is used to track the process’s
execution status.
● Each block of memory contains information about the process state, program counter,
stack pointer, status of opened files, scheduling algorithms, etc.
● All these information is required and must be saved when the process is switched from one
state to another. When the process makes a transition from one state to another, the
operating system must update information in the process’s PCB.
.
STRUCTURE OF PROCESS CONTROL BLOCK
PCB
Process State
This specifies the process state i.e. new, ready, running, waiting or terminated.

Process Number
This shows the number of the particular process.

Program Counter
This contains the address of the next instruction that needs to be executed in the process.

Registers
This specifies the registers that are used by the process. They may include accumulators, index registers, stack
pointers, general purpose registers etc.
List of Open Files

These are different files that are associated with the process

CPU Scheduling Information

The process priority, pointers to scheduling queues etc. is the CPU scheduling information that is contained in the
PCB. This may also include any other scheduling parameters.

Memory Management Information

The memory management information includes the page tables or the segment tables depending on the memory
system used. It also contains the value of the base registers, limit registers etc.
I/O Status Information

This information includes the list of I/O devices used by the process, the list of files etc.

Accounting information

The time limits, account numbers, amount of CPU used, process numbers etc. are all a part of the PCB accounting information.

Location of the Process Control Block

The process control block is kept in a memory area that is protected from the normal user access. This is done because it contains
important process information. Some of the operating systems place the PCB at the beginning of the kernel stack for the process as it is
a safe location.
● A process is defined as the execution of a program that performs the actions specified in that program.

● Process management involves various tasks like creation, scheduling, termination of processes, and a dead lock.

● The important elements of Process architecture are 1)Stack 2) Heap 3) Data, and 4) Text

● The PCB is a full form of Process Control Block. It is a data structure that is maintained by the Operating
System for every process

● A process state is a condition of the process at a specific instant of time.

● Every process is represented in the operating system by a process control block, which is also called a task
control block.
Context Switching
● Context Switching involves storing the context or state of a process so that it can be reloaded
when required and execution can be resumed from the same point as earlier. This is a feature of
a multitasking operating system and allows a single CPU to be shared by multiple processes.
● In the above diagram, initially Process 1 is
running.
● Process 1 is switched out and Process 2 is
switched in because of an interrupt or a
system call.
● Context switching involves saving the state
of Process 1 into PCB1 and loading the state
of process 2 from PCB2.
● After some time again a context switch
occurs and Process 2 is switched out and
Process 1 is switched in again.
● This involves saving the state of Process 2
into PCB2 and loading the state of process 1
from PCB1.
Context Switching Triggers
There are three major triggers for context switching. These are given as follows −

● Multitasking:

In a multitasking environment, a process is switched out of the CPU so another process can be run.

The state of the old process is saved and the state of the new process is loaded. On a pre-emptive
system, processes may be switched out by the scheduler.
● Interrupt Handling:

The hardware switches a part of the context when an interrupt occurs. This happens automatically.
Only some of the context is changed to minimize the time required to handle the interrupt.

● User and Kernel Mode Switching:

A context switch may take place when a transition between the user mode and kernel mode is
required in the operating system.
Context Switching Steps
The steps involved in context switching are as follows −
● Save the context of the process that is currently running on the CPU. Update the process control
block and other important fields.
● Move the process control block of the above process into the relevant queue such as the ready
queue, I/O queue etc.
● Select a new process for execution.
● Update the process control block of the selected process. This includes updating the process
state to running.
● Update the memory management data structures as required.
● Restore the context of the process that was previously running when it is loaded again on the
processor. This is done by loading the previous values of the process control block and registers.
Thread in Operating System
● What is a Thread?
A thread is a path of execution within a process.
● A process can contain multiple threads.
● A thread is a flow of execution through the process code, with its own program counter that
keeps track of which instruction to execute next, system registers which hold its current working
variables, and a stack which contains the execution history.
● A thread shares with its peer threads few information like code segment, data segment and open
files.
● When one thread alters a code segment memory item, all other threads see that.
Thread
● A thread is also called a lightweight process. Threads provide a way to improve application
performance through parallelism.
● Threads represent a software approach to improving performance of operating system by
reducing the overhead thread is equivalent to a classical process.
● Each thread belongs to exactly one process and no thread can exist outside a process.
● Each thread represents a separate flow of control.
● Threads have been successfully used in implementing network servers and web server.
● They also provide a suitable foundation for parallel execution of applications on shared memory
multiprocessors. The following figure shows the working of a single-threaded and a
multithreaded process.
Difference between Process and Thread
S.N. Process Thread

1 Process is heavy weight or resource intensive. Thread is light weight, taking lesser resources than a
process.
2 Process switching needs interaction with Thread switching does not need to interact with
operating system. operating system.
3 In multiple processing environments, each All threads can share same set of open files, child
process executes the same code but has its own processes.
memory and file resources.
4 If one process is blocked, then no other process While one thread is blocked and waiting, a second
can execute until the first process is unblocked. thread in the same task can run.
5 Multiple processes without using threads use Multiple threaded processes use fewer resources.
more resources.
6 In multiple processes each process operates One thread can read, write or change another thread's
independently of the others. data.
Advantages of Thread
● Threads minimize the context switching time.
● Use of threads provides concurrency within a process.
● Efficient communication.
● It is more economical to create and context switch threads.
● Threads allow utilization of multiprocessor architectures to a greater scale and
efficiency.
Types of Thread
● Threads are implemented in following two ways −
● User Level Threads − User managed threads.
● Kernel Level Threads − Operating System managed threads acting on
kernel, an operating system core.
User Level Threads
● In this case, the thread management
kernel is not aware of the existence of
threads.
● The thread library contains code for
creating and destroying threads, for
passing message and data between
threads, for scheduling thread execution
and for saving and restoring thread
contexts.
● The application starts with a single thread.
Advantages & Disadvantages
● Advantages
● Thread switching does not require Kernel mode privileges.
● User level thread can run on any operating system.
● Scheduling can be application specific in the user level thread.
● User level threads are fast to create and manage.
● Disadvantages
● In a typical operating system, most system calls are blocking.
● Multithreaded application cannot take advantage of multiprocessing.
Kernel Level Threads
● In this case, thread management is done by the Kernel.
● There is no thread management code in the application area. Kernel threads
are supported directly by the operating system.
● Any application can be programmed to be multithreaded.
● All of the threads within an application are supported within a single process.
● The Kernel maintains context information for the process as a whole and for
individuals threads within the process.
● Scheduling by the Kernel is done on a thread basis. The Kernel performs
thread creation, scheduling and management in Kernel space.
● Kernel threads are generally slower to create and manage than the user
threads.
Advantages & Disadvantages
● Advantages
● Kernel can simultaneously schedule multiple threads from the same process
on multiple processes.
● If one thread in a process is blocked, the Kernel can schedule another thread
of the same process.
● Kernel routines themselves can be multithreaded.
● Disadvantages
● Kernel threads are generally slower to create and manage than the user
threads.
● Transfer of control from one thread to another within the same process
requires a mode switch to the Kernel.
Multithreading Models
● Some operating system provide a combined user level thread and Kernel
level thread facility. Solaris is a good example of this combined approach. In a
combined system, multiple threads within the same application can run in
parallel on multiple processors and a blocking system call need not block the
entire process. Multithreading models are three types
● Many to many relationship.
● Many to one relationship.
● One to one relationship.
Many to Many Model
● The many-to-many model multiplexes any
number of user threads onto an equal or
smaller number of kernel threads.
● The following diagram shows the
many-to-many threading model where 6 user
level threads are multiplexing with 6 kernel
level threads. In this model, developers can
create as many user threads as necessary
and the corresponding Kernel threads can run
in parallel on a multiprocessor machine. This
model provides the best accuracy on
concurrency and when a thread performs a
blocking system call, the kernel can schedule
another thread for execution.
Many to One Model
● Many-to-one model maps many user level
threads to one Kernel-level thread. Thread
management is done in user space by the
thread library. When thread makes a
blocking system call, the entire process
will be blocked. Only one thread can
access the Kernel at a time, so multiple
threads are unable to run in parallel on
multiprocessors.
● If the user-level thread libraries are
implemented in the operating system in
such a way that the system does not
support them, then the Kernel threads use
the many-to-one relationship modes.
One to One Model
● There is one-to-one relationship of
user-level thread to the kernel-level
thread. This model provides more
concurrency than the many-to-one model.
It also allows another thread to run when a
thread makes a blocking system call. It
supports multiple threads to execute in
parallel on microprocessors.
● Disadvantage of this model is that creating
user thread requires the corresponding
Kernel thread. OS/2, windows NT and
windows 2000 use one to one relationship
model.
Difference between User-Level & Kernel-Level Thread
S. User-Level Threads Kernel-Level Thread
N.

1 User-level threads are faster to create and manage. Kernel-level threads are slower to create and
manage.

2 Implementation is by a thread library at the user level. Operating system supports creation of Kernel threads.

3 User-level thread is generic and can run on any Kernel-level thread is specific to the operating system.
operating system.

4 Multi-threaded applications cannot take advantage of Kernel routines themselves can be multithreaded.
multiprocessing.
Why Multithreading?
● A thread is also known as lightweight process. The idea is to achieve
parallelism by dividing a process into multiple threads. For example, in a
browser, multiple tabs can be different threads. MS Word uses multiple
threads: one thread to format the text, another thread to process inputs, etc.
More advantages of multithreading are discussed below
Multithreading
● There are four major categories of benefits to multi-threading:
○ Responsiveness - One thread may provide rapid response while other threads are blocked or
slowed down doing intensive calculations.
○ Resource sharing - By default threads share common code, data, and other resources, which
allows multiple tasks to be performed simultaneously in a single address space.
Multithreading
○ Economy - Creating and managing threads ( and context switches between them ) is much
faster than performing the same tasks for processes.
○ Scalability, i.e. Utilization of multiprocessor architectures - A single threaded process can only
run on one CPU, no matter how many may be available, whereas the execution of a
multi-threaded application may be split amongst available processors. ( Note that single
threaded processes can still benefit from multi-processor architectures when there are multiple
processes contending for the CPU, i.e. when the load average is above some certain
threshold. )
Multicore Programming
● A recent trend in computer architecture is to produce chips with
multiple cores, or CPUs on a single chip.
● A multi-threaded application running on a traditional single-core chip would
have to interleave the threads, as shown in Figure 4.3. On a multi-core chip,
however, the threads could be spread across the available cores, allowing
true parallel processing, as shown in Figure 4.4.
Multicore Programming

You might also like