0% found this document useful (0 votes)
51 views

Chapter 3 - Process Management

This document discusses processes and process management in an operating system. It defines a process as the active execution of a program and explains the different states a process can be in, such as new, running, ready, waiting, and terminated. It also describes the process control block (PCB) that the operating system uses to manage information about each process like its ID, state, priority, and memory allocation. Key aspects of process scheduling like scheduling queues and algorithms are also mentioned but discussed in further detail in Chapter 4.

Uploaded by

my pc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

Chapter 3 - Process Management

This document discusses processes and process management in an operating system. It defines a process as the active execution of a program and explains the different states a process can be in, such as new, running, ready, waiting, and terminated. It also describes the process control block (PCB) that the operating system uses to manage information about each process like its ID, state, priority, and memory allocation. Key aspects of process scheduling like scheduling queues and algorithms are also mentioned but discussed in further detail in Chapter 4.

Uploaded by

my pc
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter 3

Process Management
10 Hours
22 Marks

3.1 Concept of Process

A process can be thought of as unit of work. System consists of collection of


processes. Operating system processes execute system code whereas user
processes execute user code. A program becomes a process when an executable
file is loaded into memory. Two common ways of loading an executable file are –
by double clicking on the file, entering the executable file (with path) on the
command prompt.
A program is passive entity. It does nothing until its instructions are
executed by CPU. A program is just a file containing list of instructions stored on
a disk. Instance of program in execution is called as a process. Process is an
active entity with a program counter specifying which instruction to execute next
and a set of associated resources. A running compiler is a process. A word
processing program run by user is also a process. A system task like reading data
from a scanner is also a process.

Stack

Heap

Data

Text

Figure 3.1: Address Space associated with process / Process in Memory

With each process an address space (in main memory) is associated which
can be read or written by that process. The address space associated with process
(also referred as process in memory) is shown in figure 3.1. This address space is
normally addressed from 0 to some maximum value. This address space contains
a text section which contains the executable program along with value of
program counter and contents of processor’s registers. The address space also
includes a process stack. The stack contains temporary data such as function
parameters, return addresses and local variables. Address space also contains
3-1
data section which contains global variables. Address space may also include
heap which is the memory area that is dynamically allocated during process run-
time.
Several users may be running different mail programs at a time or single
user may invoke multiple copies of browser at the same time. All these are
different processes. Such processes may have similar text sections but the other
sections (stack, data and heap) vary.

3.2 Process States

As a process executes, it changes its state. State of a process is defined by


the current activity that the process is doing. A process can be in one of the
following states.
- New
- Running
- Waiting / Blocked
- Ready
- Terminated

New Terminated

Scheduler
Admit dispatch Exit

Ready Running

End of time slice /


Preemption
I/O or Event I/O or Event
Competion Request

Waiting/ Blocked

Figure 3.2: Process States / State Transition Diagram

Process may undergo through almost all the states enlisted above. State
transition diagram is shown in figure 3.2

3.2.1 New
This is a state of a process when a process is being created.
3.2.2 Running
The process is in running state when processor is allocated to the process
and the instructions of the process are being executed by processor. In a single-
processor system, there can be only one process in running state at a time. (But in
3-2
multi-processor system there may be multiple processes in running state at a
time). When CPU time slice allotted to the running process ends or the running
process is preempted by another process, the state of process changes to ready
state.
3.2.3 Waiting/ Blocked
This state is also called as suspended state sometimes. When a running
process requests for some I/O operation to be performed or for some event to be
occurred, the process changes its state to waiting or blocked. The process cannot
continue its execution until the requested I/O operation gets completed or the
event is occurred. i.e. the process gets blocked. When the I/O operation gets
completed or event (for which process was blocked) is occurred, the process goes
to ready state. There can be multiple waiting/ blocked processes in the system at
a time.
3.2.4 Ready
A process which is waiting for allotment of processor to it and a process
which is not waiting for any I/O operation to be completed or external event to be
occurred is said to be in ready state. A process which is newly created enters in
this state immediately on its admission. Operating system maintains a list of all
the processes which are in ready state. Whenever processor becomes free, one of
the processes from this list gets selected and is dispatched for the execution.
Basically, this function is done by scheduler depending on various scheduling
criteria and scheduling policy or algorithm used. When the process is dispatched
and its execution starts, it enters in running state. There can be multiple ready
processes at a time.
3.2.5 Terminated
When process has finished its execution, it is said to be in terminated state
(sometimes also called as halted state). In Unix this state is called as Zombie
state. This state is also called as Dormant state sometimes.

