You are on page 1of 45

Operating Systems

ECEG-5202
PROCESSES
Outline
▪Process concepts
▪Operation on Processes
▪Threads

November 14, 2022 PROCESSES 2


Process Concepts
Definition of a process
◦ A program in execution
◦ An asynchronous activity
◦ The 'animated sprit' of a procedure in execution
◦ The entity to which processors are assigned
◦ The 'dispatchable' unit

No universally agreed upon definition


“A program in execution” is mostly used
Are processes and programs the same?
◦ No.
What is the difference between process and program?
◦ Process is an “active” entity, while a program is a “passive” entity
◦ Program is only part of a process

November 14, 2022 PROCESSES 3


Process Concepts…
Difference between process and program…
◦ Process includes
◦ The program code, also called text section
◦ Current value of program counter
◦ Contents of 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
◦ One program can be several processes
◦ Consider multiple users executing the same program

November 14, 2022 PROCESSES 4


Process Concepts…
Process State
◦ As a process executes, it changes state
◦ New
◦ The process is being created
◦ Running
◦ Instructions are being executed
◦ The process has the CPU
◦ Waiting (Blocked)
◦ The process is waiting for some event to occur
◦ Ready
◦ The process is waiting to be assigned to a CPU
◦ The process has temporarily stopped running to let another process run
◦ Terminated
◦ The process has finished execution

November 14, 2022 PROCESSES 5


Process Concepts…
State diagram

November 14, 2022 PROCESSES 6


Process Concepts…
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 execute next
◦ 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
November 14, 2022 PROCESSES 7
Process Concepts…
Context Switch
◦ Happens when CPU switches from
process to another process
◦ Context of a process is represented
in the PCB
◦ Initiated by a scheduler
◦ Scheduler determines
◦ When a running process is to be
interrupted
◦ Which process from the ready queue will
run next
November 14, 2022 PROCESSES 8
Operation on Processes
• Process creation
• Parent process create children processes, which, in turn
create other processes, forming a tree of processes
• Generally, processes are 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 processes execute concurrently
• Parent waits until children terminate
November 14, 2022 PROCESSES 9
Operation on Processes…
• Process Creation …
• 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

November 14, 2022 PROCESSES 10


Operation on Processes…
C Program creating a child process

fork() forks a new child process that is


a copy of the parent
execlp() replaces the program of the
current process with the named
program
sleep() suspends execution for at least
the specified time
wait() waits for the process to finish
execution

November 14, 2022 PROCESSES 11


Operation on Processes…
• Process Termination
• Process executes last statement and then asks the operating
system to delete it using the exit() system call
• Returns status data from child to parent (via wait())
• Process’ resources are deallocated by operating system
• Parent may terminate the execution of children processes
using the abort() system call. Some reasons for doing so:
• Child has exceeded allocated resources
• Task assigned to child is no longer required
• The parent is exiting and the OS does not allow a child to
continue if its parent terminates

November 14, 2022 PROCESSES 12


Operation on Processes…
• Process Termination …
• Some operating systems do not allow child to exist if its parent has
terminated. If a process terminates, then all its children must also be
terminated
• Cascading termination
• All children, grandchildren, etc. are terminated
• The termination is initiated by the operating system
• The parent process may wait for termination of a child process by using the
wait() system call
• wait() system call passes a parameter that allows the parent to obtain the exit
status of the child
pid = wait(&status);
• Zombie process
• A process that has terminated, but whose parent has not yet called wait()
• Orphan process
• If parent is terminated without invoking wait
• Collected by init()

November 14, 2022 PROCESSES 13


Operation on Processes…
Process Termination …
◦ Reasons for process termination
◦ Normal exist
◦ Most processes terminates because they have done their job
◦ This is usually achieved by calling exit in UNIX
◦ Error exist
◦ When process discovers a fatal error
◦ E.g., a user tries to compile a program that does not exist
◦ Fatal error
◦ An error caused by process due to a bug in program
◦ E.g., executing an illegal instruction, referring non-existing memory or dividing by zero
◦ Killed by another Process
◦ A process executes a system call telling the Operating Systems to terminate some other
process
◦ In UNIX, this is done using kill

