You are on page 1of 45

Unit III

Process Management

Marks:14
3.1 Process, Process State, PCB

Process:
• A process is a program in execution.
• Process is also called as job, task or unit of work
• A process is a basic unit of work to be
implemented in system.
• A process is an 'active' entity. Program is a
'passive' entity.
• Attributes held by process include hardware
state, memory, CPU etc.
Process memory is divided into four sections ─
stack, heap, text and data.
1) The Text section includes compiled program
code, read in from non-volatile storage when
the program is launched. It contains values of
program counter and the contents of the
processor’s registers.
2) The Data section is made up the global and
static variables, allocated and initialized prior to
executing the main.
3) The Heap is used for the dynamic memory
allocation to a process during its run time, and is
managed via calls to new, delete, malloc, free,
etc.
4) The Stack is used to store temporary data such
as method/function parameters, return address
and local variables.
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.
• A process may be in one of the following states:
1) New: The process is being created.
2) Running: Process is running. Instructions are being
executed.
3) Waiting: The process is waiting for some event to
occur (such as an I/O completion or reception of a
signal).
4) Ready: Process is ready for execution. The process is
waiting for the availability of processor.
• Only one process can be running on any processor at
any instant. Many processes may be ready and waiting.
Process Control Block
• Each process is represented in the operating system by a
process control block (PCB)—also called a task
control block.
• Process Control Block is a data structure that contains
information of the process.
• A PCB is shown in Figure 3.3.
 PCB includes foll. in information:
• Process state: This specifies the process state i.e. new, ready,
running, waiting or terminated.
• Program number: It indicates unique process id.
• Program counter: This contains the address of the next
instruction that needs to be executed in the process.
• CPU 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 the different files that are
associated with the process.
• CPU-scheduling information: It includes a process priority,
pointers to scheduling queues, and any other scheduling
parameters.
• Memory-management information: It include page tables, or
the segment tables, depending on the memory system used. It
• I/O Status Information: It 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.
• Location of the Process Control Block: operating systems
place the PCB at the beginning of the kernel stack for the process
as it is a safe location.
• All PCB information must be saved when an interrupt occurs, to
allow the process to be continued correctly afterward. It is shown
in figure 3.4
3.2 Process Scheduling
• The two main objectives of the process
scheduling system are to keep the CPU busy at
all times and to deliver "acceptable" response
times for all programs, particularly for
interactive ones.
• The process scheduler must meet these
objectives by implementing suitable policies for
swapping processes in and out of the CPU.
3.2.1 Scheduling Queues
• All processes are stored in the job queue.
• Processes that are ready and waiting to execute are
placed in the ready queue.
• Processes waiting for a particular I/O device is called a
device queue . Each device has its own device queue.
• Other queues may also be created and used as needed.
• These queues is generally stored as a linked list.
• A ready-queue header contains pointers to the first and
final PCBs in the list. Each PCB includes a pointer
field that points to the next PCB in the ready queue.
This is shown in figure 3.5.
• Queueing-diagram representation of process scheduling is shown
in figure 3.6.
• Each rectangular box represents a queue. Two types of queues
are present: the ready queue and a set of device 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 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 child process and wait
for the child’s termination.
3. The process could be removed forcibly from the CPU, as
a result of an interrupt, and be put back in the ready
queue.
• In first two cases, process switches from the waiting
state to the ready state and is then put back in the
ready queue.
3.2.2 Schedulers
• A process migrates among the various
scheduling queues throughout its lifetime. The
OS must select processes from these queues ,
for scheduling purposes. The selection process
is carried out by the scheduler.
• Types of Scheduler:
1. Long-term Scheduler or Job Scheduler
2. Short-term Scheduler Or CPU Scheduler
3. Medium-term Scheduler
1. Long-term Scheduler or Job Scheduler:
• Used in batch system or a very heavily loaded
system. Such processes are spooled(Job queue).
• selects which processes should be brought into the
ready queue
• It runs less frequently.
• The long-term scheduler controls the degree of
multiprogramming i.e. the total number of
processes that are present in the ready state..
• Time-sharing operating systems like Windows and
UNIX usually don't have a long term scheduler.
• Processes can be described as either:
– I/O-bound process – spends more time doing I/O
than computations, many short CPU bursts
– CPU-bound process – spends more time doing
computations; few very long CPU bursts
• Long-term scheduler must select a good process mix
of I/O-bound and CPU-bound processes to increase the
performance of system.
2. Short-term Scheduler or CPU Scheduler:
• Short-Term Scheduler takes the process from ready
queue allocates the CPU to one of them.
• It runs more frequently(within a few seconds).
• Short-term scheduler must be fast.
• Short term scheduler has less control over Degree of
Multi-programming.
• This is the place where all the scheduling algorithms
are used i.e. it can be FCFS or Round-Robin or SJF or
any other scheduling algorithm.
3. Medium-term Scheduler :
• It is also known as the Swapping scheduler.
• It is responsible for swapping of a process from the Main
Memory to Secondary Memory(swapped out) and vice-
versa(swapped in).
• This is helpful in reducing the degree of multiprogramming.
• Swapping is also useful to improve the mix of I/O bound
and CPU bound processes in the memory.
Medium Term Scheduler

