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

Operating System Cha 2

Uploaded by

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

Operating System Cha 2

Uploaded by

zerihun nana
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 53

CHAPTER-2

PROCESS AND PROCESS


MANAGEMENT
1
2.1. The process concept
Introduction:
 A process is an activity of some kind.

 It has a program code, input, output and a state.

 The process as an instance of a program in execution.

 The entity that can be assigned to and executed on a

processor.
 A unit of activity characterized by the execution of a

2
sequence of instructions.
Comparison between program and process
Program Process
 A program consists of a set of  A process is a sequence of
instructions in a programming instruction execution
language

 A program is a static object  A process is a dynamic object


that exists in a file i.e, a program in execution

 A program is loaded in to the  A process is loaded into the


secondary storage device. main memory

 The time span of a program is  The time span of a process is


unlimited limited
 A program is a passive entity  A process is an active entity
3
Process state
 The life cycle of a process can be divided in to several states.
 Each with its own characteristic to describe the process.
 It means that as a process starts executing, it goes through
different states
 Each process may be in one of the following states
1. New: The process is being created, but not yet admitted to the pool
of executable process.
2. Ready: The process is ready for execution and is waiting to be
allocated to a processor.

 All ready processes are kept in a queue and they keep waiting for

CPU time to be allocated by the OS in order to run, a program


4
Figure 2.1 Process State Diagram (PSD)

In system programming, an interrupt is a signal to the processor emitted by


5
hardware or software indicating an event that needs immediate attention.
3. Running: The process is now executing, using the CPU,

the instruction with in a process are executing.

4. Waiting: The process is waiting for some event to occur,

before it can continue execution.

 A waiting process lacks some resources other than the

CPU.

5. Terminate: A process terminates when it finishes


6
Process Control Block (PCB)
 Another responsibility of an OS is to collect all

information that it needs about a particular process in to


a data structure called Process Control Block (PCB)
 It acts as a repository for any information for processes.

 Note: When a process is created, the OS creates a

corresponding PCB and when it terminates, its PCB is


released to the pool of free memory locations from
7
which new PCBs are drawn.
 Information associated with a specific process:

1. Pointer: Pointer points to another process control block.

- Pointer is used for maintaining the scheduling list.

2. Process State: It indicates the current state of the Process

(i.e, new, ready, running, waiting and terminated).

3. Process ID: It is a unique process number or identifier that

identifies each process.


8
Figure: 2.2. Process control block
4. Program Counter: It contains the address of the next
instruction to be executed.
9 It is useful in indicating the status of a process.
5. CPU registers: It includes general purpose register, stack
pointers, index registers and accumulators etc.
 Number of register and type of register totally depends upon the

computer architecture.

6. Memory management information: Include the value of base


and limit registers, the page tables, or the segment tables.
 Depending on the memory system used by the operating system.

 This information is useful for deallocating the memory when the

process terminates.

10
7. Accounting information: Includes the amount of
CPU and real time used, time limits, job or process
numbers, account numbers etc.

8. CPU scheduling information: It includes a priority


of a process, address to scheduling queue and so on.

9. I/O status information: It includes the name of I/O


devices used by a process, list of open files information
about allocation of peripheral devices to process.
11
CPU Switch From Process to Process

12
Con’t…

 When current executing process P0 interrupted, the

OS saves the state into PCBo of P0 and load the


PCB1 of process P1 and P0 process will be idle and
P1 will be executing.
 After P1 execution is completed, the OS will save

state into PCB1 and reload state from PCBo and P1


process will be idle whereas P0 will be executing
13
Process Scheduling
1. Job queue: Set of all processes in the system.

2. Ready queue: Set of all processes residing in main memory,


ready and waiting to execute.

- They are ordered by priority, the highest priority process is at the


front of the queue. because queue works on FIFO principle.

Note: When we say that the process are listed together, we mean
that actually their PCBs are linked together.
o The header of the queue contains two pointers:
 The first pointer points to the first PCB.

14
The second points to the last PCB in the list.
3. Device queue: Some of process which are blocked due to
unavailability of an I/O device.
- These processes form a device queue.

- There is a separate queue for each I/O device.

Role of Device queue


 A process is created and is initially put in the ready queue,

when the CPU picks a process for its execution then a change
in the state of the process occurs.
 If it’s state is changed to block, it is added to the device queue

of the device whose services are required by it.


15
 This task is done by the process scheduler.
Operations on a Process
 A process is a sequential, the scenario is that there are many process

and a single CPU. So we use time slicing or multitasking.


 This is a type of pseudo-parallel processing and is also known as

concurrent sequential processing.

Note: In a true parallel system many CPUs are there.

Various operations that can be done on processes are:


I. Process creation

II. Process termination

III. Run a process

IV. Change a process priority

V.
16
Get process information
I. Process creation:
 It is the job of an OS to create process.
 There are four ways of achieving it.
 For a batch environment, a process is created in a response to the
submission of a job, usually tape and disk .
 In an interactive environment, a process is created when a new
