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

Unit 2

Uploaded by

Zeel Goyani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit 2

Uploaded by

Zeel Goyani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

Operating System (102040402)

Unit-2
Process and
Threads
Management
Process concept
Section - 1
What is Process?

Program Process

3
What is Process?
🞂 Process is a program under execution.
🞂 Process is an abstraction of a running program.
🞂 Process is an instance of an executing program, including the current
values of the program counter, registers & variables.
🞂 Each process has its own virtual CPU.

4
Multiprogramming
🞂 The real CPU switches back and forth from process to process.
🞂 This rapid switching back and forth is called multiprogramming.
🞂 The number of processes loaded simultaneously in memory is
called degree of multiprogramming.

5
Multiprogramming execution
P1 P P Memor
2 3 yLogical
Program Counter
P1
Process
or
Logical Physical
Program Counter Program Counter
P2

Logical
Program Counter
P3

🞂 There are three processes, one processor (CPU), three logical


program counter (one for each processes) in memory and one physical
program counter in processor.
🞂 Here CPU is free (no process is running).
🞂 No data in physical program counter.
6
Multiprogramming execution
P1 P P Memor
2 3 yLogical
Program Counter
P1
P Process
1 or
Logical Physical
Program Counter Program Counter
P2

Logical
Program Counter
P3

🞂 CPU is allocated to process P1 (process P1 is running).


🞂 Data of process P1 is copied from its logical program counter to the
physical program counter.

7
Multiprogramming execution
P1 P P Memor
2 3 yLogical
Program Counter
P Process
1 or
Logical P Physical
Program Counter 2 Program Counter
P2 P1

Logical
Program Counter
P3

🞂 CPU switches from process P1 to process P2.


🞂 CPU is allocated to process P2 (process P2 is running).
🞂 Data of process P1 is copied back to its logical program counter.
🞂 Data of process P2 is copied from its logical program counter to the
physical program counter.
8
Multiprogramming execution
P1 P P Memor
2 3 yLogical
Program Counter
P1
Process
or
Logical Physical
P
Program Counter Program Counter
2
P2
P
3
Logical
Program Counter
P3

🞂 CPU switches from process P2 to process P3.


🞂 CPU is allocated to process P3 (process P3 is running).
🞂 Data of process P2 is copied back its logical program counter.
🞂 Data of process P3 is copied from its logical program counter to the
physical program counter.
9
Process Model

Multiprogramming of Conceptual model of 4 independent, sequential Over a long period of time interval, all the
four programs in memory processes, each with its own flow of control processes have made progress, but at any
(i.e., its own logical program counter) and each given instant only one process is actually
one running independently of the other ones. running.

10
Process Creation
1. System initialization
⮩ At the time of system (OS) booting various
processes are created
⮩ Foreground and background processes are created
⮩ Background process – that do not interact
with user e.g. process to accept mail
⮩ Foreground Process – that interact with user

2. Execution of a process creation system


call (fork) by running process
⮩ Running process will issue system call (fork) to
P create one or more new process to help it.
P1
3
2 ⮩ A process fetching large amount of data and
execute it will create two different processes one
for fetching data and another to execute it.

11
Process Creation
3. A user request to create a new
process
⮩ Start process by clicking an icon (opening word
file by double click) or by typing command.

4. Initialization of batch process


⮩ Applicable to only batch system found on large
mainframe
12
Process Termination

1. Normal exit 2. Error exit (voluntary)


(voluntary) ⮩ The process discovers a fatal error e.g. user
⮩ Terminated because types the command cc foo.c to compile the
process has done its work. program foo.c and no such file exists, the
compiler simply exit.
13
Process Termination

3. Fatal error (involuntary) 4. Killed by another process


⮩ An error caused by a process often due (involuntary)
to a program bug e.g. executing an ⮩ A process executes a system call
illegal instruction, referencing telling the OS to kill some other
nonexistent memory or divided by zero. process using kill system call.

14
Process Hierarchies
🞂 Parent process can create child process, child P
process can create its own child process. 1 Parent
process
🞂 UNIX has hierarchy concept which is
P P
known as process group 2 P 4 Child
3 process
🞂 Windows has no concept of hierarchy
⮩ All the process as treated equal (use handle P P
concept) 5 6

P
3 Parent
process
P P
5 6 Child
process

15
Handle
🞂 When a process is created, the parent
process is given a special token called
handle. P
1 Parent
🞂 This handle is used to control the child process
process. P
P
P
Child
2 4
🞂 A process is free to pass this token to 3 process
some other process.

