0% found this document useful (0 votes)
11 views36 pages

Understanding Operating System Processes

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)
11 views36 pages

Understanding Operating System Processes

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

Lecture 3 –

Processes & Processes


Management
Monday, January 23, 2023
Operating System Concepts
Visual Metaphor of OS
An operating system is like a … toy shop manager.

• Directs operational resources


• Control use of employee time, parts,
tools ….
• Enforces working policies
• Fairness, safety, clean-up
• Mitigates difficulty of complex
tasks
• Simplifies operation and optimizes
performance
Visual Metaphor of OS
An operating system is like a … toy shop manager.

• Directs operational resources • Directs operational resources


• Control use of employee time, • Control use of CPU, memory,
parts, tools …. peripheral devices ….
• Enforces working policies
• Enforces working policies • E.g., fair resource access, limits
• Fairness, safety, clean-up to resource usage….
• Mitigates difficulty of
• Mitigates difficulty of complex complex tasks
tasks • Abstract hardware defaults
• Simplifies operation and optimizes (system calls)
performance
Visual Metaphor of A Process

A process is like an order of toys


• State of execution
• Completed toys, waiting to be
built, etc.
• Parts & Temporary Holding
Area
• Plastic pieces, wooden pieces,
containers
• May require special
hardware
• Sewing machine, glue guns, etc.
Visual Metaphor of A Process

A process is like an order of toys

• State of execution • State of execution


• Program counter, stack pointer • Completed toys, waiting to be
• Parts & Temporary Holding built, etc.
Area • Parts & Temporary Holding
• Data, register state, occupies Area
state in memory • Plastic pieces, wooden pieces,
• May require special containers
hardware • May require special
• I/O devices hardware
• Sewing machine, glue guns, etc.
Process Concept
• An operating system executes a variety of programs that run
as a process.
• Process – a program in execution; process execution must
progress in sequential fashion. No parallel execution of
instructions of a single process
• Multiple parts
• The program code, also called text section
• Current activity including program counter, processor registers
• Stack containing temporary data
• Function parameters, return addresses, local variables
• Data section containing global variables
• Heap containing memory dynamically allocated during run time
Process Concept (Cont.)
• Program is passive entity stored on disk (executable file);
process is active
• Program becomes process when an executable file is loaded
into memory
• Execution of program started via GUI mouse clicks,
command line entry of its name, etc.
• One program can be several processes
• Consider multiple users executing the same program
What is a process?
Main
Memory
OS manages Application →
CPU
hardware on program on disk,
behalf of flash memory ….
applications Disk (static entity)

Main
Memory
Process → state of a
program when CPU
executing loaded in
memory (active Disk
entity)
What is a process?
So, process therefore represents the execution state of
an active application.
Process in Memory
What does a process look like?

Vmax Types of state in a process address space:


• Text and data
• static state when process first loads
• the program code/the executable code
• global variables
• Heap Section
• memory that is dynamically allocated during
program run time (dynamically created
during execution)
• Stack
So, an OS • temporary data storage when invoking
V0 abstraction used to functions (such as function parameters, return
addresses, and local variables)
encapsulates all of
• Grow and shrinks ~ LIFO
the process state is
an address space.
Memory Layout of a C Program
Process Life Cycle
• Processes can be running or idle
SCHEDULER
DISPATCH

READY RUNNING

INTERRUPT

What other states How is that


can a process be in? determined?
Process State
• As a process executes, it changes state
• New: The process is being created
• Running: Instructions are being executed
• Waiting: The process is waiting for some event to occur
• Ready: The process is waiting to be assigned to a processor
• Terminated: The process has finished execution
Process Life Cycle: States
new terminated
admitted exit
interrupt

ready running

scheduler dispatch
I/O event completion I/O or event wait

waiting
Process Life Cycle: States
new terminated
admitted exit
interrupt

ready running

scheduler dispatch
I/O event completion I/O or event wait