November 14, 2022 PROCESSES 14


Threads
• What are threads?
• Thread is a single sequence of stream within a
process
• Sometimes, referred to as lightweight processes
• Has some properties of processes
• Allow multiple executions of streams in a process
• Comprises a threadID, a program counter, a register
set, and a stack
• Threads are not independent of one another like
processes
• Share their code section, data section, OS
resources (e.g., opened files)

November 14, 2022 PROCESSES 15


Threads…
Threads vs Processes
• Similarities
◦ Like processes threads share CPU and only one thread is active (running) at a
time (for one processor)
◦ Like processes, threads within a process execute sequentially
◦ Like processes, threads can create children
◦ And like process, if one thread is blocked, another thread can run

◦ Differences
◦ Unlike processes, threads are not independent of one another
◦ 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
originate from different usersZ

November 14, 2022 PROCESSES 16


Threads…
• Why threads (in OS)?
• A process with multiple threads make a great server
• E.g, printer server, web server
• Resource sharing
• No need to use inter-process communication (i.e., no message passing)
• Scalability
• Can take advantage of multiprocessors
• Economy
• Cheaper than process creation and process switching; and lower overhead in context
switching
• Only need a stack and storage for registers during thread creation
• Do not need address space, global data, program code or OS resources
• Responsiveness
• May allow continued execution if part of process is blocked, especially important for user
interfaces
• Drawback: No protection between threads
November 14, 2022 PROCESSES 17
Threads…
Multicore programming
◦ Recent systems have multiple computing cores on a single chip
=> Multicore or multiprocessors
◦ Concurrency on a single chip
=> interleaved threads
◦ Concurrency on a multicore
=> threads can run in parallel

◦ Parallelism
◦ A system can perform more than one task simultaneously
◦ Concurrency
◦ Supports more than one task that make progress
◦ Single processor/core, scheduler providing concurrency

November 14, 2022 PROCESSES 18


Threads…
Multicore programming
◦ Multicore or multiprocessor systems put pressure on
programmers
◦ Challenges include:
◦ Identify tasks
◦ Find areas that can be divided into separate, concurrent tasks
◦ Balance
◦ Tasks should perform equal work of equal value
◦ Data splitting
◦ Similar to tasks the data also needs to be divided to run on separate cores
◦ Data dependency
◦ When one task depends on data from another, programmers must ensure that the execution of the
task is synchronized to accommodate the data dependency
◦ Testing and debugging
◦ Should take into consideration the different execution paths

November 14, 2022 PROCESSES 19


Threads…
Types of parallelism
◦ Data parallelism
◦ Focuses on distributing subsets of the same data across multiple computing cores
◦ Performs the same operation on each core
◦ Task parallelism
◦ Focuses on distributing tasks across multiple computing cores
◦ Each thread is performing a unique operation
◦ May operate on the same data or on different data
◦ Hybrid

November 14, 2022 PROCESSES 20


Threads…
• Types of threads
• User threads
• Supported above the kernel
• Management is done by user-level thread libraries
• E.g., POSIX Pthreads, Java threads, Windows
threads
• Kernel threads
• Supported and managed by the OS kernel
• Almost all recent OSs support them

November 14, 2022 PROCESSES 21


Threads…
• User-level threads
• OS is not aware of user-level threads
• OS knows and schedules about the process containing the
threads
• Programmer uses a thread library to manage them (create,
delete, synchronize, and schedule them)

November 14, 2022 PROCESSES 22


Threads…
• User-level threads…
• Advantage
• No context switch involved when switching user-level threads
• User-level threads scheduling is more flexible
• A programmer can define a problem dependent thread scheduling
policy
• Each process might use a different scheduling algorithm for its own
threads
• A thread can voluntarily give up the processor by telling the
scheduler it will yield to another threads
• Do not require system calls to create them
• Usually much faster than kernel threads

