SlideShare a Scribd company logo
Chapter -
two
Process Management
Process and Threads
1
2
Outlin
e
• Process concepts
• The process model
• Process Management
• Types of Processes
• Process creation and
termination
• Process states and PCB
• Threads
• Process scheduling algorithms
• Inter process communication
3
Process
concepts
• Process vs. Program
– Program
• It is sequence of instructions defined to perform
some task
• It is a passive entity
– Process
• It is a program in execution
• It is an instance of a program running on a
computer
• It is an active entity
• A processor performs the actions defined by a
process
4
The process model
• A process is just an executing program.
• The CPU switches back and forth from
process to process,
• this rapid switching back and forth is
called multiprogramming.
• However, at any instant of time, the CPU
runs only one program.
• Thus giving the users illusion of parallelism.
5
System
calls
• The interface between the operating system and the
user program is defined by the set system calls that
the operating system provides.
• System calls provide the interface between a
running program and the operating system.
– Generally available as assembly-language
instructions.
– Languages defined to replace assembly language
for systems
– Programming allow system calls to be made
directly (e.g., C, C++)
6
System calls…
• Three general methods are used to pass
parameters between a running program and the
operating system.
– Pass parameters in registers.
– Store the parameters in a table in memory, and
the table address is passed as a parameter in a
register.
– Push (store) the parameters onto the stack by the
program, and pop off the stack by operating
system.
• Types of system calls
– Process control
– File management
– Device management
– Information maintenance
Process Management
• Overview
• The most fundamental task of modern operating
systems is process management.
• It includes:
– Creation of processes
– Allocation of resources to processes
– Protecting resources of each processes from other
processes
– Enabling processes to share and exchange information
– Enabling synchronization among processes for
proper sequencing and coordination when
dependencies exist
– Termination of processes
7
Types of
Processes
– There are two types of processes:
• Sequential Processes
– Execution progresses in a sequential fashion, i.e. one after the
other
– At any point in time, at most one process is being executed
• Concurrent Processes
– There are two types of concurrent processes
– True Concurrency (Multiprocessing)
» Two or more processes are executed
simultaneously in a
multiprocessor environment
» Supports real parallelism
– Apparent Concurrency
(Multiprogramming)
» Two or more processes are
executed in parallel in a uniprocessor
environment by switching from one process
9
Process creation
• Operating system needs some way to make sure
all the necessary processes are created and
terminated.
• There are four principal events that cause a
processes to be created:
1. System initialization
2. Execution of a process creation system call
by a running process.
3. A user request to create a new process.
4. Initialization of a batch of job.
10
Process
creation…
1. System initialization - When an operating
system is booted, typically several processes
are created.
• Some of these are foreground processes, (i.e.
processes that interact with users and perform work
for them.)
• Others are background processes, which are
not associated with particular users.
– Example: one background process may be designed to accept
incoming-mail sleeping most of the time, incoming request for
web pages
• Processes that stay in the background to handle
some activities are called daemons.
• In linux: ps, ps –fl, ps -efl
• In windows: ctrl-alt-del
11
Process creation…
2- Creation of processes by running process
• Often a running process will issue system calls to
create one or more new processes to help it to do its
job.
Example: If a large amount of data is being fetched
over a network for subsequent processing,
• one process to fetch the data and put them in a
shared buffer,
• while the second process removes the data item
and process them.
12
Process creation…
3 - A user request to create processes
• Users can start a program by typing a
command or (double) clicking an icon.
• Taking either of these actions starts a new process
and runs the selected program in it.
13
Process
creation…
4 - Initiation of a batch of job
• Here user can submit batch of jobs to the system
(possibly remotely) in main frame computer.
• When the operating system decides that it has the
resources to run another job, it creates a new process
and runs the next job from the input queue in it.
• In all these cases, a new process is created by having
an existing process execute a process creation
system call.
Process creation:
Summary
• One process can create another process, perhaps to do
some work for it
• The original process is called the parent
• The new process is called the child
• The child is an (almost) identical copy of parent (same
code, same data, etc.)
• The parent can either:
– wait for the child to complete, or
– continue executing in parallel (concurrently) with the child
• There are also two possibilities in terms of the address
space of the new process:
- the child process is a duplicate of the parent process
- the child process has a new program loaded
to it
14
Process creation:
Summary…
• In Linux, a process creates a child process using
the system call fork( )
• In child process, fork( ) returns 0
• In parent process, fork( ) returns process id of
new child
• Child often uses exec( ) to start another
completely different program
• In windows, a single system call,
CreateProcess, handles both
- process creation and
- loading the correct program in to the new
process. 15
16
Process Termination
• After a process has been created, it starts
running and does whatever its job is.
• Sooner or later the new process will
terminate, due to one of the following
conditions:
1. Normal exit (voluntary)
2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)
17
Process Termination…
1Normal exit – Most process terminates because they
have done their work.
Example:
• When a compiler has compiled the program given to
it, the compiler executes a system call to tell the
operating system that it is finished.
• This call is Exit in UNIX and ExitProcess in windows.
• Screen oriented programs also support
voluntary termination.
– Word processors, Internet browser and similar programs
always have an icon or menu item that the user can click to
tell the process to terminate. File  Exit
Process Termination…
2- Error exit - When the process discovers an error.
• Example: If the user typed a command
javac Function.java
• To compile the program and no such file exists,
the compiler simply exits.
3- Fata error – is an error caused by the process, often
due to a program bug.
• Example:
– Executing an illegal instruction
– Referencing non-existent memory
– Dividing by zero 22
Process Termination…
4 - Killed by another process – a process executes a
system call telling the operating system to kill some
other process.
• In UNIX this call is Kill.
• In window TerminateProcess.
23
Process Hierarchies
• For various reasons, a process can creates
another process.
• The child process can itself create more
processes, forming a process hierarchy.
• Example: How Linux initializes itself when it is
started.
– A special process, called init is present in the boot
image.
– It reads a file telling how many terminals there are
– Then it forks off one new process per terminal
– These processes waiting for something to log in
– If log in is successful, the login process executes a shell to
accept commands.
– These commands may start up more processes…..
• All processes created above belong to a single tree
with
init at the root. pstree
24
21
Process States
– During its lifetime, a process passes
through a number of states.
– new: The process is being created.
– ready: The process is waiting to be assigned
to a processor.
– running: Instructions are being executed.
– Waiting/blocked: The process is waiting for
some event to occur such as I/O operation
– terminated: The process has finished
execution.
Diagram of Process
State
22
23
State Transitions in Five-State Process
Model
• new  ready
– Admitted to ready queue; can now be considered by
CPU scheduler
• ready  running
– CPU scheduler chooses that process to execute next,
according to some scheduling algorithm
• running  ready
– Process has used up its current time slice
• running  blocked
– Process is waiting for some event to occur (for I/O
operation to complete, etc.)
• blocked  ready
– Whatever event the process was waiting on has occurred
Process Control Block
(PCB)
Information associated with each
process.
– Process state
– Program counter
– CPU registers
– CPU scheduling information
– Memory-management information
– Accounting information
– I/O status information
24
CPU Switch From Process to
Process
25
CPU Switch From Process to
Process Context Switch
Time
Task A
Task A
Save context
of Task A Start
Task B
Start
Task B
Time
Switch to Task B
Context Switch Time
PC
Register 0
:
:
Other Context
Load context
of Task B
PC
Register 0
:
:
Other Context
26
27
Thread
s
• Thread vs. Process
– A thread is a dispatchable unit of work
(lightweight process) that has independent
context, state and stack
– A process is a collection of one or more threads
and associated system resources
– Traditional operating systems are single-
threaded systems
– Modern operating systems are
multithreaded systems
28
Threads…
• The thread has:
– A program counter that keeps track of which
instruction to execute next.
– Has registers which hold its current working variables
– Has a stack which contains the execution history
• Processes are used to group resources together.
• Threads are the entities scheduled for execution on
the CPU.
29
Threads
…
• What threads add to the process model is to allow
multiple executions to take place in the same
process environment.
• Having multiple threads running in parallel in one
process is analogous to having multiple processes
running in parallel in one computer.
– In the former case the threads share an address space,
open files, and other resources.
– In the later case processes share physical memory,
disks, printers and other resources.
Threads…
• Threads are sometimes called lightweight process.
• Multithreading- is to describe the situation of
allowing multiple threads in the same process.
30
31
Single and Multithreaded
Processes…
• A thread is a single sequence of execution
within a program
• Multithreading refers to multiple
threads of control within a single
program
– each program can run multiple threads of
control within it, e.g., Web Browser, MS Words,
…
Single and Multithreaded
Processes…
32
Thread Types
User
Threads
• Thread
management done
by user-level
threads library
• Examples
- POSIX Pthreads
- Mach C-threads
- Solaris threads
Kernel Threads
• Supported by the
Kernel
• Examples
-Windows
95/98/NT/2000/XP
…
- Solaris
- Tru64 UNIX
- BeOS
- Linux
39
Multithreading
Models
 Many-to-One
 One-to-One
 Many-to-