waiting
Representation of Process
Scheduling
Process Scheduling
• The objective of multiprogramming is to have some
process running at all times so as to maximize CPU
utilization.
• The objective of time sharing is to switch a CPU core
among processes so frequently that users can interact
with each program while it is running.
• To meet these objectives, the process scheduler selects
an available process (possibly from a set of several
available processes) for program execution on a core.
• Each CPU core can run one process at a time.
Process Scheduling
• For a system with a single CPU core, there will never be
more than one process running at a time, whereas a
multicore system can run multiple processes at one
time.
• If there are more processes than cores, excess processes
will have to wait until a core is free and can be
rescheduled.
• The number of processes currently in memory is known
as the degree of multiprogramming.
Process Scheduling
• Process scheduler (CPU scheduler) selects among available
processes for next execution on CPU core
• Goal -- Maximize CPU use, quickly switch processes onto
CPU core
• Maintains scheduling queues of processes
• Ready queue – set of all processes residing in main memory,
ready and waiting to execute
• Wait queues – set of processes waiting for an event (i.e., I/O)
• Processes migrate among the various queues
Ready and Wait Queues
Role of the CPU Scheduler
A CPU scheduler determines which one of the
currently ready processes will be dispatched to
the CPU to start running, and how long it In order to manage the
should run for. CPU, OS must:
1. Preempt : interrupt
Ready Queue and save current
context (preemption)
… P1 P2 P3 2. Schedule : run

time
CPU
scheduler to choose
next process
3. Dispatch : dispatch
process & switch into
its context
Process Control Block (PCB)
How does the OS know what a process is doing?
• Program Counter
• CPU registers
• Stack pointer

PC

SP
MM CPU
Process Control Block
Process Control Block (PCB)
• PCB created when
process is created

• Certain fields are


updated when process
state changes

• Other fields change too


frequently
Process Control Block (PCB)
Information associated with each process(also called task control block)
• Process state – running, waiting, etc.
• Program counter – location of instruction to next execute
• CPU registers – contents of all process-centric registers
• CPU scheduling information- priorities, scheduling queue pointers
• Memory-management information – memory allocated to the
process
• Accounting information – CPU used, clock time elapsed since start,
time limits
• I/O status information – I/O devices allocated to process, list of
open files
Example of Process Representation
in Linux
Represented by the C structure task_struct

pid t_pid; /* process identifier */


long state; /* state of the process */
unsigned int time_slice /* scheduling information */
struct task_struct *parent; /* this process’s parent */
struct list_head children; /* this process’s children */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space of this process */
Context Switch
• When CPU switches to another process, the system must save
the state of the old process and load the saved state for the
new process via a context switch
• Context of a process represented in the PCB
• Context-switch time is pure overhead; the system does no
useful work while switching
• The more complex the OS and the PCB ➔ the longer the context
switch
• Time dependent on hardware support
• Some hardware provides multiple sets of registers per CPU ➔
multiple contexts loaded at once
CPU Switch From Process to Process
• A context switch
occurs when the
CPU switches
from one process
to another.
Multitasking in Mobile Systems
• Some mobile systems (e.g., early version of iOS) allow only
one process to run, others suspended
• Due to screen real estate, user interface limits iOS provides
for a
• Single foreground process- controlled via user interface
• Multiple background processes– in memory, running, but not on the
display, and with limits
• Limits include single, short task, receiving notification of events,
specific long-running tasks like audio playback
• Android runs foreground and background, with fewer limits
• Background process uses a service to perform tasks
• Service can keep running even if background process is suspended
• Service has no user interface, small memory use
How a PCB is used – Hot Cache Example
• Context Switch = switching
the CPU from the context of
one process to the context
of another
• They are expensive!
Processor Cache
• Direct costs: number of cycles
data P1 for load and store instructions
• Indirect costs: COLD cache!
Cache misses!

PCB_P1
How is a PCB Used?
• Context Switch = switching
the CPU from the context of
one process to the context
of another
• They are expensive!
Processor Cache
• Direct costs: number of cycles
data P1 data P2 for load and store instructions
• Indirect costs: COLD cache!
Cache misses!

PCB_P1
Operations on Processes
• System must provide mechanisms for:
• Process creation
• Process termination
Process Creation
• Parent process create children processes, which, in turn create
other processes, forming a tree of processes
• Generally, process identified and managed via a process
identifier (pid)
• Resource sharing options
• Parent and children share all resources
• Children share subset of parent’s resources
• Parent and child share no resources
• Execution options
• Parent and children execute concurrently
• Parent waits until children terminate
Process Creation (Cont.)
• Address space
• Child duplicate of parent
• Child has a program loaded into it
• UNIX examples
• fork() system call creates new process
• exec() system call used after a fork() to replace the process’
memory space with a new program
• Parent process calls wait()waiting for the child to terminate
A Tree of Processes in Linux
C Program
Forking
Separate
Process

You might also like