3.3 Process Control Block

Operating system maintains a data structure named process control


block (PCB) for each process. It is also called as task control block. When a
new process is created a PCB for that process gets created and the PCB gets
removed when the process is killed. All the PCBs are kept in the memory
reserved for operating system.
PCB contains many pieces of information associated with specific process
as shown in figure 3.3. They are discussed from 3.3.1 to 3.3.-----.

3.3.1 Process ID (PID)


When a process gets created, operating system assigns unique
identification number to it called as process ID or PID. This number is used for
carrying all the operations on the process. Generally, operating system sets some
maximum limit for number of processes that can be handled. If the limit is set to
n, the process id varies from 0 to n–1.
3.3.2 Process State
This field stores information about current state of the process as whether
the process is in new, ready, running, blocked or terminated state.

3-3
Process ID
Process State
Program Counter
Process Priority

Register Save Area

Memory Management
Information
Accounting information
List of open files
Pointer to other resources
Other information
Pointer to other PCB

Figure 3.3: Process Control Block

3.3.3 Program Counter


This field indicates address of next instruction to be executed for the
process.
3.3.4 Process Priority
Higher priority processes are required to be completed urgently than other
low priority processes. The priority can be set either externally by user/ system
manager or internally depending on various parameters.
3.3.5 Register Save Area
This is the area where current values of all the CPU registers are saved at
the time of context switch.
3.3.6 Memory Management Information
This information may contain values of base register (pointer to process
memory), limit register, page table and segment table depending on the memory
system used by operating system.
3.3.7 Accounting information
It gives the account of process’s usage of resources such as CPU time, disk
I/O used etc. This information is useful especially in data centers / cost centers
where users are charged for their system usage.
3.3.8 List of open files
It contains list of files those were opened by the process. This information
is useful to operating system for closing the open files which are not closed by
process on its termination.
3.3.9 Pointer to other resources
It maintains a pointer to other resources, data structures required by the
process.
3.3.10 Other information
It contains information like pathname of current directory or home
directory.

3-4
3.3.11 Pointer to other PCB
This gives pointer to next PCB within a specific category. E.g. operating
system maintains list of ready processes. In this case this field holds address of
next PCB whose process state is ready. Similarly, operating system maintains
hierarchy of processes so that parent process can traverse to PCBs of all its child
processes.
Generally, each PCB has two such pointer fields for maintaining forward
chain and backward chain. In both cases, ‘*’ indicates end of chain.

3.4 Process Scheduling

Discussed in Chapter 4
3.4.1 Scheduling Queues

Discussed in Chapter 4
3.4.2 Scheduler

Discussed in Chapter 4
3.4.3 Context Switch

There are many situations where CPU has to switch from one process to
another. Some of the situations are
- Expiration of time slice allotted to a running process
- I/O request in running process
- Occurrence of interrupt
- Running process has to wait for some event (like completion of execution of
child process)
Process P0 Process P1
Executing

State Save in PCB of P0


Idle

State Restore from PCB of P1

Executing
Idle

State Save in PCB of P1

Idle
State Restore from PCB of P0
Executing

Figure 3.4: Context Switch


3-5
In any of these situations, running process has to release CPU for other
process. Switching of CPU from one process to another is called as context
switch. In a context switch, two main steps are required to be followed. They are
- State Save of running process.
- State Restore of the process to which CPU is to be allotted.
While switching to other process, context of process currently running on
CPU is needed to be saved so that the process can resume in near future from the
same location where it was interrupted. The context of a process is represented in
its Process Control Block (PCB) which includes data like values of CPU registers,
memory management information etc.
When a context switch occurs, kernel saves context (state save) of currently
running process in its PCB and restores context (state restore) of process
scheduled to run from its PCB as shown in figure 3.4.
Context switch time is pure overhead. System does not perform any useful
work during context switch. Speed of context switch varies from machine to
machine, depending on memory speed, number of CPU registers to be copied etc.
Some machines provide special instruction to store or load all the registers at a
time so that context switch can be performed with greater speed. Some processors
provide multiple sets of registers. In such case, context switch may require to just
change pointer to the current register set.

3.5 Operations on Process

Various operations are required to be performed on a process. They are,