Many
Many-to-
One
 Many user-level
threads mapped to
single kernel thread.
 Thread management is
done in user space by
the thread library.
Drawbacks:
• 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.
One-to-
One
 Each user-level thread
maps to kernel thread.
 It allows another thread
to run when a thread
makes a blocking system
call.
 It allows multiple threads
to run on parallel in
multiprocessor system.
Drawback
 creating user
threads requires
creating the
corresponding
kernel threads.
 Examples
- Windows , OS/2
Many-to-Many
Model
 Allows many user level threads to
be mapped to many kernel
threads.
• developers can create
as many user threads
as necessary and
• the corresponding
Kernel threads can run
in parallel on a
multiprocessor machine.
• when a thread performs
a blocking system call,
the kernel can schedule
another thread for
execution.
• Solaris 2, Windows…
38
CPU
Scheduling
• Basic Concepts
• Scheduling Criteria
• Scheduling
Algorithms
CPU Scheduler: OS
• Selects among processes in memory that are ready to
be executed, and allocates CPU to one of them.
• CPU scheduling decisions may take place
when a process:
1.Switches from running to waiting state.
2.Switches from running to ready state.
3.Switches from waiting to ready.
4. Terminates.
• Scheduling under 1 and 4 is nonpreemptive.
• All other scheduling is preemptive.
39
40
CPU Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – # of processes that complete
their execution per unit time
• Turnaround time – total time required to
execute a particular process
• Waiting time – amount of time a process waits in
the ready queue
• Response time – amount of time it takes from when a
request was submitted until the first response is
produced.
41
CPU Optimization Criteria
• Maximize throughput - run as many jobs as possible in a
given amount of time.
– This could be accomplished easily by running only short jobs
or by running jobs without interruptions.
• Minimize response time - quickly turn around interactive
requests.
– This could be done by running only interactive jobs and letting
the batch jobs wait until the interactive load ceases.
• Minimize turnaround time - move entire jobs in and out of
the system quickly.
– This could be done by running all batch jobs first (because batch
jobs can be grouped to run more efficiently than interactive jobs).
42
CPU Optimization
Criteria…
• Minimize waiting time - move jobs out of the READY
queue as quickly as possible.
– This could only be done by reducing the number of users
allowed on the system so the CPU would be available
immediately whenever a job entered the READY queue.
• Maximize CPU efficiency - keep the CPU busy 100 percent of
the time.
– This could be done by running only CPU-bound jobs (and not
I/O- bound jobs).
• Ensure fairness for all jobs - give everyone an equal
amount of CPU and I/O time.
– This could be done by not giving special treatment to any
job, regardless of its processing characteristics or priority.
43
Process Scheduling
Algorithms
• Part of the operating system that makes scheduling
decision is called scheduler and the algorithm it
uses is called scheduling algorithm
• The Process Scheduler relies on a process
scheduling algorithm,
– based on a specific policy, to allocate the CPU and
move jobs through the system.
• There are six process scheduling algorithms that
have been used extensively.
44
First-Come, First-Served (FCFS)
Scheduling
Basic Concept
• is a non preemptive scheduling algorithm that handles
jobs according to their arrival time:
• the earlier they arrive, the sooner they’re served.
• It’s a very simple algorithm to implement because it
uses a FIFO queue.
• In a strictly FCFS system there are no WAIT queues (each
job is run to completion).
First-Come, First-Served (FCFS)
Scheduling
Process Burst Time
(msec)
P1 24
P2 3
P3 3
• Suppose that the processes arrive in the order: P1 , P2 ,
P3
The Gantt Chart for the schedule is:
P1 P2
P3
0 24 27 30
• Waiting time for P1 = 0; P2 = 24; P3 = 27
• Average waiting time: (0 + 24 + 27)/3 = 17
• Turnaround time for P1=24, P2=27, P3=30
65
FCFS Scheduling (Cont.)
• Waiting time for P1 = 6; P2 = 0; P3 = 3
• Average waiting time: (6 + 0 + 3)/3 =
3
• Turnaround time for P1 =30, P2 =3, P3 =6
• Avg turnaround time: (30+3+6)/3= 13
• Much better than previous
case.
30
Suppose that the processes arrive in the
order
P2 , P3 , P1 .
• The Gantt chart for the schedule is:
P2 P3
P1
0 3 6
66
47
Implementation: FCFS
1. Input the processes along with their burst time (bt).
2. Find waiting time (wt) for all processes.
3. As first process that comes need not to wait so
waiting time for process 1 will be 0 i.e. wt[0] = 0.
4. Find waiting time for all other processes i.e. for
process i -> wt[i] = bt[i-1] + wt[i-1] .
5. Find turnaround time = waiting_time + burst_time
for all processes.
6. Find average waiting time =
total_waiting_time / no_of_processes.
7. Similarly, find average turnaround time
= total_turn_around_time /
no_of_processes.
48
Shortest-Job-First (SJF)
Scheduling
• Associate with each process the length of its next
CPU burst.
• Use these lengths to schedule the process with
the shortest time.
• Two schemes:
– nonpreemptive – once CPU given to the process it
cannot be preempted until completes its CPU burst.
– preemptive – if a new process arrives with CPU burst
length less than remaining time of current executing
process, preempt. This scheme is know as the
Shortest-Remaining- Time-First (SRTF).
• SJF is optimal – gives minimum average waiting time
for a given set of processes.
Process Arrival Time Burst
Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• Waiting time P1=0-0=0, P2=8-2=6, P3=7-4=3, P4=12-5=7
• Average waiting time = (0 + 6 + 3 + 7)/4 = 4
• Turnaround time: P1= 7-0=7, P2=12-2=10, P3=8-4=4, P4=16-5=11
• Avg. turnaround time: (7+10+4+11)/4= 8
Example of Non-Preemptive
SJF
P3 P2
• SJF (non-preemptive)
P1
0
3
P4
7 8 12
16
69
Example of Preemptive
SJF
Process Arrival Time Burst
Time
P1 0.0 7
P2 2.0 4
P3 4.0 1
P4 5.0 4
• Average waiting time = (9 + 1 + 0 +2)/4 = 3
• Turnaround time:P1=16-0=16, P2=7-2=5, P3=5-4=1, P4=11-5=6
• Avg. turnaround time= (16+5+1+6)/4=7
• SJF (preemptive)
P1
P2
0
P4
2 4 5
7
P3 P2 P1
11
16
70
51
Priority
Scheduling
• A priority number (integer) is associated with each process
• The CPU is allocated to the process with the highest
priority (smallest integer  highest priority).
– Preemptive
– nonpreemptive
• SJF is a priority scheduling where priority is the predicted
next CPU burst time.
• Problem  Starvation – low priority processes may
never execute.
• Solution  Aging – as time progresses increase the
priority of the process.
52
Round Robin (RR)
• Each process gets a small unit of CPU time (time
quantum), usually 10-100 milliseconds.
• After this time has elapsed, the process is preempted
and added to the end of the ready queue.
• If there are n processes in the ready queue and the time
quantum is q, then each process gets 1/n of the CPU
time in chunks of at most q time units at once.
• No process waits more than (n -1)q time units.
• Performance
– q large  FIFO
– q small  q must be large with respect to context
switch, otherwise overhead is too high.
Example of RR with Time Quantum =
20
Process Burst
Time
P1 53
P2 17
P3 68
P4 24
• The Gantt chart
is:
• Avg turnaround time = (134+37+162+121)/4 = 113.5
• Typically, higher average turnaround than SJF, but
better response.
P1 P2
P3
0 20 37
P4 P1 P3 P4 P1 P3 P3
57 77 97 117 121 134 154
162
73