November 14, 2022 PROCESSES 23


Threads…
• User-level threads…
• Disadvantage
• OS could make poor scheduling decisions
• It might run a process that only has idle threads
• If a user-level thread is waiting for I/O, the entire process
will wait
• Solving this problem requires communication
between the kernel and the user level thread
manager
• OS knows only about the process and, hence, schedules the
process the same way as other processes, regardless of the
number of user threads
• For kernel threads, the more threads a process creates,
the more time slices the OS will dedicate to it
November 14, 2022 PROCESSES 24
Threads…
• Kernel threads
• Also known as a lightweight process
• The kernel manages and schedules threads
• Switching between kernel threads is slightly faster than
switching between processes
• Switching between kernel threads of the same process
requires a small context switch
• The values of registers, program counter, and stack pointer must
be changed
• Memory management information does not need to be changed
since the threads share an address space

November 14, 2022 PROCESSES 25


Threads…
• Kernel threads…
• Advantage
• Kernel has full knowledge of all threads
• Scheduler may decide to give more time to a process
having large number of threads than process having
small number of threads
• Kernel-level threads are especially good for applications that
frequently block
• Disadvantage
• The kernel-level threads are slow and inefficient
• Kernel require a full thread control block (TCB) for each
thread to maintain information about threads.
=> There is significant overhead and increased complexity
in kernel
November 14, 2022 PROCESSES 26
Threads..
• Relationship between user-level threads
and kernel threads
• Three multi-threading models
• Many-to-one model
• Many user-level threads mapped to single
kernel thread
• Thread management is done by the thread
library in user space
• Efficient
• One thread blocking causes all to block
• Multiple threads may not run in parallel on
multicore system because only one can access
the kernel at a time
• Few systems currently use this model
• E.g., Solaris Green Threads

November 14, 2022 PROCESSES 27


Threads…
• One-to-one model
• Each user-level thread maps to kernel thread
• Creating a user-level thread creates a kernel thread
• More concurrency than many-to-one
• Allows another thread to run when a thread makes blocking system call
• Allows multiple threads to run in parallel on multiprocessors
• Disadvantage
• Creating a user thread requires creating a corresponding kernel thread
• Affects the performance of an application
• Most implementations of this model restrict the number of threads
supported by the system
• E.g., Linux and Windows implement this model

November 14, 2022 PROCESSES 28


Threads…
• Many-to-many model
• Multiplexes many user-level threads to a smaller or equal
number of kernel threads
• Addresses the following limitations of many-to-one and one-
to-one models
• No true concurrency, and
• “Limitation” on number of threads
to be created
• Two-level model
• Variation of many-to-many model
• Allows a user-level thread to be bound to a
kernel thread
• Supported in versions older than Solaris 9

November 14, 2022 PROCESSES 29


Threads…
Implicit threading
◦ Recap on challenges of designing multithreaded applications
◦ Identify tasks, balance, data splitting, data dependency, testing and debugging
+ program correctness
◦ How to address this difficulties?
◦ Transfer the creation and management of threads from application developers to compilers and
run-time libraries
=> Implicit threading

November 14, 2022 PROCESSES 30


Threads…
Implicit threading…
◦ Alternative strategies for designing multithreaded programs through implicit
threading
◦ Thread pool
◦ Create a number of threads at process startup and place them into pool
◦ Protects programs from creating unlimited number of threads and exhaust system resources
◦ Service request with an existing thread is faster than waiting to create a service
◦ Separates the task to be performed from the mechanics of creating the task allows us to use
different strategies (e.g., execute task periodically)
◦ Identify parallel regions as blocks of code
◦ Application developers insert compiler directives into their code at parallel regions
◦ E.g., OpenMP, Grand Central Dispatch (GCD)