user attempts to log on.
 The OS can create a process, to perform a function on behalf of a
user program.
 Spawned by existing process (A number of processes can be
generated from the main process), For the purpose of modularity or
17
to exploit parallelism.
II. Process Termination: Two types of termination

A. Normal Termination

 A process is done by using an exit system call.

 It returns some outputs data to its parent process.


 All the resources allocated to the process are freed by it at this
time.

B. Forced Termination

 The process is done by its parent process when it executes an abort


system call.

 When a parent process terminates the child process also


18
terminated automatically. Why?????
Reasons:
 The child process has executed allocated resources.

 Tasks assigned to child is no longer required.

 Parent is terminating and OS does not allow a child to

continue.
-This is known as cascading termination.

- UNIX supports cascading termination

19
Cooperating Processes
 Two process are said to be “serial” if the execution of one must

be complete before the execution of another process can start.


 While two processes are said to be “concurrent” if their

execution can overlap in time.


 On the other hand, the processes which execute concurrently

(parallely) and affect or get affected by other processes


executing in the system are known as cooperating processes.
 A process gets affected by another process when data sharing

or other resource sharing occurs.


20
Note: Any process which shares data with other processes
is cooperating processes.

Advantages of process cooperation


 Information sharing

 Computation speed-up

 Modularity

 Convenience

21
2.2. The Threads Concept
Introduction:
 A thread is similar to a sequential program, like a

thread also has a beginning, an end, a sequence etc.


 But a thread itself is not a program because it can not

run its own. So, it runs with in a program.


Note: A thread is a single sequential flow of control
with in a program. It also known as a light weight
22
process.
A thread has some similarities to processes
 A thread also has its lifecycle

 They also share process

 Each thread has its own stack and program counter

 A thread executes sequentially with in a process

Some dissimilarities b/n threads and processes


 Threads are not independent of one another.

 Threads require lesser creation time as compared to a process.

 Switching between threads is faster

23  Thread enhance communication efficiencies


Comparison of Process and Thread
Process Thread
 Process cannot share the same memory  Thread can share memory and files
 Process creation is time consuming  Thread creation is not time
consuming
 process execution is very slow  Thread execution is very fast
 It takes more time to terminate a  It takes less time to terminate
process
 It takes more time to switch b/n two  It takes less time to switch b/n two
processes threads
 Process is loosely coupled  Threads are tightly coupled
i.e. lesser resources sharing i.e. more resource sharing
 Communication b/n processes is  Communication b/n thread is easy
difficult and
efficient
 System calls are required for  System call are not required
communication why???
 It is a heavy weight process as it  It is a light weight process as it
24
requires requires
Advantages and Disadvantages of Thread
Advantages
 Thread minimize context switching time.

 Use of threads provides concurrency within a process.

 Efficient communication.

 Economy- It is more economical to create and context switch

threads.
 Utilization of multiprocessor architectures to a greater scale and
efficiency

Disadvantages
 More complicated code
25
 Deadlocks
Types of Thread
Threads are implemented in following two ways:

I. User Level Threads:


 In a pure ULT facility, all of the work of thread management is

done by the application.


 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
26

 The application begins with a single thread and begins
running in that thread and This application and its thread are
allocated to a single process managed by the kernel.

27 Figure 2.3 User-Level and Kernel-Level Threads


Advantages and Disadvantages of User Level Threads:

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

28 multiprocessing.
II. Kernel Level Threads
 Thread management done by the Kernel.

 There is no thread management code in the application level


simply an application programming interface (API) to the kernel
thread facility, Windows is an example of this approach.
 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 performs thread creation, scheduling and management

29 in Kernel space.
Advantages and Disadvantages of Kernel Level Threads
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 multithreaded.
Disadvantages
 Kernel threads are generally slower to create and manage than the

user threads.
 Transfer of control from one thread to another within same process
30
requires a mode switch to the Kernel.
Difference between User Level & Kernel Level
Thread

31
2.3. Multithreading and thread usage
Multithreading:
 Multithreading is similar to multitasking, but enables

the processing of multiple threads at one time, rather


than multiple processes.
 Since threads are smaller, more basic instructions

than processes, multithreading may occur within


processes.
32
Single and Multithreaded Processes

33
Multithreading Models
Multithreading models are three types
1. Many to Many Model
 Many user level threads multiplexes to the Kernel
thread of smaller or equal numbers.
 The number of Kernel threads may be specific to either
a particular application.

34
2. Many to One Model
 Maps many user level threads to one Kernel level thread.

 Thread management is done in user space.


 Only one thread can access the Kernel at a time, so multiple

threads are unable to run in parallel on multiprocessors.

35
3. One to One Model
 There is one to one relationship of user level thread to the
kernel level thread.
 Provides more concurrency than the many to one model.

 It support multiple thread to execute in parallel on

microprocessors.
 Disadvantage: creating user thread requires the

corresponding Kernel thread.

36
2.4. Inter process communication
 Cooperating process require IPC to exchange data and

information.
 Mechanism for processes to communicate and to