More Related Content

PPTX
unit 2- process management of Operating System
PPTX
OS - Chapter # 3 for the development of os
PPTX
Operating system Chapter 3 Part-1 process and threads.pptx
PPTX
Lecture 2 process
PDF
Lecture 2- Processes.pdf
PPTX
Operating Systems chap 2_updated2.pptx
PPTX
operating system module 2 presentation notes
PPTX
Operating Systems chap 2_updated2 (1).pptx
unit 2- process management of Operating System
OS - Chapter # 3 for the development of os
Operating system Chapter 3 Part-1 process and threads.pptx
Lecture 2 process
Lecture 2- Processes.pdf
Operating Systems chap 2_updated2.pptx
operating system module 2 presentation notes
Operating Systems chap 2_updated2 (1).pptx

Similar to operating system process management with example (20)

PPTX
Process and thread Management Operating system
PPTX
OS (1).pptx
PPTX
Operating Systems Process Management.pptx
PPTX
Os presentation process
PPTX
2Chapter Two- Process Management(2) (1).pptx
PPTX
Chapter two process.pptx
PDF
Process Management.pdf
PPT
9 cm402.13
PDF
Comprehensive Introduction to Operating Systems and Their Functions
PDF
Lecture_3-Process Management.pdf
PPT
Lecture5
PPTX
Unit 1 process management operating system.pptx
PDF
process.pdfzljwiyrouyaeutoaetodtusiokklhh
PPT
Ch2_Processes_and_process_management_1.ppt
PPTX
Process management
PPT
Lecture 8 9 process_concept
PPTX
Chapter -2 operating system presentation
PPTX
Lecture_Slide_4.pptx
PPTX
2 Process and Thread_java20231123-3.pptx
PDF
Cs8493 unit 2
Process and thread Management Operating system
OS (1).pptx
Operating Systems Process Management.pptx
Os presentation process
2Chapter Two- Process Management(2) (1).pptx
Chapter two process.pptx
Process Management.pdf
9 cm402.13
Comprehensive Introduction to Operating Systems and Their Functions
Lecture_3-Process Management.pdf
Lecture5
Unit 1 process management operating system.pptx
process.pdfzljwiyrouyaeutoaetodtusiokklhh
Ch2_Processes_and_process_management_1.ppt
Process management
Lecture 8 9 process_concept
Chapter -2 operating system presentation
Lecture_Slide_4.pptx
2 Process and Thread_java20231123-3.pptx
Cs8493 unit 2
Ad