- Process Creation
- Process Termination / Killing a process
- Process Dispatch
- Time-up of process
- Blocking a process
- Wake-up a process
- Change priority of process
etc
Operating system must provide system calls for performing these
operations on processes. Most of these operations result in changes in process
states. Hence, Linking and de-linking of PCBs from various queues is required. In
following sections we will discuss mechanisms involved in process creation and
process termination.

3.5.1 Process Creation


When user enters a command on command prompt (which works as
command interpreter), the command interpreter process creates a new process for
execution of the entered command.
As seen in the above example, a process may create several new processes
using create-process system call. The creating process is called as a parent
process and the newly created process is called as child process. Each child
process may in turn create other processes forming a tree of processes. A sample
process tree is shown in figure 3.5.
Most of the operating system identifies process using a unique process
identifier (PID). It is typically an integer number.
Four important events that cause processes to be created are
3-6
- System initialization
- Execution of a process creation system call by a running process
- A user request to create new process
- Initiation of a batch job

Figure 3.5: Sample Process Tree

A newly created process may require certain resources (as CPU time,
memory, files, I/O devices etc) for its execution. The child process may obtain its
required resources directly from operating system or the child process may be
restricted to use subset of resources available with its parent. Second choice is
better as it prevents any process from overloading the system by creating too
many child processes. In this choice, parent process may partition its resources
among its multiple child processes.
When a child process is created, there are two possibilities regarding its
address space. They are
1. Child process is duplicate of parent process (i.e. it has same program
and data as parent process).
2. Child process has different program loaded in its address space.

When a parent process creates a child process, there are two possibilities
regarding their execution. They are
1. Parent process continues its execution concurrently with its child
process.
2. Parent process waits until some or all of its child processes have
terminated (i.e have completed their execution).

3.5.2 Process Termination


When process finishes executing its final statement, it gets terminated.
Operating system uses exit system call to delete the process. While terminating,
process may return status value (generally an integer number) to its parent
process. Also, all the resources allocated to the (terminating) process are de-
allocated by the operating system.
The usual conditions due to which process gets terminated are,
- Normal Exit (Voluntary)
- Error Exit (Voluntary)

3-7
- Fatal Error (Involuntary)
- Killed by another process (Involuntary)
A process may terminate another process by using TerminateProcess
system call. Generally, parent process uses this system call to terminate its child
process. Parent process may terminate one of its child process various reasons
like
- Child process has exceeded usage of some resources allocated to it.
- Task assigned to child process is no longer needed.
- Parent is exiting and operating system does not allow continuing child
processes if parent process is terminated.

As discussed above, some operating systems do not allow child process


continuing its execution if parent process is terminated. In such systems, if a
process is terminating, all its child processes are also terminated. This
phenomenon is called as cascading termination.

3.6 Inter-process Communication (IPC)

Processes which are run concurrently in the system are either independent
processes or cooperating processes. Independent processes are the process
which neither affect any other process nor get affected by any other process
executing in the system. Independent processes do not share any data with any
other process. On the other hand, cooperating processes are the process which
either affect other processes or get affected by other processes running in the
system. Any process that shares data with other process is a cooperative process.
Some of the reasons for allowing process cooperation are,
- Information sharing
Several users may need same piece of information. So, information
sharing must be provided.
- Computation speedup
For running a particular task faster, it must be broken into smaller
subtasks and they can be executed by cooperating with each other.
- Modularity
Modular systems can have different modules running concurrently.
- Convenience
A single user may work with different tasks at a time. E.g. user may
perform editing, printing, compiling etc in parallel.

Cooperating processes require inter-process communication mechanism for


exchanging data and information. Two fundamental models of inter-process
communication are
- Shared Memory Model
- Message Passing Model
These models are discussed in following sections.

3.6.1 Shared Memory Model


In this model, communicating processes have to establish a shared memory
region. Generally, shared memory resides in the address space of one of the
cooperating processes. This shared memory segment should be attached to
3-8
address space of another cooperating process(es). Normally, operating system
prevents one process from accessing memory of another process. For using
concept of shared memory, cooperating processes have to agree to remove this
restriction. Then the processes can communicate by reading and writing data
from/to this shared memory segment. The processes are responsible for ensuring
that they are not writing to the same memory location at the same time. The
model is visualized in figure 3.6.

Process 1

1
Shared memory
2
Process 2

Kernel

Figure 3.6: Shared Memory Model

