0% found this document useful (0 votes)
7 views50 pages

Understanding Process Management Concepts

Uploaded by

dh4nushshetty
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)
7 views50 pages

Understanding Process Management Concepts

Uploaded by

dh4nushshetty
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

Unit-1 – continued…

Process Management
Topics

• Process concept

• Process scheduling

• Operations on processes

• Inter-process communication
Process Concept

• An operating system executes a variety of programs that run


as a process.

The Process – is a program in execution


• Status of current activity of a process is given by
– value of the program counter

– contents of processor’s registers


Layout of a process in memory

• Memory layout of a process is divided into

– text section: the executable code


– Data section containing global variables

– Heap section is the memory that is dynamically allocated


during run time

– Stack section is temporary data storage for

• function parameters, return addresses, local variables


Layout of a process in memory
– sizes of the text and data sections are fixed
• as their sizes do not change during the program execution

– Stack and heap sections can shrink and grow dynamically during
program execution
– activation record : it is created each time a function is called and
pushed onto the stack
• It contains function parameters, local variables, and the return address
• the activation record is popped from the stack when control is returned from the
function

– Heap will grow as memory is dynamically allocated, and will shrink


when memory is returned to the system
– stack and heap sections grow toward one another
– OS ensures that they do not overlap
Layout of a process in memory

• Program is passive entity stored on disk (executable file); process is


active
• Program becomes process when an executable file is loaded into
memory
• Execution of program started via GUI mouse clicks, command line
entry of its name, etc.
• Two processes may be associated with the same program
– Ex: Same user may invoke many copies of the web browser program
– Each of these is a separate process
– Here, text sections are equivalent. But the data, heap, and stack
sections vary.
Layout of a process in memory
Memory Layout of a C program

Data
section
Process state

• As a process executes, it changes state

– New: The process is being created

– Running: Instructions are being executed

– Waiting: The process is waiting for some event to occur

– Ready: The process is waiting to be assigned to a


processor

– Terminated: The process has finished execution


Diagram of Process State
Process Control Block

• Each process is represented in the operating system by a


process control block (PCB)
• It also called as task control block.
• It contains many pieces of information associated with a
specific process, including these:
– Process state: The state may be new, ready, running, waiting, halted,
and so on
– Program counter: The counter indicates the address of the next
instruction to be executed for this process.
Process Control Block

– CPU registers: The registers vary in number and type, depending on


the computer architecture.
• The registers include accumulators, index registers, stack pointers, and general-
purpose registers, plus any condition-code information.

• Program counter and info of CPU registers must be saved when an interrupt occurs

• allows the process to be continued correctly afterward when it is rescheduled to


run

– CPU-scheduling information: This information includes a process


priority, pointers to scheduling queues, and any other scheduling
parameters.
Process Control Block

– Memory-management information. This information may include such


items as the value of the base and limit registers and the page tables, or
the segment tables, depending on the memory system used by the
operating system
– Accounting information: This information includes the amount of CPU and
real time used, time limits, account numbers, job or process numbers, and
so on
– I/O status information. This information includes the list of I/O devices
allocated to the process, a list of open files, and so on.

• In brief, the PCB simply serves as the repository for all the data
needed to start, or restart, a process, along with some accounting
data
Process Control Block
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 a time

• Thus, the user cannot simultaneously type in characters and


run the spell checker.
• Most modern operating systems have extended the process
concept to allow a process to have multiple threads of
execution and thus to perform more than one task at a time.
• This feature is especially beneficial on multicore systems,
where multiple threads can run in parallel.
• A multithreaded word processor could, for example, assign
one thread to manage user input while another thread runs
the spellchecker.
• On systems that support threads, the PCB is expanded to
include information for each thread.
Process Representation in Linux
Represented by the C structure task_struct

pid t_pid; /* process identifier */


long state; /* state of the process */
struct sched entity se; /* scheduling information */
struct task_struct *parent; /* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space of this process */
• The process control block in the Linux operating system is
represented by the C structure task_struct
• task_struct is found in the <include/linux/sched.h> include file
in the kernel source-code directory.
• This structure contains
– state of the process
– scheduling and memory-management information
– list of open files
– pointers to the process’s parent
– a list of its children and siblings.
Process Scheduling