16
Process states
Section - 2
Process states
Runni
ng Running:
Process is actually using
the CPU

Ready
Ready:
Process is runnable, temporarily stopped to
let another process to run

Block
ed Blocked:
Process is unable to run until some external
event happens

18
Process states
Blocke
Runni
Rea
🞂 Running – Process is
d
dy
ng
actually using the CPU
🞂 Ready – Process is
runnable, temporarily Runni
stopped to let another ng
process to run
🞂 Blocked – process is
unable to run until some
external event happens Block
Ready
ed
🞂 Processes are always
either executing
(running), waiting to
execute (ready) or
waiting for an event
(blocked) to occur. 19
Process states
transitions
Section - 3
Process State Transitions
🞂 When and how these transitions occur (process
moves from one state to another)?
1. Process blocks for input or waits for an event (i.e.
printer is not available)
Runni
2. Scheduler picks another process
ng
▪ End of time-slice or pre-emption.
3. Scheduler picks this process 3
1 2
4. Input becomes available, event arrives (i.e. printer
become available)
Block
Ready
ed 4

Processes are always either executing (running) or waiting to


execute (ready) or waiting for an event (blocked) to occur.

21
Five State Process Model and Transitions
HD RA RA -
D M Dispat M
Adm ch Relea
Ne it Rea Runni se Ex
w dy Time- ng it
out
Even
Eve
t
nt
Occu
Wai
Block
rs
t
ed
RA
M
🞂 New – process is being created
🞂 Ready – process is waiting to run (runnable), temporarily stopped to
let another process run
🞂 Running – process is actually using the CPU
🞂 Blocked – unable to run until some external event happens
🞂 Exit (Terminated) – process has finished the execution
Exercis A process resides in which memory during
e different state?
22
Queue Diagram
HD RA RA -
D M Dispat M
Adm ch Relea
Ne it Rea Runni se Ex
w dy Time- ng it
out
Even
Eve
t
nt Process
Occu
Block
Wai is Process
rs
ed
t
Schedu is
Ready
RA Adm led comple
Queue Process ted
M it to run
Dispatch or Exit

Time-
out

Event
Even Wait
t Blocked
Occu Queue
23
Process Control Block
(PCB)
Section - 4
What is Process Control Block (PCB)?
🞂 A Process Control Block (PCB) is a data
structure maintained by the
operating system for every process.
🞂 PCB is used for storing the collection
of information about the processes.
🞂 The PCB is identified by an integer
process ID (PID).
🞂 A PCB keeps all the information
needed to keep track of a process.
🞂 The PCB is maintained for a process
throughout its lifetime and is deleted
once the process terminates.
🞂 The architecture of a PCB is completely
dependent on operating system and
may contain different information in
different operating systems. 25
Fields of Process Control Block (PCB)
🞂 Process ID - Unique identification for each of the
process in the operating system.
🞂 Process State - The current state of the process i.e.,
whether it is ready, running, waiting.
🞂 Pointer - A pointer to parent process.
🞂 Priority - Priority of a process.
🞂 Program Counter - Program Counter is a pointer to
the address of the next instruction to be executed for
this process.
🞂 CPU registers - Various CPU registers where process
need to be stored for execution for running state.
🞂 IO status information - This includes a list of I/O
devices allocated to the process.
🞂 Accounting information - This includes the amount of
CPU used for process execution, time limits etc. 26
Context switching
Section - 5
Context switching
🞂 Context switch means stopping one
process and restarting another
process.
🞂 When an event occur, the OS saves the
state of an active process (into its
PCB) and restore the state of new
process (from its PCB).
🞂 Context switching is purely overhead
because system does not perform any
useful work while context switch.
🞂 Sequence of action:
1. OS takes control (through interrupt)
2. Saves context of running process in the
process PCB 1. Time slice has elapsed
3. Reload
Exercis Whatcontext of new process
causes (Reasons from 2.the
for) context Process with a higher priority has become
e new process PCB ready to run.
switching?
4. Return control to new process 28
Threads
Section - 6
What is Threads?
🞂 Thread is light weight process created by a process.
Processes are used to execute large, ‘heavyweight’ jobs such
as working in word, while threads are used to carry out smaller
or ‘lightweight’ jobs such as auto saving a word document.

Thread
s

30
What is Threads?
🞂 Thread is light weight process created by a process.
🞂 Thread is a single sequence stream within a process.
🞂 Thread has it own

⮩ program counter that keeps track of which instruction to execute next.


⮩ system registers which hold its current working variables.
⮩ stack which contains the execution history.