synchronize their actions.


 IPC is a facility provided by an OS, using which

cooperating processes can communicate with each other.


 Message system: processes communicate with each other

without resorting to shared variables.


37
Two Models

A. Shared Memory: Read and write data in shared region


maximum speed and convenience with computer.

B. Message passing: Exchange messages b/n cooperating


process useful for exchanging small amount of data
implementation ease for intercomputer communication
Vs SM
 IPC facility provides two operations:

• Send (message)- message size fixed or variable


38
• Receive (message)
 If P and Q wish to communicate, they need to:
• Establish a communication link between them.

• Exchange messages via send/receive.

 Implementation of communication link:

• Physical (e.g. shared memory, hardware bus)

• Logical (e.g. direct or indirect, synchronous or

asynchronous, buffering)

39
Implementation Questions
 How are links established?
 Can a link be associated with more than two processes?
 How many links can there be between every pair of
communicating processes?
 What is the capacity of a link?
 Is the size of a message that the link can accommodate
fixed or variable?
 Is a link unidirectional or bi-directional?
40
Direct Communication
 Properties of communication link:

 Links are established automatically b/n every pair

of process wishing to communicate.


 A link is associated with exactly one pair of

communicating processes.
 Between each pair there exists exactly one link

 The link may be unidirectional, but is usually

41
bi-directional
 processes must explicitly name the receiver or
sender of a message:
I. Symmetric Addressing:
• Send (P, message):- Send a message to process P
• Receive (Q, message):- Receive a message from process Q

II. Asymmetric Addressing:


 Variant of above scheme, only sender names the receiver and

receiver is does not have to know the name of specific :


• Send (P, message):- Send a message to process P

• Receive (id, message):- Receive a pending (posted)

message from any process.


42 - when the message arrives, id is set to the name of process
Indirect Communication
 Messages are directed and received from mailboxes (also

referred to as ports)
 Processes can communicate only if they share a mailbox

 Properties of communication link:

• Link established only if processes share a common mailbox.

• A link may be associated with many processes.

• Each pair of processes may share several communication

links.

43
• Link may be unidirectional or bi-directional
 Operations:

• Create a new mailbox

• Send and receive messages through mailbox

• Destroy a mailbox

 Messages are sent to or receive from mailboxes. The

send and received primitives can take the following


forms:
• Send (A, message):- Send a message to mailbox A

• Receive (A, message):- Receive a message from


44
mailbox A
 Mailbox sharing

• P1, P2, and P3 share mailbox A

• P1, sends, P2 and P3 receive

• Who gets the message?

 Solutions

• Allow a link to be associated with at most two processes

• Allow only one process at a time to execute a receive

operation
• Allow the system to select arbitrarily the receiver, Sender
45 is notified who the receiver was.
Synchronization
 Message passing may be either blocking or non-blocking

 Blocking is considered synchronous

• Blocking send: has the sender block until the message is

received
• Blocking receive: has the receiver block until a message is

available
 Non-blocking is considered asynchronous

• Non-blocking send: has the sender send the message and


46
Buffering
 Messages exchanged by communicating process resides

in a temporary queue.
 Such queues can be implemented in one of three ways

1. Zero capacity:- 0 messages, Sender must wait for


receiver

2. Bounded capacity:- finite length of n messages

- Sender must wait if link full

3.
47
Unbounded capacity:- infinite length, Sender never waits
Race Condition
 A race condition is defined as the situation in which

multiple threads or processes read and write a shared data


item at the same time.
 The final result depends on the order of execution.

 Instruction-by-instruction sequence determined by the OS

CPU Scheduler.
 Depends on which process "wins" the "race".

 Extremely difficult to debug because of its transient


48
nature, Lurking bugs possible.
Critical Section
 The critical section problem refers to the problem of how to
ensure that at most one process is executing its critical section
at a given time.
 Consider system of n processes {Po, P1, … pn-1}

 Each process has critical section segment of code.


• Process may be changing common variables, updating table,
writing file, etc.
• When one process in critical section, no other may be in its
critical section.
 Each process must ask permission to enter critical section in

entry section, may follow critical section with exit section,


49 then remainder section
Solution to Critical-Section Problem
I. Mutual Exclusion - If process P1 is executing in its critical

section, then no other processes can be executing in their

critical sections.

II. Progress - If no process is executing in its critical section

and there exist some processes that wish to enter their critical

section, then the selection of the processes that will enter the

critical section next cannot be postponed indefinitely.


50
III. Bounded Waiting - A bound must exist on the

number of times that other processes are allowed to

enter their critical sections after a process has made

a request to enter its critical section and before that

request is granted

 Assume that each process executes at a nonzero

51 speed
 Two approaches depending on if kernel is

preemptive or non-preemptive

 Preemptive:- allows preemption of process when

running in kernel mode.


 Non-preemptive:- runs until exits kernel mode,

blocks, or voluntarily yields CPU.


o Essentially free of race conditions in kernel mode
52
END
QUESTIONS
?
?
?
53

You might also like