Short Term Scheduler


Long Term
Scheduler
Comparison
Long term scheduler Medium term scheduler Short term scheduler

Long term scheduler is a Medium term is a Short term scheduler is


job scheduler. process of swapping called a CPU scheduler.
schedulers.
The speed of long term is The speed of medium The speed of short term
lesser than the short term is in between short is fastest among the
term. and long term scheduler. other two.

Long term controls the Medium term reduces The short term provides
degree of the degree of lesser control over the
multiprogramming. multiprogramming. degree of
multiprogramming.

The long term selects the Medium term can Short term selects those
processes from the pool reintroduce the process processes that are ready
and loads them into into memory and to execute.
memory for execution. execution can be
continued.
3.2.3 Context Switch
• A context switching is a process that involves switching of
the CPU from one process to another.
• Switching the CPU to another process requires performing a
state save of the current process and a state restore of a
different process. This task is known as a context switch.
• The context of process is represented in the PCB . It
includes the value of the CPU registers, the process state, etc.
• Context switching happens very very frequently.
• Context-switch time is pure overhead, because the system
does no useful work while switching.
• So context switches ( state saves & restores ) need to be as
fast as possible.
• Context switching is used to achieve multitasking.
3.3 Interprocess Communication
• Processes within system may be either independent
processes or cooperating.
• A process is independent if it cannot affect or be affected
by the other processes executing in the system. A process
that does not share data with any other process is also
independent.
• A process is cooperating if it can affect or be affected by
the other processes executing in the system. A process
that shares data with other processes is a cooperating
process.
Interprocess Communication
• Advantages of cooperating processes:
1. Information sharing
2. Computation speedup
3. Modularity
4. Convenience
• Cooperating processes need interprocess
communication (IPC)
• Two models of IPC
1. Shared Memory
2. Message Passing
1. Shared Memory Model
• An area of memory shared among the processes that wish
to communicate .
• Processes can then exchange information by reading and
writing data to the shared
• The form of the data and the location are determined by
these processes. Control is not under the operating
system.
• Need provide process synchronization mechanism
• System calls are required only to establish shared
memory region. Hence Fastest mechanism
• It is useful in exchanging large amounts of data
• Example : producer–consumer
2. Message Passing Model
• Allows processes to exchange data in the form of
messages.
• A message-passing facility provides two operations:
1. send(message) receive(message)
• If processes P and Q wish to communicate, they need to:
1. Establish a communication link between them
2. Exchange messages via send/receive
• The message size is either fixed or variable
• Implemented using system calls ,Hence Slower than
Shared Memory.
• It is useful in exchanging small amount of data
• Also useful in a distributed environment
• Example: an Internet chat program
Message Passing Model cont…
• Implementation of communication link
– Physical:
• Shared memory
• Hardware bus
• Network
– Logical:
• Direct or indirect
• Synchronous or asynchronous
• Automatic or explicit buffering
3.4 Threads
• A thread is a basic unit of CPU utilization.
• It comprises a thread ID, a program counter, a register
set, and a stack.
• Traditional ( heavyweight ) processes have a single
thread of control. There is one program counter, and one
sequence of instructions that can be carried out at any
given time.
• Multi-threaded applications have multiple threads within
a single process, each having their own program
counter, stack and set of registers, and shares
common code, data, and certain structures such as
open files.
• An application typically is implemented as a separate process
with several threads of control.
• For example in a word processor, one thread may check
spelling and grammar, second thread processes user input,
while a third thread loads images and a fourth does backup of
the file being edited.
• Another example is a web server - Multiple
threads allow for multiple requests to be
satisfied simultaneously. A daemon thread
would listen at a port, create a child for every
incoming request, and then go back to
listening to the port
• Benefits of Multi-threading
1. Responsiveness - may allow continued execution if part of
process is blocked especially important for user interfaces
process is blocked,
2. 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.
3. Economy - Creating and managing threads ( and context
switches between them ) is much faster than performing the
same tasks for processes. Thread creation is about 30
times faster and context switching is about 5 times faster.
4. Scalability, i.e. Utilization of multiprocessor architectures -
A single threaded process can only run on one CPU, whereas
the execution of a multi-threaded application may be split
amongst available processors.
Types of threads
• Two types of threads:
1. User threads
2. kernel threads
• User threads are supported above the kernel and are
managed without kernel support. These are the threads
that application programmers would put into their
programs.
• Kernel threads are supported and managed directly
by the operating system.
• Operating systems—including Windows, Linux, Mac
OS X, and Solaris—support kernel threads.
• A relationship must exist between user threads and
kernel threads.
Multithreading Models
• A relationship must exist between user threads
and kernel threads.
• 3 ways of establishing relationship:
1. many-to-one model
2. one-to-one model,
3. many-to-many model
Many-to-One Model
• This model maps many user-level
threads to single kernel thread.
• Advantage: The thread management
is done on the user level so it is more
efficient.
• Disadvantage:
1) The entire process will block if a
thread makes a blocking system call.
2) Only one thread can access the
kernel at a time, multiple threads are
unable to run in parallel on
multicore systems.
• Example OS:
– Green threads for Solaris
One-to-One Model
• This model maps each user-level thread to a kernel thread
• It provides more concurrency to a kernel thread.
• Advantages:
1. multiple thread can run on multiple processor.
2. The entire system will not blocked if a thread makes a
blocking system call
• Disadvantages:
1. overhead of creating kernel threads can burden the
performance of an application
• Drawback can be tackled by restricting the number of threads.
• Example OS:
– Linux, Windows,
– Solaries 9 and later
Many-to-Many Model
• This model maps many user-level
threads to a smaller or equal number
of kernel threads.
• The number of kernel threads may be
specific to either a particular
application or a particular machine.
• Advantage: If a user thread is blocked
we can schedule others user thread to
other kernel thread. Thus, System
doesn’t block if a particular thread
is blocked.
• It is the best multi threading model.
• Example OS:
– Solaries prior to 9,
– Windows with the ThreadFiber
package
Two level threads Model
• Similar to Many to Many thread model, except
that it allows certain user threads to be bound
to kernel thread
• Examples OS: IRIX ,HP-UX , Tru64 UNIX , Solaris 8
and earlier
3.5 Process Management Commands
ps: process status
• This command is used to display currently active
processes
• Syntax: $ ps
• Example:

