Professional Documents
Culture Documents
CS 241
Spring 2021
By
Marwa M. A. Elfattah
Main Reference
Operating System Concepts, Abraham Silbrschatz,
10th Edition
Processes
Process Concept
▪ Process is a program in execution.
Program is passive entity stored on disk (executable file);
process is active
Program becomes process when an executable file is
loaded into memory
Program Process
Process Concept
▪ One program can be
several processes
The same user may
invoke many copies of
an application. Each of
these is a separate
process.
Although the text
sections are equivalent,
the data, heap, and
stack sections vary.
▪ A process can itself
be an execution
environment for other
code, as Java virtual
machine
Process in Memory
▪ The executable code - text section
▪ Data section - global variables
Process in Memory
▪ Stack - temporary data
▪ Function parameters, return addresses, local
variables
Process in Memory
▪ Heap - memory dynamically allocated
during run time
Process in Memory
▪ Text and data sections have fixed size
Process in Memory
▪ The stack and heap sections can shrink
and grow dynamically during program
execution - never overlap
Process in Memory
▪ The stack and heap sections can shrink
and grow dynamically during program
execution - never overlap
Process Control Block (PCB)
Each process is represented in the operating
system by a data structure called PCB(also
called task control block)
▪ Process state – running, waiting, etc.
▪ Process number (PID)
▪ Program counter – location of instruction
to next execute
▪ CPU registers
Alongwith the program counter, this state
information must be saved when an interrupt
occurs
▪ CPU scheduling information- priorities,
scheduling queue pointers
▪ ...
Start Running A New User Process
Kernel must:
▪ Allocate and initialize the process control block.
▪ Allocate memory for the process.
▪ Copy the program from disk into the newly allocated
memory.
▪ Allocate a user-level stack for user-level execution.
▪ Allocate a kernel-level stack for handling system calls,
interrupts and processor exceptions.
▪ Copy arguments into user memory.
• For example, when you click on a file icon, the kernel is asked to
start the application associated with the file, passing it the file name
to open.
▪ Transfer control to user mode.
Multiprogramming
▪ As a single process cannot, in general, keep either
the CPU or the I/O devices busy at all times,
An I/O-bound process: spends more of its time doing I/O
than doing computations.
A CPU-bound process: uses more of its time doing
computations.
▪ One of the most important aspects of an OS is the
ability to run multiple process - multiprogramming,
To increase CPU utilization
Also to keep users satisfied
Multiprogramming
▪ The Idea is:
The operating system keeps several processes in memory
simultaneously.
– The number of processes currently in memory is known
as the degree of multiprogramming.
The operating system picks and begins to execute one of
these processes.
Eventually, the process may have to wait for some task,
such as an I/O operation, to complete.
– In a non-multiprogrammed system, the CPU would sit
idle.
– In a multiprogrammed system, the operating system
simply switches to, and executes, another process.
Multitasking
▪ Multitasking is a logical extension of
multiprogramming.
▪ In multitasking systems, the CPU executes multiple
processes by switching among them, but the
switches occur frequently,
• providing the user with a fast response time.
Process State
▪ As a process executes, it changes state
• New: The process is being created
• Ready: The process is waiting to be assigned to
a processor
• Running: Instructions are being executed
Process State
▪ As a process executes, it changes state
• Waiting: The process is waiting for some event
to occur
• Terminated: The process has finished execution
Process State
▪ Only one process can be running on any processor
core at any instant.
▪ Many processes may be ready and waiting
Process Scheduling
▪ Process scheduler selects among available
processes for next execution on CPU core
▪ Goal:
• Maximize CPU use,
• Quickly switch processes onto CPU core
▪ It 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)
Ready and Wait Queues
Process Scheduling
▪ A new process is initially put in the ready queue. It
• Waits until it is selected for execution, or
dispatched.
Process Scheduling
▪ Once the process is allocated a CPU core and is
executing, one of several events could causing
processes migration among the various queues
Process Scheduling
▪ A process continues this cycle until it terminates,
• It is removed from all queues
• Its PCB and resources are deallocated.
Context Switch
▪ Switching the CPU core to another process is
known context switch
▪ Context of a process represented in the PCB
▪ 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.
Kernel
User
Context Switch
Context Switch
▪ 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
• Also speed depends on memory speed, the number of
registers that must be copied,…
▪ Time dependent on hardware support
• Some hardware provides multiple sets of registers per
CPU ➔ multiple contexts loaded at once
• A context switch here simply requires changing the
pointer to the current register set.
Operations on Processes
▪ Process creation
▪ Process termination
Process Creation
▪ Why create a new process?
• May be ➔ program wants to run an additional instance of
itself.
E.g., web server receives request; creates additional
instance of itself to handle the request; original instance
continues listening for requests
• May be ➔ program wants to run a different program.
E.g., shell receives a command; creates an additional
instance of itself; additional instance overwrites itself
with requested program to handle command; original
instance continues listening for commands
Process Creation
▪ Generally, process identified and managed via a
process identifier (pid)
A unique identifier, which is typically an integer number.
Used as an index to access various attributes of a process
within the kernel.
▪ During the course of execution, a process may
create several new processes.
Parent process create children processes, which, in turn
create other processes, forming a tree of processes.
A parent process may have multiple child processes but a
child process only one parent process.
Parent and children execute concurrently, or parent waits
until children terminate.
EX: A Tree of Processes in Linux