The concept of shared memory model can be illustrated with the example
of producer-consumer problem which is the best example for cooperating
processes. A producer process produces information that is consumed by
consumer process. It resembles to client-server architecture. Server represents a
producer process whereas client represents consumer process. E.g. web server
produces web pages and client (web browser) consumes those web pages.
A buffer is made available which is filled by producer and emptied by
consumer process. This buffer will reside in the memory which is shared by
producer process and consumer process. There should be proper synchronization
among the producer and consumer processes (so that consumer should not try to
consume an item that has not yet been produced by the producer).
The buffer can be of two types – unbounded buffer and bounded buffer.
Unbounded buffer do not have any restrictions on size of buffer. i.e. Producer
process can produce any number of new items without waiting for emptying the
buffer. Only, consumer process may have to wait for new items to be produced by
producer process. On the other hand, bounded buffer assumes fixed buffer size.
In this case, consumer must wait if buffer is empty and producer has to wait if
buffer is full.

Advantages
- It allows maximum speed.

3-9
- It allows convenience of communication.
- Once shared memory is established, no assistance form kernel is required.

Disadvantages
- It is difficult to implement when the communicating processes are on
different computers.

3.6.2 Message Passing Model


It provides a mechanism to processes to communicate and to synchronize
their actions without sharing any address space. In this model, the cooperating
processes may be on different computers in a network. So, this model is very
much useful in distributed environment. The best example is a chat program
which is designed so that participants communicate with each other using
message passing model.

Process 1

Process 2

Kernel M

Figure 3.7: Message Passing Model

For performing message passing, at least two operations send and receive
must be provided. Messages can be sent with either fixed size or variable size.
Implementation of fixed-sized messages is simple as compared to variable-sized
messages.
When two processes want to communicate with each other, a
communication link must be established between them. Methods for logically
implementing communication link are,
- Direct or Indirect communication
- Synchronous or Asynchronous communication
- Automatic or Explicit buffering

3.6.2.1 Direct or Indirect Communication


Under direct communication, each process that wants to communicate
must explicitly specify the name of recipient or sender. In this scheme, the send
and receive operations are defined as,
3-10
- send (P, M1)
send message M1 to process P
- receive (Q, M2)
receive message M2 from process Q

A communication link in this scheme has following properties


- A link is automatically established between every pair of processes that
want to communicate with each other. The only requirement is that the
processes need to know each other’s identity.
- A link is associated with exactly two processes.
- Between each pair of processes, there exists exactly one link.

Under indirect communication, messages are sent to or received from


mailboxes or ports. Each mailbox has a unique identification. With this scheme,
processes can communicate with each other through any number of mailboxes.
The only requirement is that the mailbox through which processes want to
communicate must be shared mailbox. The send and receive operations are
defines as,
- send (A, M1)
send message M1 to mailbox A
- receive (A, M2)
receive message M2 from mailbox A

A communication link in this scheme has following properties


- A link is established between pair of processes only if both the processes
have a shared mailbox.
- A link may be associated with more than two processes.
- Between each pair of communicating processes, there may be number of
different links (each link corresponding to one mailbox).

3.6.2.2 Synchronous or Asynchronous communication


As seen above message passing is done through send and receive
operations. The message passing can be either blocking (i.e. synchronous) or
non-blocking (i.e. asynchronous).
In case of synchronous or blocking communication, the sending process is
blocked until the message is received by the receiving process or by the mailbox.
Also the receiving process is blocked until message is available.
In case of asynchronous or non-blocking communication, the sending
process sends the message and resumes the operation (i.e. it does not block).
Receiving process receives either a valid message or a null.

3.6.2.3 Automatic or Explicit Buffering


In direct as well as indirect communication, the exchanged messages are
held in temporary queue. Such queues can be implemented in three ways,
- Zero capacity (Blocking communication)
Queue has maximum length of zero. i.e. Queue cannot have any
messages waiting in it. So, sender must block until the message is received
by the receiver.
- Bounded capacity (Explicit Buffering)

3-11
The queue has finite length n. So, at the most n messages can reside
in the queue. If the queue is not full, the message sent is put in the queue
and operation is resumed by the sending process. If the queue is full,
sending process is blocked until space becomes available in the queue.
- Unbounded capacity
(Automatic Buffering, Non-blocking communication)
The queue length is infinite (until the limitations of memory). So,
any number of messages can be placed in it. Sender never blocks.