• From the output we can see four columns,


– PID: This is the process id.
– TTY: this represents the terminal type.
– TIME: This is the total time the process has been running for.
– CMD: This is the command that launches the process.
• Options of ps command
– -f : It is used to display full listing of attributes of a
process.
– -u: Shows the activities of any specified user
– -a: It shows the processes of all users.
– -e: It displays processes including user and system
processes
– -x: It displays processes not executed in the terminal
kill:
• The kill command sends a signal to a process.
• This can terminate a process (the default), interrupt it, suspend
it, crash it, and so on.
• To kill a process, we first need its PID
• Syntax: $ kill PID
• Example: $ kill 257
• Use the killall command to kill a process by name
$ killall sleep
• To list the signal names, pass the -l option as follows:
$ kill -l
Example:
sleep:
• This command pauses the execution for an amount of
time which is defined by NUMBER
• Syntax: $ sleep NUMBER[SUFFIX]
• Sleep's default unit of time is seconds.
• Support the following units of time:
– s: seconds
– m: minutes
– h: hours
– d: days
• Example: $sleep 2m -will create a pause of 2 minutes
$sleep 30 -will create a pause of 30 seconds
wait:
• waits until all background processes are completed and
then exits.
• the wait command only works with processes run in the
background.
• Example:

• The first line starts the sleep command in the background. The
second grabs the process ID of the most recently executed
backgrounded process ($!). The script then displays that
information, waits for that process to finish and displays the exit
status.
exit:
• used to exit the shell
• Syntax: exit [n]
• Example:
• $exit exit the shell
• $exit 110 exits the shell with a return of status 10
• Return status is important and used to tell error, warnings
and notifications
“0” means the program has executed successfully.
“1” means the program has minor errors.
• Display the last return status: $ echo $?

You might also like