[go: up one dir, main page]

0% found this document useful (0 votes)
27 views51 pages

Chapter 2 Process

The document provides an overview of processes and threads in operating systems, detailing definitions, states, transitions, and control mechanisms. It explains the concept of multiprogramming, process creation and termination, process hierarchies, and the role of process control blocks. Additionally, it discusses threading, including types of threads, advantages, and various multithreading models.

Uploaded by

fabodi9727
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)
27 views51 pages

Chapter 2 Process

The document provides an overview of processes and threads in operating systems, detailing definitions, states, transitions, and control mechanisms. It explains the concept of multiprogramming, process creation and termination, process hierarchies, and the role of process control blocks. Additionally, it discusses threading, including types of threads, advantages, and various multithreading models.

Uploaded by

fabodi9727
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
You are on page 1/ 51

Topics to be covered

▪ Definition of process
▪ Process relationship
▪ Process states
▪ Process state transitions
▪ Process control
▪ Process control block
▪ Context switching
▪ Threads
▪ Concept of multithreads
▪ Benefits of threads
▪ Types of threads
What is Process?
Program Process
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.
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.
Multiprogramming execution
P1 P2 P3 Memory
Logical
Program Counter
P1
Processor
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.
Multiprogramming execution
P1 P2 P3 Memory
Logical
Program Counter
P1 P1
Processor
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.
Multiprogramming execution
P1 P2 P3 Memory
Logical
Program Counter
P1 Processor
Logical P2 Physical
Program Counter 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.
Multiprogramming execution
P1 P2 P3 Memory
Logical
Program Counter
P1
Processor
Logical P2 Physical
Program Counter Program Counter
P2
P3
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.
Process Model

• Fig. (a) Multiprogramming of four programs in memory


• Fig. (b) Conceptual model of 4 independent, sequential processes,
each with its own flow of control (i.e., its own logical program
counter) and each one running independently of the other ones.
• Fig. (c) over a long period of time interval, all the processes have
made progress, but at any given instant only one process is actually
running.
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
Process Creation
2. Execution of a process creation system call (fork) by running
process
• Running process will issue system call (fork) to create one or
more new process to help it.
• A process fetching large amount of data and execute it will create
two different processes one for fetching data and another to
execute it.

P1
P3
P2
Process Creation (Cont…)
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.
Process Creation (Cont…)
4. Initialization of batch process
• Applicable to only batch system found on large mainframe
Process Termination
1. Normal exit (voluntary)
• Terminated because process has done its work.
Process Termination
2. Error exit (voluntary)
• The process discovers a fatal error e.g. user types the command
cc foo.c to compile the program foo.c and no such file exists, the
compiler simply exit.
Process Termination
3. Fatal error (involuntary)
• An error caused by a process often due to a program bug e.g.
executing an illegal instruction, referencing nonexistent memory
or divided by zero.
Process Termination
4. Killed by another process (involuntary)
• A process executes a system call telling the OS to kill some other
process using kill system call.
Process Hierarchies
▪ Parent process can create child process, child process can create
its own child process.

P1 Parent process

P2 P4 Child process
P3
Process Hierarchies
▪ Parent process can create child process, child process can create
its own child process.

P1

P2 P4
P3 P3 Parent process

P5 P6 P5 P6 Child process

▪ UNIX has hierarchy concept which is known as process group


▪ Windows has no concept of hierarchy
• All the process as treated equal (use handle concept)
Handle
▪ When a process is created, the parent process is given a special
token called handle.
▪ This handle is used to control the child process.
▪ A process is free to pass this token to some other process.

P1

P2 P4
P3
Process State
1. Running – Process is actually
using the CPU Running
2. Ready – Process is runnable,
temporarily stopped to let
another process to run
Blocked Ready
3. Blocked – process is unable
to run until some external
event happens

Processes are always either executing (running) or waiting to