3.7 Threads

In various applications, multiple activities are going on at a time. Some of


them may get blocked from time to time. Such application can be decomposed into
multiple sub-processes (threads) to increase the overall speed of the
application. Such sub-processes share the address space of the process. So, thread
is a sub-process. When process is decomposed in multiple threads and these
threads are executed concurrently, it is called as multithreading. A web
browser may have multiple threads – one for displaying images, one for
displaying text, one for retrieving data from the network etc. A word processor
may have multiple threads – one for displaying graphics, one for responding to
keystrokes from user, one for performing spell check.
A traditional (or heavyweight) process is an executing program with single
thread of control. If a process has multiple threads of control, it can perform more
than one task at a time. Figure 3.8 shows difference between traditional single-
threaded process and multithreaded process. Thread is a lightweight process.
It is a basic unit of CPU utilization. It includes a thread ID, a program counter, a
register set and a stack. It shares code section, data section and other operating
system resources (e.g. open files) with other threads within the same process. As
threads are lightweight processes, they are easier to create or delete than the
processes. Creating a thread is almost 10 to 100 times faster than creating a
process.

code data files code data files

registers stack registers registers registers

stack stack stack

thread threads

Single-threaded process Multithreaded process

Figure 3.8: Single-threaded and multithreaded processes


3-12
Thread can also be defined as asynchronous code path within a process.
Threads can have priorities just like processes. Like processes, they can create
child threads. When a thread is created, a corresponding thread control block
(TCB) also gets created.

3.7.1 User threads and Kernel Threads


Threads can be implemented in user space as well as kernel space.
The threads implemented at user-level or in user space are called as user
level threads. User threads are supported above the kernel and are managed
without kernel support. User threads are represented as shown in figure 3.9.

Figure 3.9: User Thread

The threads implemented at kernel level or in kernel space are called as


kernel level threads. Kernel threads are supported and managed directly by
the operating system. Many of the operating systems including Windows XP,
Linux and Solaris support kernel threads. Kernel threads are represented as
shown in figure 3.10.

Figure 3.10: Kernel Thread

3.7.2 Benefits of Threads


Four major benefits of multithreaded programming are,
Responsiveness
Multithreading allows continuing execution of application even if some of
its part is blocked due to some lengthy operation. In this way, responsiveness to
user gets increased. E.g. Web browser may allow user to interact in one thread
even if another thread is busy in loading an image.
Resource Sharing
Threads share memory and resources of a process to which they belong.
Economy
As threads share memory and resources of the process to which they
belong, it is more economical to create threads and to perform context switch
among threads as compared to processes.
Utilization of multiprocessor architectures
Benefits of multithreading can be greatly increased in a multiprocessor
architecture where threads may run in parallel on different processors. (A single-
3-13
threaded process can run on a single CPU even if multiprocessor architecture is
available).

3.7.3 Multithreading Models


As discussed in 3.7.1, there can be user level threads as well as kernel level
threads. There must exist a relationship between user threads and kernel
threads. Three common ways of establishing this relationship are discussed in
following sections.

3.7.3.1 Many-to-one Model

Figure 3.11: Many to One Model

In this model, many user level threads are mapped to a single kernel level
thread as shown in figure 3.11.

Advantage
- Thread management is done in user space. So this model is efficient.

Disadvantages
- But, only one user thread can access the kernel thread at a time. So,
multiple threads are unable to run in parallel.
- Also, if a thread is blocked, entire process gets blocked.

3.7.3.2 One-to-one Model


In this model, each user thread is mapped to a separate kernel thread as
shown in figure 3.12.

K K K K

Figure 3.12: One to One Model

3-14
Advantage
- This model provides more concurrency than many to one model. Even if a
thread gets blocked, it does not block the entire process. Instead, other
threads continue their execution

Disadvantage
- Creating a user thread requires creation of corresponding kernel thread.
Overhead of creating kernel threads may degrade the performance.

3.7.3.3 Many-to-many Model

K K K

Figure 3.13: Many to Many Model

In this model, many user threads are mapped (by multiplexing) to a


smaller or equal number of kernel threads as shown in figure 3.13. Number of
kernel threads may be specific to particular application or to a particular
machine.

Advantages
- This model provides very fast and efficient thread management providing
better application performance and system throughput.
- User threads need not to worry about creation of kernel threads.
Disadvantage
- This model is complex to implement.

3-15

You might also like