Osy Class Notes Unit 3
Osy Class Notes Unit 3
Osy Class Notes Unit 3
Unit-3
3. Process
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 −
Unit-3
1 Stack
The process Stack contains the temporary data such as method/function
parameters, return address and local variables.
2 Heap
This is dynamically allocated memory to a process during its run time.
3 Text
This includes the current activity represented by the value of Program Counter
and the contents of the processor's registers.
4 Data
This section contains the global and static variables.
Program
A program is a piece of code which may be a single line or millions of lines. A computer
program is usually written by a computer programmer in a programming language. For
example, here is a simple program written in C programming language −
#include <stdio.h>
int main() {
printf("Hello, World! \n");
return 0;
}
A computer program is a collection of instructions that performs a specific task when
executed by a computer. When we compare a program with a process, we can conclude that
a process is a dynamic instance of a computer program.
Unit-3
When a process executes, it passes through different states. These stages may differ in
different operating systems, and the names of these states are also not standardized.
In general, a process can have one of the following five states at a time.
1 Start
This is the initial state when a process is first started/created.
2 Ready
The process is waiting to be assigned to a processor. Ready processes are waiting
to have the processor allocated to them by the operating system so that they can
run. Process may come into this state after Start state or while running it by but
interrupted by the scheduler to assign CPU to some other process.
3 Running
Once the process has been assigned to a processor by the OS scheduler, the
process state is set to running and the processor executes its instructions.
4 Waiting
Process moves into the waiting state if it needs to wait for a resource, such as
waiting for user input, or waiting for a file to become available.
5 Terminated or Exit
Once the process finishes its execution, or it is terminated by the operating
system, it is moved to the terminated state where it waits to be removed from
main memory.
Unit-3
Each process is represented in the operating system by a process control block (PCB)—also
called a task control block. A PCB is shown in Figure. 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.
CPU registers. The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-purpose
registers, plus any condition-code information. Along with the program counter, this state
Unit-3
information must be saved when an interrupt occurs, to allow the process to be continued
correctly afterward when it is rescheduled to run.
• 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
A Process Control Block is a data structure maintained by the Operating System for every
process. The PCB is identified by an integer process ID (PID). A PCB keeps all the
information needed to keep track of a process as listed below in the table −
1 Process State
The current state of the process i.e., whether it is ready, running, waiting, or whatever.
2 Process privileges
This is required to allow/disallow access to system resources.
3 Process ID
Unique identification for each of the process in the operating system.
Unit-3
4 Pointer
A pointer to parent process.
5 Program Counter
Program Counter is a pointer to the address of the next instruction to be executed for this
process.
6 CPU registers
Various CPU registers where process need to be stored for execution for running state.
9 Accounting information
This includes the amount of CPU used for process execution, time limits, execution ID etc.
10 IO status information
This includes a list of I/O devices allocated to the process.
The architecture of a PCB is completely dependent on Operating System and may contain
different information in different operating systems. Here is a simplified diagram of a PCB
−
Unit-3
The PCB is maintained for a process throughout its lifetime, and is deleted once the process
terminates.
Definition
The process scheduling is the activity of the process manager that handles the removal of
the running process from the CPU and the selection of another process on the basis of a
particular strategy.
Process scheduling is an essential part of Multiprogramming operating systems. Such
operating systems allow more than one process to be loaded into the executable memory at
a time and the loaded process shares the CPU using time multiplexing.
Unit-3
As processes enter the system, they are put into a ready queue, where they are ready and
waiting to execute on a CPU’s core This queue is generally stored as a linked list; a ready-
queue header contains pointers to the first PCB in the list, and each PCB includes a pointer
field that points to the next PCB in the ready queue. The system also includes other queues.
When a process is allocated a CPU core, it executes for a while and eventually terminates,
is interrupted, or waits for the occurrence of a particular event, such as the completion of an
I/O request. Suppose the process makes an I/O request to a device such as a disk. Since
devices run significantly slower than processors, the process will have to wait for the I/O to
become available. Processes that are waiting for a certain event to occur — such as
completion of I/O — are placed in a wait queue
The OS maintains all PCBs in Process Scheduling Queues. The OS maintains a separate
queue for each of the process states and PCBs of all processes in the same execution state
are placed in the same queue. When the state of a process is changed, its PCB is unlinked
from its current queue and moved to its new state queue.
The Operating System maintains the following important process scheduling queues −
Job queue − This queue keeps all the processes in the system.
Unit-3
Ready queue − This queue keeps a set of all processes residing in main memory,
ready and waiting to execute. A new process is always put in this queue.
Device queues − The processes which are blocked due to unavailability of an I/O
device constitute this queue.
The OS can use different policies to manage each queue (FIFO, Round Robin, Priority,
etc.). The OS scheduler determines how to move processes between the ready and run
queues which can only have one entry per processor core on the system; in the above
diagram, it has been merged with the CPU.
A common representation of process scheduling is a queueing diagram, such as that in
Figure 3.5. Two types of queues are present: the ready queue and a set of wait queues. 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.
Unit-3
• The process could be removed forcibly from the core, as a result of an interrupt or having
its time slice expire, and be put back in the ready queue.
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 and has its PCB and resources
deallocated.
Schedulers
Schedulers are special system software which handle process scheduling in various ways.
Their main task is to select the jobs to be submitted into the system and to decide which
process to run. Schedulers are of three types −
Long-Term Scheduler
Short-Term Scheduler
Medium-Term Scheduler
Unit-3
It is also called a job scheduler. A long-term scheduler determines which programs are
admitted to the system for processing. It selects processes from the queue and loads them
into memory for execution. Process loads into the memory for CPU scheduling.
The primary objective of the job scheduler is to provide a balanced mix of jobs, such as I/O
bound and processor bound. It also controls the degree of multiprogramming. If the degree
of multiprogramming is stable, then the average rate of process creation must be equal to
the average departure rate of processes leaving the system.
On some systems, the long-term scheduler may not be available or minimal. Time-sharing
operating systems have no long term scheduler. When a process changes the state from new
to ready, then there is use of long-term scheduler.
It is also called as CPU scheduler. Its main objective is to increase system performance in
accordance with the chosen set of criteria. It is the change of ready state to running state of
the process. CPU scheduler selects a process among the processes that are ready to execute
and allocates CPU to one of them.
Short-term schedulers, also known as dispatchers, make the decision of which process to
execute next. Short-term schedulers are faster than long-term schedulers.
Medium-term scheduling is a part of swapping. It removes the processes from the memory.
It reduces the degree of multiprogramming. The medium-term scheduler is in-charge of
handling the swapped out-processes.
Unit-3
A running process may become suspended if it makes an I/O request. A suspended process
cannot make any progress towards completion. In this condition, to remove the process
from memory and make space for other processes, the suspended process is moved to the
secondary storage. This process is called swapping, and the process is said to be swapped
out or rolled out. Swapping may be necessary to improve the process mix.
It is a process swapping
1 It is a job scheduler It is a CPU scheduler
scheduler.
It is almost absent or
It is also minimal in time It is a part of Time sharing
4 minimal in time sharing
sharing system systems.
system
Context Switch
A context switch is the mechanism to store and restore the state or context of a CPU in
Process Control block so that a process execution can be resumed from the same point at a
later time. Using this technique, a context switcher enables multiple processes to share a
single CPU. Context switching is an essential part of a multitasking operating system
features.
When the scheduler switches the CPU from executing one process to execute another, the
state from the current running process is stored into the process control block. After this, the
Unit-3
state for the process to run next is loaded from its own PCB and used to set the PC,
registers, etc. At that point, the second process can start executing.
Context switches are computationally intensive since register and memory state must be
saved and restored. To avoid the amount of context switching time, some hardware systems
employ two or more sets of processor registers. When the process is switched, the following
information is stored for later use.
Program Counter
Scheduling information
Base and limit register value
Currently used register
Changed State
I/O State information
Accounting information
Unit-3
Inter-process communication means the way in which processes communicates with each
other and synchronize actions.
We know that there are different processes running in our system. Some processes running
in background and some processes running in foreground. And these processes may need to
communicate with each other. So the way the processes communicate with each other is
known as inter-process communication.
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.
Why IPC?
There are several reasons for providing an environment that allows process cooperation:
• Information sharing
Since several applications may be interested in the same piece of information (for instance,
copying and pasting), we must provide an environment to allow concurrent access to such
information.
• Computation speedup
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. Notice that such a speedup can be achieved only if
the computer has multiple processing cores.
• Modularity
Unit-3
We may want to construct the system in a modular fashion, dividing the system functions
into separate processes or threads.
• Convenience
Both of the models just mentioned are common in operating systems, and many systems
implement both. Message passing is useful for exchanging smaller amounts of data, because
no conflicts need be avoided. 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.
Unit-3
write m
m
read
Messages sent by a process can be either fixed or variable in size. If only fixed-sized
messages can be sent, the system-level implementation is straightforward.
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.
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
Below we highlight a few examples of multithreaded applications:
• An application that creates photo thumbnails from a collection of images may use a separate thread to
generate a thumbnail from each separate image.
• A web browser might have one thread display images or text while another thread retrieves data from the
network.
• A word processor may have a thread for displaying graphics, another thread for responding to keystrokes
from the user, and a third thread for performing spelling and grammar checking in the background.
Unit-3
Process switching needs interaction with operating Thread switching does not need to interact with
2
system. operating system.
If one process is blocked, then no other process can While one thread is blocked and waiting, a second
4
execute until the first process is unblocked. thread in the same task can run.
In multiple processes each process operates One thread can read, write or change another
6
independently of the others. thread's data.
Advantages of Thread
Types of Thread
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
Unit-3
scheduling thread execution and for saving and restoring thread contexts. The application starts with a
single thread.
Advantages
Disadvantages
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
Kernel can simultaneously schedule multiple threads from the same process on multiple processes.
Unit-3
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.
1 User-level threads are faster to create and Kernel-level threads are slower to create and
manage. manage.
3 User-level thread is generic and can run Kernel-level thread is specific to the operating
on any operating system. system.
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 running even if part of it
is blocked or is performing a lengthy operation, thereby increasing responsiveness to the user. This
quality is especially useful in designing user interfaces. For instance, consider what happens when a
user clicks a button that results in the performance of a time-consuming operation. A single-threaded
application would be unresponsive to the user until the operation had been completed. In contrast, if
the time-consuming operation is performed in a separate, asynchronous thread, the application
remains responsive to the user.
2. Resource sharing.
Processes can share resources only through techniques such as shared memory and message passing.
Such techniques must be explicitly arranged by the programmer. However, threads share the
memory and the resources of the process to which they belong by default. The benefit of sharing
Unit-3
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 the 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 thread creation
consumes less time and memory than process creation. Additionally, context switching is typically
faster between threads than between processes.
4. Scalability.
The benefits of multithreading can be even greater in a multiprocessor architecture, where threads
may be running in parallel on different processing cores. A single-threaded process can run on only
one processor, regardless how many are available. We explore this issue further in the following
section.
Multithreading Models
Some operating system provides 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
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.
Unit-3
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.
Unit-3
There is one-to-one relationship of user-level thread to the kernel-level thread. The one-to-one model maps
each user thread to a kernel 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.