• The objective of multiprogramming is to have some process


running at all times so as to maximize CPU utilization.

• The objective of time sharing is to switch a CPU core among


processes so frequently that users can interact with each
program while it is running.

• To meet these objectives, the process scheduler selects an


available process (possibly from a set of several available
processes) for execution on a core.
• Each CPU core can run one process at a time

• For a system with a single CPU core, there will never be more
than one process running at a time

• whereas a multicore system can run multiple processes at one


time.

• If there are more processes than cores, excess processes will


have to wait until a core is free and can be rescheduled.

• The number of processes currently in memory is known as the


degree of multiprogramming.
• Balancing the objectives of multiprogramming and time
sharing requires taking the general behaviour of a process
into account.

• In general, most processes can be described as either


– I/O bound or CPU bound.

• An I/O-bound process is one that spends more of its time


doing I/O than it spends doing computations.

• A CPU-bound process, in contrast, generates I/O requests


infrequently, using more of its time doing computations
Scheduling Queues

• Following are the queues involved in scheduling of processes:

– Ready queue – contains set of all processes residing in


main memory, ready and waiting to execute

– Wait queues – contains set of processes waiting for an


event (i.e., I/O)

– Processes migrate among the various queues


Ready and Wait Queues
Queueing-diagram representation of process scheduling
• The circles represent the resources that serve the queues, and
the arrows indicate the flow of processes in the system
• A new process is initially put in the ready queue.
• It waits there until it is selected for execution, or dispatched.
Once the process is allocated a CPU core 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
wait queue
– The process could create a new child process and then be placed in a
wait queue while it awaits the child’s termination
– The process could be removed forcibly from the core, (as a result of an
interrupt having its time slice expire) and be put back in the ready
queue.

• A process continues this cycle until it terminates, at which


time it is removed from all queues and has its PCB and
resources deallocated.
CPU Scheduling

• A process migrates among the ready queue and various wait


queues throughout its lifetime

• The role of the CPU scheduler is to select from among the


processes that are in the ready queue and allocate a CPU core
to one of them

• The CPU scheduler must select a new process for the CPU
frequently
• The CPU scheduler executes at least once every 100
milliseconds, although typically much more frequently
• Some operating systems have an intermediate form of
scheduling known as swapping
– Here key idea is that sometimes it can be advantageous to remove a
process from memory and thus reduce the degree of
multiprogramming
• swapped out from memory to disk

– Later, the process can be reintroduced into memory and its execution
can be continued where it left off
• swapped in from disk back to memory
Context Switch

• When CPU switches to another process, the system must save


the state of the old process and load the saved state for the
new process via a context switch

• When an interrupt occurs, the system needs to save the


current context of the process running on the CPU core so
that it can restore that context when its processing is done,
essentially suspending the process and then resuming it

• Context of a process is represented in the PCB


• Context-switch time is pure overhead; the system does no
useful work while switching
– Switching speed depends on memory speed, the number
of registers that must be copied, and the existence of
special instructions
– Switching speed is highly dependent on hardware support
• Some hardware provides multiple sets of registers per CPU
• Context-switch here requires changing the pointer to the current
register set

• A typical speed is a several microseconds


CPU Switch From Process to Process
Operations on Processes
• Operating System must provide mechanisms for:

– Process creation

– Process termination
Process Creation

• Parent process create children processes, which, in turn


create other processes, forming a tree of processes

• Most operating systems (including UNIX, Linux, and Windows)


identify processes according to a unique process identifier (or
pid), which is typically an integer number.

• The pid provides a unique value for each process in the


system, and it can be used as an index to access various
attributes of a process within the kernel.
Tree of processes in Linux
Tree of processes in Linux
• systemd is created when the system boots
– is the first user process
– Root/parent process for all user processes
– systemd process creates processes which provide additional
services
• In this example:
– logind and sshd are created by system
– logind is responsible for managing clients that log onto the
system
– Here, a client has logged on and is using the bash shell (pid
8416)
– Using the bash command-line interface, this user has created
the process ps as well as the vim editor
– sshd process is responsible for managing clients using ssh
• Parent-child Resource sharing options:
– Parent and children share all resources (CPU time, memory, files, I/O
devices)
– Children share subset of parent’s resources
– Parent and child share no resources