Recently uploaded (20)

PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
RMMM.pdf make it easy to upload and study
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Classroom Observation Tools for Teachers
PDF
01-Introduction-to-Information-Management.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Supply Chain Operations Speaking Notes -ICLT Program
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Microbial disease of the cardiovascular and lymphatic systems
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Final Presentation General Medicine 03-08-2024.pptx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
TR - Agricultural Crops Production NC III.pdf
O7-L3 Supply Chain Operations - ICLT Program
RMMM.pdf make it easy to upload and study
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Classroom Observation Tools for Teachers
01-Introduction-to-Information-Management.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Microbial diseases, their pathogenesis and prophylaxis
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
VCE English Exam - Section C Student Revision Booklet
Supply Chain Operations Speaking Notes -ICLT Program
Ad

operating system process management with example

  • 2. 2 Outlin e • Process concepts • The process model • Process Management • Types of Processes • Process creation and termination • Process states and PCB • Threads • Process scheduling algorithms • Inter process communication
  • 3. 3 Process concepts • Process vs. Program – Program • It is sequence of instructions defined to perform some task • It is a passive entity – Process • It is a program in execution • It is an instance of a program running on a computer • It is an active entity • A processor performs the actions defined by a process
  • 4. 4 The process model • A process is just an executing program. • The CPU switches back and forth from process to process, • this rapid switching back and forth is called multiprogramming. • However, at any instant of time, the CPU runs only one program. • Thus giving the users illusion of parallelism.
  • 5. 5 System calls • The interface between the operating system and the user program is defined by the set system calls that the operating system provides. • System calls provide the interface between a running program and the operating system. – Generally available as assembly-language instructions. – Languages defined to replace assembly language for systems – Programming allow system calls to be made directly (e.g., C, C++)
  • 6. 6 System calls… • Three general methods are used to pass parameters between a running program and the operating system. – Pass parameters in registers. – Store the parameters in a table in memory, and the table address is passed as a parameter in a register. – Push (store) the parameters onto the stack by the program, and pop off the stack by operating system. • Types of system calls – Process control – File management – Device management – Information maintenance
  • 7. Process Management • Overview • The most fundamental task of modern operating systems is process management. • It includes: – Creation of processes – Allocation of resources to processes – Protecting resources of each processes from other processes – Enabling processes to share and exchange information – Enabling synchronization among processes for proper sequencing and coordination when dependencies exist – Termination of processes 7
  • 8. Types of Processes – There are two types of processes: • Sequential Processes – Execution progresses in a sequential fashion, i.e. one after the other – At any point in time, at most one process is being executed • Concurrent Processes – There are two types of concurrent processes – True Concurrency (Multiprocessing) » Two or more processes are executed simultaneously in a multiprocessor environment » Supports real parallelism – Apparent Concurrency (Multiprogramming) » Two or more processes are executed in parallel in a uniprocessor environment by switching from one process
  • 9. 9 Process creation • Operating system needs some way to make sure all the necessary processes are created and terminated. • There are four principal events that cause a processes to be created: 1. System initialization 2. Execution of a process creation system call by a running process. 3. A user request to create a new process. 4. Initialization of a batch of job.
  • 10. 10 Process creation… 1. System initialization - When an operating system is booted, typically several processes are created. • Some of these are foreground processes, (i.e. processes that interact with users and perform work for them.) • Others are background processes, which are not associated with particular users. – Example: one background process may be designed to accept incoming-mail sleeping most of the time, incoming request for web pages • Processes that stay in the background to handle some activities are called daemons. • In linux: ps, ps –fl, ps -efl • In windows: ctrl-alt-del
  • 11. 11 Process creation… 2- Creation of processes by running process • Often a running process will issue system calls to create one or more new processes to help it to do its job. Example: If a large amount of data is being fetched over a network for subsequent processing, • one process to fetch the data and put them in a shared buffer, • while the second process removes the data item and process them.
  • 12. 12 Process creation… 3 - A user request to create processes • Users can start a program by typing a command or (double) clicking an icon. • Taking either of these actions starts a new process and runs the selected program in it.
  • 13. 13 Process creation… 4 - Initiation of a batch of job • Here user can submit batch of jobs to the system (possibly remotely) in main frame computer. • When the operating system decides that it has the resources to run another job, it creates a new process and runs the next job from the input queue in it. • In all these cases, a new process is created by having an existing process execute a process creation system call.
  • 14. Process creation: Summary • One process can create another process, perhaps to do some work for it • The original process is called the parent • The new process is called the child • The child is an (almost) identical copy of parent (same code, same data, etc.) • The parent can either: – wait for the child to complete, or – continue executing in parallel (concurrently) with the child • There are also two possibilities in terms of the address space of the new process: - the child process is a duplicate of the parent process - the child process has a new program loaded to it 14
  • 15. Process creation: Summary… • In Linux, a process creates a child process using the system call fork( ) • In child process, fork( ) returns 0 • In parent process, fork( ) returns process id of new child • Child often uses exec( ) to start another completely different program • In windows, a single system call, CreateProcess, handles both - process creation and - loading the correct program in to the new process. 15
  • 16. 16 Process Termination • After a process has been created, it starts running and does whatever its job is. • Sooner or later the new process will terminate, due to one of the following conditions: 1. Normal exit (voluntary) 2. Error exit (voluntary) 3. Fatal error (involuntary) 4. Killed by another process (involuntary)
  • 17. 17 Process Termination… 1Normal exit – Most process terminates because they have done their work. Example: • When a compiler has compiled the program given to it, the compiler executes a system call to tell the operating system that it is finished. • This call is Exit in UNIX and ExitProcess in windows. • Screen oriented programs also support voluntary termination. – Word processors, Internet browser and similar programs always have an icon or menu item that the user can click to tell the process to terminate. File  Exit
  • 18. Process Termination… 2- Error exit - When the process discovers an error. • Example: If the user typed a command javac Function.java • To compile the program and no such file exists, the compiler simply exits. 3- Fata error – is an error caused by the process, often due to a program bug. • Example: – Executing an illegal instruction – Referencing non-existent memory – Dividing by zero 22
  • 19. Process Termination… 4 - Killed by another process – a process executes a system call telling the operating system to kill some other process. • In UNIX this call is Kill. • In window TerminateProcess. 23
  • 20. Process Hierarchies • For various reasons, a process can creates another process. • The child process can itself create more processes, forming a process hierarchy. • Example: How Linux initializes itself when it is started. – A special process, called init is present in the boot image. – It reads a file telling how many terminals there are – Then it forks off one new process per terminal – These processes waiting for something to log in – If log in is successful, the login process executes a shell to accept commands. – These commands may start up more processes….. • All processes created above belong to a single tree with init at the root. pstree 24
  • 21. 21 Process States – During its lifetime, a process passes through a number of states. – new: The process is being created. – ready: The process is waiting to be assigned to a processor. – running: Instructions are being executed. – Waiting/blocked: The process is waiting for some event to occur such as I/O operation – terminated: The process has finished execution.
  • 23. 23 State Transitions in Five-State Process Model • new  ready – Admitted to ready queue; can now be considered by CPU scheduler • ready  running – CPU scheduler chooses that process to execute next, according to some scheduling algorithm • running  ready – Process has used up its current time slice • running  blocked – Process is waiting for some event to occur (for I/O operation to complete, etc.) • blocked  ready – Whatever event the process was waiting on has occurred
  • 24. Process Control Block (PCB) Information associated with each process. – Process state – Program counter – CPU registers – CPU scheduling information – Memory-management information – Accounting information – I/O status information 24
  • 25. CPU Switch From Process to Process 25
  • 26. CPU Switch From Process to Process Context Switch Time Task A Task A Save context of Task A Start Task B Start Task B Time Switch to Task B Context Switch Time PC Register 0 : : Other Context Load context of Task B PC Register 0 : : Other Context 26
  • 27. 27 Thread s • Thread vs. Process – A thread is a dispatchable unit of work (lightweight process) that has independent context, state and stack – A process is a collection of one or more threads and associated system resources – Traditional operating systems are single- threaded systems – Modern operating systems are multithreaded systems
  • 28. 28 Threads… • The thread has: – A program counter that keeps track of which instruction to execute next. – Has registers which hold its current working variables – Has a stack which contains the execution history • Processes are used to group resources together. • Threads are the entities scheduled for execution on the CPU.
  • 29. 29 Threads … • What threads add to the process model is to allow multiple executions to take place in the same process environment. • Having multiple threads running in parallel in one process is analogous to having multiple processes running in parallel in one computer. – In the former case the threads share an address space, open files, and other resources. – In the later case processes share physical memory, disks, printers and other resources.
  • 30. Threads… • Threads are sometimes called lightweight process. • Multithreading- is to describe the situation of allowing multiple threads in the same process. 30
  • 31. 31 Single and Multithreaded Processes… • A thread is a single sequence of execution within a program • Multithreading refers to multiple threads of control within a single program – each program can run multiple threads of control within it, e.g., Web Browser, MS Words, …
  • 33. Thread Types User Threads • Thread management done by user-level threads library • Examples - POSIX Pthreads - Mach C-threads - Solaris threads Kernel Threads • Supported by the Kernel • Examples -Windows 95/98/NT/2000/XP … - Solaris - Tru64 UNIX - BeOS - Linux 39
  • 35. Many-to- One  Many user-level threads mapped to single kernel thread.  Thread management is done in user space by the thread library. Drawbacks: • 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.
  • 36. One-to- One  Each user-level thread maps to kernel thread.  It allows another thread to run when a thread makes a blocking system call.  It allows multiple threads to run on parallel in multiprocessor system. Drawback  creating user threads requires creating the corresponding kernel threads.  Examples - Windows , OS/2
  • 37. Many-to-Many Model  Allows many user level threads to be mapped to many kernel threads. • developers can create as many user threads as necessary and • the corresponding Kernel threads can run in parallel on a multiprocessor machine. • when a thread performs a blocking system call, the kernel can schedule another thread for execution. • Solaris 2, Windows…
  • 38. 38 CPU Scheduling • Basic Concepts • Scheduling Criteria • Scheduling Algorithms
  • 39. CPU Scheduler: OS • Selects among processes in memory that are ready to be executed, and allocates CPU to one of them. • CPU scheduling decisions may take place when a process: 1.Switches from running to waiting state. 2.Switches from running to ready state. 3.Switches from waiting to ready. 4. Terminates. • Scheduling under 1 and 4 is nonpreemptive. • All other scheduling is preemptive. 39
  • 40. 40 CPU Scheduling Criteria • CPU utilization – keep the CPU as busy as possible • Throughput – # of processes that complete their execution per unit time • Turnaround time – total time required to execute a particular process • Waiting time – amount of time a process waits in the ready queue • Response time – amount of time it takes from when a request was submitted until the first response is produced.
  • 41. 41 CPU Optimization Criteria • Maximize throughput - run as many jobs as possible in a given amount of time. – This could be accomplished easily by running only short jobs or by running jobs without interruptions. • Minimize response time - quickly turn around interactive requests. – This could be done by running only interactive jobs and letting the batch jobs wait until the interactive load ceases. • Minimize turnaround time - move entire jobs in and out of the system quickly. – This could be done by running all batch jobs first (because batch jobs can be grouped to run more efficiently than interactive jobs).
  • 42. 42 CPU Optimization Criteria… • Minimize waiting time - move jobs out of the READY queue as quickly as possible. – This could only be done by reducing the number of users allowed on the system so the CPU would be available immediately whenever a job entered the READY queue. • Maximize CPU efficiency - keep the CPU busy 100 percent of the time. – This could be done by running only CPU-bound jobs (and not I/O- bound jobs). • Ensure fairness for all jobs - give everyone an equal amount of CPU and I/O time. – This could be done by not giving special treatment to any job, regardless of its processing characteristics or priority.
  • 43. 43 Process Scheduling Algorithms • Part of the operating system that makes scheduling decision is called scheduler and the algorithm it uses is called scheduling algorithm • The Process Scheduler relies on a process scheduling algorithm, – based on a specific policy, to allocate the CPU and move jobs through the system. • There are six process scheduling algorithms that have been used extensively.
  • 44. 44 First-Come, First-Served (FCFS) Scheduling Basic Concept • is a non preemptive scheduling algorithm that handles jobs according to their arrival time: • the earlier they arrive, the sooner they’re served. • It’s a very simple algorithm to implement because it uses a FIFO queue. • In a strictly FCFS system there are no WAIT queues (each job is run to completion).
  • 45. First-Come, First-Served (FCFS) Scheduling Process Burst Time (msec) P1 24 P2 3 P3 3 • Suppose that the processes arrive in the order: P1 , P2 , P3 The Gantt Chart for the schedule is: P1 P2 P3 0 24 27 30 • Waiting time for P1 = 0; P2 = 24; P3 = 27 • Average waiting time: (0 + 24 + 27)/3 = 17 • Turnaround time for P1=24, P2=27, P3=30 65
  • 46. FCFS Scheduling (Cont.) • Waiting time for P1 = 6; P2 = 0; P3 = 3 • Average waiting time: (6 + 0 + 3)/3 = 3 • Turnaround time for P1 =30, P2 =3, P3 =6 • Avg turnaround time: (30+3+6)/3= 13 • Much better than previous case. 30 Suppose that the processes arrive in the order P2 , P3 , P1 . • The Gantt chart for the schedule is: P2 P3 P1 0 3 6 66
  • 47. 47 Implementation: FCFS 1. Input the processes along with their burst time (bt). 2. Find waiting time (wt) for all processes. 3. As first process that comes need not to wait so waiting time for process 1 will be 0 i.e. wt[0] = 0. 4. Find waiting time for all other processes i.e. for process i -> wt[i] = bt[i-1] + wt[i-1] . 5. Find turnaround time = waiting_time + burst_time for all processes. 6. Find average waiting time = total_waiting_time / no_of_processes. 7. Similarly, find average turnaround time = total_turn_around_time / no_of_processes.
  • 48. 48 Shortest-Job-First (SJF) Scheduling • Associate with each process the length of its next CPU burst. • Use these lengths to schedule the process with the shortest time. • Two schemes: – nonpreemptive – once CPU given to the process it cannot be preempted until completes its CPU burst. – preemptive – if a new process arrives with CPU burst length less than remaining time of current executing process, preempt. This scheme is know as the Shortest-Remaining- Time-First (SRTF). • SJF is optimal – gives minimum average waiting time for a given set of processes.
  • 49. Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • Waiting time P1=0-0=0, P2=8-2=6, P3=7-4=3, P4=12-5=7 • Average waiting time = (0 + 6 + 3 + 7)/4 = 4 • Turnaround time: P1= 7-0=7, P2=12-2=10, P3=8-4=4, P4=16-5=11 • Avg. turnaround time: (7+10+4+11)/4= 8 Example of Non-Preemptive SJF P3 P2 • SJF (non-preemptive) P1 0 3 P4 7 8 12 16 69
  • 50. Example of Preemptive SJF Process Arrival Time Burst Time P1 0.0 7 P2 2.0 4 P3 4.0 1 P4 5.0 4 • Average waiting time = (9 + 1 + 0 +2)/4 = 3 • Turnaround time:P1=16-0=16, P2=7-2=5, P3=5-4=1, P4=11-5=6 • Avg. turnaround time= (16+5+1+6)/4=7 • SJF (preemptive) P1 P2 0 P4 2 4 5 7 P3 P2 P1 11 16 70
  • 51. 51 Priority Scheduling • A priority number (integer) is associated with each process • The CPU is allocated to the process with the highest priority (smallest integer  highest priority). – Preemptive – nonpreemptive • SJF is a priority scheduling where priority is the predicted next CPU burst time. • Problem  Starvation – low priority processes may never execute. • Solution  Aging – as time progresses increase the priority of the process.
  • 52. 52 Round Robin (RR) • Each process gets a small unit of CPU time (time quantum), usually 10-100 milliseconds. • After this time has elapsed, the process is preempted and added to the end of the ready queue. • If there are n processes in the ready queue and the time quantum is q, then each process gets 1/n of the CPU time in chunks of at most q time units at once. • No process waits more than (n -1)q time units. • Performance – q large  FIFO – q small  q must be large with respect to context switch, otherwise overhead is too high.
  • 53. Example of RR with Time Quantum = 20 Process Burst Time P1 53 P2 17 P3 68 P4 24 • The Gantt chart is: • Avg turnaround time = (134+37+162+121)/4 = 113.5 • Typically, higher average turnaround than SJF, but better response. P1 P2 P3 0 20 37 P4 P1 P3 P4 P1 P3 P3 57 77 97 117 121 134 154 162 73