November 14, 2022 PROCESSES 31


November 14, 2022 PROCESSES 32
Process Scheduling
• Refers to the problem of determining
• When processors should be assigned to a process; and,
• To which process to assign the processor
• Also called CPU scheduling
• Objective
• Maximize CPU utilization
• Scheduler
• Part of an OS that decides which process should use the processor
• Uses scheduling algorithm
• Dispatcher
• Gives control of the CPU to the process selected by the scheduler
• Function involved
• Switching context
• Switching to user mode
• Jumping to the proper location in the newly loaded program
• Dispatch latency: time consumed by the dispatcher
November 14, 2022 PROCESSES 33
Process Scheduling…
Goals of scheduling
◦ Fairness
◦ Each process gets its fair share of the CPU and no process can suffer indefinite postponement
◦ Giving equivalent or equal time is not always fair
◦ E.g., safety control and payroll at a nuclear plant
◦ Policy enforcement
◦ The systems policy must be enforced

◦ Efficiency
◦ CPU should be kept busy for almost 100% of the time

◦ Response time
◦ Response time for interactive users must be minimized

◦ Turnaround
◦ The time batch users must wait for an output should be minimized

◦ Throughput
◦ The number of jobs processed per unit time should be maximized

November 14, 2022 PROCESSES 34


Process Scheduling…
Maintains scheduling queues of processes
Types of queues
◦ Job queue – set of all processes in the system
◦ Ready queue – set of all processes residing in main memory, ready and waiting to
execute
◦ Device queues – set of processes waiting for an I/O device
Processes could move among the various queues

November 14, 2022 PROCESSES 35


Process Scheduling…
Queuing diagram
◦ Represents queues, resources, flows

November 14, 2022 PROCESSES 36