• Execution options:
– Parent and children execute concurrently
– Parent waits until children terminate
• Address space:
– Child is duplicate of parent
– Child has a program loaded into it
• UNIX examples
– fork() system call creates new process

– exec() system call used after a fork() to replace the process’ memory
space with a new program

– Parent process calls wait()waiting for the child to terminate


C Program to create a separate process
Creating a Separate Process via Windows API
Process Termination

• Process executes last statement and then asks the operating


system to delete it using the exit() system call.
– Returns status data from child to parent

– Process’ resources like physical and virtual memory, open files, I/O
buffers are deallocated and reclaimed by the operating system

• Parent may terminate the execution of child processes using


the abort() system call. Some reasons for doing so:
– Child has exceeded allocated resources

– Task assigned to child is no longer required


– The parent is exiting, and the operating systems does not allow a
child to continue if its parent terminates

• Some operating systems do not allow child to exist if its


parent has terminated.

• If a process terminates, then all its children must also be


terminated.
– cascading termination. All children, grandchildren, etc., are terminated.

– The termination is initiated by the operating system.

• A parent process may wait for the termination of a child


process by using the wait() system call.
• The call returns status information and the pid of the
terminated process

pid = wait(&status);

• A process that has terminated, but whose parent has not yet
called wait(), is known as a zombie process.

• All processes transition to this state when they terminate, but


generally they exist as zombies only briefly.

• Once the parent calls wait(), the process identifier of the


zombie process and its entry in the process table are released
• If parent terminated without invoking wait(), then the child
processes become orphans
• Traditional UNIX systems addressed this scenario by assigning
the init process as the new parent to orphan processes
• The init process periodically invokes wait(), there by allowing
the exit status of any orphaned process to be collected and
releasing the orphan’s process identifier and process-table
entry
• Linux allows systemd to inherit orphan processes and manage
their termination.
Android Process Importance Hierarchy

• Mobile operating systems often have to terminate processes


to reclaim system resources such as memory.
• Hierarchy of processes from most to least important:
– Foreground process
– Visible process
– Service process
– Background process
– Empty process

• If system resources must be reclaimed, Android will begin


terminating processes that are least important
Inter Process Communication

• Processes within a system may be independent or


cooperating

• A process is independent if it does not share data with any


other processes executing in the system

• A process is cooperating if it can affect or be affected by the


other processes executing in the system.

• Clearly, any process that shares data with other processes is a


cooperating process.
• There are several reasons for providing an environment that allows
process cooperation:
– Information sharing: Since several applications may require the same
piece of information (for instance, copying and pasting), we must
provide an environment to allow concurrent access to such
information
– Computation speed up: If we want a particular task to run faster, we
must break it into subtasks, each of which will be executing in parallel
with the others
– Modularity: We may want to construct the system in a modular
fashion, dividing the system functions into separate processes or
threads
• Cooperating processes need inter process communication
(IPC) mechanism

• Two fundamental models of IPC are:


– Shared memory

– Message passing

• Shared Memory
– In the shared-memory model, a region of memory that is shared by
the cooperating processes is established.

– Processes can then exchange information by reading and writing data


to the shared region
– Shared memory can be faster than message passing, since message-
passing systems are typically implemented using system calls and thus
require the more time-consuming task of kernel intervention.

– In shared-memory systems, system calls are required only to establish


shared memory regions.

– Once shared memory is established, all accesses are treated as routine


memory accesses, and no assistance from the kernel is required
• Message Passing
– In the message-passing model, communication takes place by means
of messages exchanged between the cooperating processes

– Message passing is useful for exchanging smaller amounts of data.

– Message passing is also easier to implement in a distributed system


than shared memory
Communications Models
(a) Shared Memory (b) Message Passing

You might also like