31
Single Threaded Process VS Multiple Threaded Process
▪ A single-threaded
process is a process
with a single thread.
▪ A multi-threaded
process is a process
with multiple threads.
▪ The multiple threads
have its own registers,
stack and counter but
they share the code and
data segment.

32
Comparison between
process and thread
Section - 7
Similarities between Process & Thread
🞂 Like processes threads share CPU and only one thread is running at
a time.
🞂 Like processes threads within a process execute sequentially.
🞂 Like processes thread can create children's.
🞂 Like a traditional process, a thread can be in any one of several
states: running, blocked, ready or terminated.
🞂 Like process threads have Program Counter, Stack, Registers and
State.

34
Dissimilarities between Process & Thread
🞂 Unlike processes threads are not independent of one another.
🞂 Threads within the same process share an address space.
🞂 Unlike processes all threads can access every address in the task.
🞂 Unlike processes threads are design to assist one other. Note that
processes might or might not assist one another because processes may
be originated from different users.

35
Benefits/Advantages of
threads
Section - 8
Benefits/Advantages of Threads
🞂 Threads minimize the context switching time.
🞂 Use of threads provides concurrency within a process.
🞂 Efficient communication.
🞂 It is more easy to create and context switch threads.
🞂 With threads, an application can avoid per-process overheads
⮩ Thread creation, deletion, switching easier than processes.
🞂 Threads have full access to address space (easy sharing).

37
Types of threads
Section - 9
Types of Threads
1. Kernel Level Thread
2. User Level Thread

User
Level
Threa
ds

Kernel
Level
Threa
ds

39
Types of Threads
User Level Thread Kernel Level Thread
User thread are implemented by Kernel threads are implemented by
users. OS.
OS doesn’t recognize user level
Kernel threads are recognized by OS.
threads.
Implementation of user threads is Implementation of kernel thread is
easy. complex.
Context switch time is less. Context switch time is more.
Context switch requires no hardware Context switch requires hardware
support. support.
If one user level thread perform If one kernel thread perform blocking
blocking operation then entire operation then another thread with
process will be blocked. in same process can continue
Example : Java thread, POSIX execution.
Example : Window Solaris
threads.

40
Hybrid Threads
🞂 Combines the advantages of user level and
kernel level thread.
🞂 It uses kernel level thread and then
multiplex user level thread on to some
or all of kernel threads.
🞂 Gives flexibility to programmer that how
many kernel level threads to use and how
many user level thread to multiplex on each
one.
🞂 Kernel is aware of only kernel level
threads and schedule it.

41
Multi Threading Models
Section - 10
Multi Threading Models

One to One Many to One Many to Many


Model Model Model
Each user threads Multiple user Multiple user
mapped to one threads mapped threads multiplex
kernel thread. to one kernel to more than one
Problem with this thread. kernel threads.
model is that Problem with this Advantage with
creating a user model is that a this model is that
thread requires user thread can a user thread can
the corresponding block entire not block entire
kernel thread. process because process because
we have only one we have multiple
kernel thread. kernel thread.
43
Pthread function calls
Section - 11
Pthread function calls
1. Pthread_create:- Create a new thread
2. Pthread_exit:- Terminate the calling thread
3. Pthread_join:- Wait for a specific thread to exit
4. Pthread_yield:- Release the CPU to let another thread run
5. Pthread_attr_init:- Create and initialize a thread’s attribute structure
6. Pthread_destroy:- Remove a thread’s attribute structure

45
System calls
Section - 12
System calls
🞂 A system call is the programmatic way in which a computer program
requests a service from the kernel of the operating system it is
executed on.
🞂 A system call is a way for programs to interact with the operating
system.
🞂 A computer program makes a system call when it makes a request
to the operating system’s kernel.
🞂 System call provides the services of the operating system to the
user programs via Application Program Interface(API).
🞂 It provides an interface between a process and operating system to
allow user-level processes to request services of the operating system.
🞂 System calls are the only entry points into the kernel system.
🞂 All programs needing resources must use system calls.

47
System calls
🞂 ps (process status):- The ps (process status) command is used to provide
information about the currently running processes, including their
process identification numbers (PIDs).
🞂 fork:- Fork system call is used for creating a new process, which is
called child process, which runs concurrently with the process that
makes the fork() call (parent process).
🞂 wait:- Wait system call blocks the calling process until one of its
child processes exits or a signal is received. After child process
terminates, parent continues its execution after wait system call
instruction.
🞂 exit:- Exit system call terminates the running process normally.
🞂 exec family:- The exec family of functions replaces the current running
process with a new process.

48

You might also like