Process Scheduling…
• Schedulers
• Short-term scheduler (or CPU scheduler)
• Selects which process should be executed next and allocates CPU
• Sometimes the only scheduler in a system
• Short-term scheduler is invoked frequently (milliseconds)  (must be fast)
• Long-term scheduler (or job scheduler)
• Selects which processes should be brought into the ready queue
• Long-term scheduler is invoked infrequently (seconds, minutes)  (may be slow)
• The long-term scheduler controls the degree of multiprogramming (# of processes in memory)
• Medium-term scheduler
• Can be added if degree of multiple programming needs to be decreased
• Remove process from memory, store on disk, bring back in from disk to continue execution:
swapping
• Processes can be described as either
• I/O-bound process
• Spends more time doing I/O than computations, many short CPU bursts
• CPU-bound process
• Spends more time doing computations; few very long CPU bursts
• Best performance could be achieved by combining I/O-bound and CPU-bound process
November 14, 2022 PROCESSES 37
Process Scheduling …
Scheduling algorithms
◦ Can be divided into two categories based on how
they deal with clock interrupts
◦ Non-preemptive scheduling
◦ Once a process has been given the CPU, the CPU cannot be taken away from that process
◦ Characteristics
◦ Short jobs are made to wait by longer jobs but the overall treatment of all processes is fair
◦ Response times are more predictable because incoming high priority jobs can not displace waiting jobs
◦ A scheduler executes jobs in the following two situations:
◦ When a process switches from running state to the waiting state
◦ When a process terminates

◦ Preemptive Scheduling
◦ Once a process has been given CPU, the CPU can be taken away

November 14, 2022 PROCESSES 38


Process Scheduling…
Scheduling algorithms…
◦ First come first served
◦ Also called First-In-First-Out (FIFO), Run-to-Completion, Run-until-Done
◦ Simplest scheduling algorithm
◦ Processes are dispatched according to their arrival time on the ready queue
◦ Once process has a CPU, it runs to completion
◦ Advantage
◦ Fair
◦ More predictable
◦ Simple to write and understandable code
◦ Disadvantage
◦ Long jobs make short jobs wait
◦ Unimportant jobs make important jobs wait
◦ Not useful in scheduling interactive users
◦ Average timing is often quite long

November 14, 2022 PROCESSES 39


Process Scheduling…
Scheduling algorithms…
◦ Shortest-Job-First (SJF)
◦ Waiting process with the smallest estimated run-time-to-completion is run next
◦ Appropriate for batch jobs for which the run times are known in advance
◦ Favors short jobs (or processes) at the expense of longer ones
◦ Could be preemptive or non-preemptive
◦ Preemptive occurs when a new process arrives in the ready queue that has a predicted burst time
shorter than the time remaining in the process whose burst is currently on the CPU
◦ Advantage
◦ Gives the minimum average time for a given set of processes
◦ Disadvantage
◦ Requires precise knowledge of how long a job or process will run
◦ Relies on user estimates of run times

November 14, 2022 PROCESSES 40


Process Scheduling…
Scheduling algorithms…
◦ Priority
◦ Each process is assigned a priority and allowed to run according to its priority
◦ Equal-priority processes are scheduled in FCFS order
◦ Could be defined internally or externally
◦ Internal priorities
◦ Time limits, memory requirement, file requirement, CPU vs I/O requirements
◦ External defined priorities are set by criteria that are external to OS
◦ Importance of process, amount of funds being paid for computer use, politics
◦ Can be either preemptive or non-preemptive
◦ Preemptive
◦ When process with higher priority arrives
◦ Problem
◦ Indefinite blocking or starvation
◦ Solution
◦ Aging
◦ Gradually increases the priority of processes that wait in the system for a longer period

November 14, 2022 PROCESSES 41


Process Scheduling…
Scheduling algorithms…
◦ Round robin
◦ Processes are dispatched in a FIFO manner but are given a limited amount of CPU time called a
time-slice or a quantum
◦ The oldest, simplest, fair and most widely used
◦ Preemptive
◦ Effective in time-sharing environment
◦ Guarantees reasonable response time for interactive users
◦ Challenge
◦ Setting the length of the quantum
◦ Too short quantum
◦ Causes too many context switches and lower the CPU efficiency
◦ Too long quantum
◦ Causes poor response time
◦ Average waiting time is often quite long

November 14, 2022 PROCESSES 42


Priority Scheduling…
Scheduling algorithms…
◦ Multilevel queue
◦ Partitions the ready queue in several separate queues
◦ Processes are assigned to a queue permanently based on
◦ Memory size, process priority, process type, …
◦ Each queue has its own scheduling algorithm policy
◦ Could use either preemptive or non-preemptive

◦ Possibility I
◦ If each queue has absolute priority over lower priority queues, wait for all processes in highest priority
queue finishes

◦ Possibility II
◦ If there is a time slice between the queues then each queue gets a certain amount of CPU time, which
it can then schedule among the processes in its queue
◦ Has advantage of low scheduling overhead but its inflexible
◦ Example:
◦ 80% of the CPU time to foreground queue using RR
◦ 20% of the CPU time to background queue using FCFS

November 14, 2022 PROCESSES 43


Priority Scheduling…
Scheduling algorithms…
◦ Multilevel feedback queue
◦ Same as multilevel queue but allows a process to move between queues
◦ If the characteristics of a job changes between CPU-intensive and I/O intensive
◦ If a process waits too long in the lower-priority queue, it may be moved to a higher priority queue
◦ Is the most flexible as it can be tuned for any situation
◦ Complex to implement because of all the adjustable parameters
◦ The number of queues
◦ The scheduling algorithm for each queue
◦ The methods used to upgrade or demote
processes from one queue to another
◦ The method used to determine which queue
a process enters initially

November 14, 2022 PROCESSES 44


Acknowledgment
These slides are adopted from the slides of
Surafel Lemma Abebe (Ph. D.)

Here, I would like to acknowledge and thank him for allowing me to


customize and use the slides for this course.

November 14, 2022 PROCESSES 45

You might also like