execute (ready) or waiting for an event (blocked) to occur.
Process State Blocked
Running
Ready
• Running – Process is actually
using the CPU
• Ready – Process is runnable,
temporarily stopped to let
Running
another process to run
• Blocked – process is unable to
run until some external event
happens
Blocked Ready
• Processes are always either
executing (running), waiting to
execute (ready) or waiting for
an event (blocked) to occur.
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. Running
printer is not available)
2. Scheduler picks another 3
2
1
process
• End of time-slice or
Blocked Ready
pre-emption. 4
3. Scheduler picks this process
4. Input becomes available,
event arrives (i.e. printer
become available)
Five State Process Model and Transitions
Dispatch
Admit Release
New Ready Running Exit
Time-out

Event
Occurs Event
Wait
Blocked

▪ 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
Seven State Process Model and Transitions

▪ suspend ready - if a process is in ready state and a more priority process


arrives then the previous process is sent to suspend ready state (inside the
secondary memory)
▪ suspend wait - if a process is in wait or block state and a more priority
process arrives then the previous process is sent to suspend block state (inside
the secondary memory) as soon as the ready or block state is empty it is sent to
ready or block state respectively.
Scheduler
▪ 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
Long-Term Scheduler
▪ job scheduler
▪ provide a balanced mix of jobs
▪ controls the degree of multiprogramming
Short-Term Scheduler
▪ CPU scheduler
▪ increase system performance
▪ also known as dispatchers
Medium-Term Scheduler
▪ process swapping scheduler
▪ removes the processes from the memory
▪ reduces the degree of multiprogramming
Comparison among Scheduler
Queue Diagram
Process is
Scheduled Process is
completed
Ready Queue to run
Admit Dispatch Exit
Processor

Time-out

Event
Event Wait
Occurs Blocked Queue
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.
Process Control Block (PCB)
▪ 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.
▪ PCB lies in kernel memory space.
Process Control Block (PCB) contains
• 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.
Process Control Block (PCB) contains
• 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.
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 and restore the state of
new process.
• Context switching is purely
overhead because system does
not perform any useful work
while context switch.
Steps performed by OS during Context switching
• Sequence of action:
1. OS takes control (through
interrupt)
2. Saves context of running
process in the process PCB
3. Reload context of new
process from the new
process PCB
4. Return control to new
process
Thread
▪ 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
▪ Thread is light weight process created by a process.
▪ Thread is a single sequence stream within a process.
▪ Thread has it own

1. program counter that keeps track of which instruction to


execute next.
2. system registers which hold its current working variables.
3. stack which contains the execution history.
Single Thread VS Multiple Thread
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 childrens.
▪ 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.
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.
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.
▪ Threads can execute in parallel on multiprocessors.
▪ 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).
▪ Speeding up the application.
Types of Threads
1. Kernel Level Thread
2. User Level Thread

User
Level
Threads

Kernel
Level
Threads
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
multiprocessing.
Kernel level thread
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 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.
Types of Threads (Cont…)

User thread are implemented by users. Kernel threads are implemented by OS.
OS doesn’t recognize user level threads. Kernel threads are recognized by OS.
Implementation of User threads is easy. Implementation of Kernel thread is
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 blocking If one kernel thread perform blocking
operation then entire process will be operation then another thread with in
blocked. same process can continue execution.
Example : Java thread, POSIX threads. Example : Window Solaris
Hybrid Thread
• 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.
Multi threading models

One to One Model Many to One Model Many to Many Model


Each user threads Multiple user threads Multiple user threads
mapped to one kernel mapped to one kernel multiplex to more than
thread. thread. one kernel threads.
Problem with this Problem with this Advantage with this
model is that creating model is that a user model is that a user
a user thread requires thread can block entire thread can not block
the corresponding process because we entire process because
kernel thread. have only one kernel we have multiple
thread. kernel thread.
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
Questions
1. Explain Process/Thread Life Cycle with diagram.
2. Explain process control block (PCB) with diagram.
3. Difference between process and thread.
4. Write various multi threading models.
5. Write benefits of threads.

You might also like