0% found this document useful (0 votes)
9 views104 pages

Unit 2

The document provides an overview of processes, threads, and CPU scheduling in operating systems. It discusses the structure and states of processes, process control blocks, scheduling queues, and the roles of various types of schedulers. Additionally, it covers inter-process communication models, multithreading, and the benefits and challenges of multicore programming.

Uploaded by

11jaswanth
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)
9 views104 pages

Unit 2

The document provides an overview of processes, threads, and CPU scheduling in operating systems. It discusses the structure and states of processes, process control blocks, scheduling queues, and the roles of various types of schedulers. Additionally, it covers inter-process communication models, multithreading, and the benefits and challenges of multicore programming.

Uploaded by

11jaswanth
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/ 104

Unit 2

Processes: Process concept - Process scheduling - Operations on processes - Interprocess


communication, Threads: Multi core programming - Multithreading models - Threading issues,
CPU Scheduling: Basic concepts - Scheduling criteria - Scheduling algorithms - Thread
scheduling - Multiple processor scheduling - Real time CPU scheduling
Process Concept
• Process is a program in execution, which forms the basis of all computation.
• Early computers allowed only one program to be executed at a time. This
program had complete control of the system and had access to all the system’s
resources.
• Modern computer systems allow multiple programs to be loaded into memory
and executed concurrently.
• A system therefore consists of a collection of processes: operating system
processes executing system code and user processes executing user code.
• Processes can execute concurrently, with the CPU (or CPUs) multiplexed among
them.
• By switching the CPU between processes, the operating system can make the
computer more productive.
Process Structure in Memory
• A process is more than the program code, which is sometimes
known as the text section.
• It also includes the current activity, as represented by the value
of the program counter and the contents of the processor’s
registers.
• A process includes the process stack, which contains temporary
data (such as function parameters, return addresses, and local
variables), and a data section, which contains global variables.
• A process may also include a heap, which is memory that is
dynamically allocated during process run time.
Process – Active Entity
• A program is a passive entity, such as a file containing a list of instructions stored
on disk (often called an executable file).
• In contrast, a process is an active entity, with a program counter specifying the
next instruction to execute and a set of associated resources.
• A program becomes a process when an executable file is loaded into memory.
• A process itself can be an execution environment for other code. The Java
programming environment provides a good example.
• The JVM executes as a process that interprets the loaded Java code and takes
actions (via native machine instructions) on behalf of that code.
java Program
• The command java runs the JVM as an ordinary process, which in turns
executes the Java program Program in the virtual machine.
Process State
• As a process executes, it changes state. The state of a process is defined in
part by the current activity of that process.
• New. The process is being created.
• Running. Instructions are being executed.
• Waiting. The process is waiting for some event to occur (such as an I/O
completion or reception of a signal).
• Ready. The process is waiting to be assigned to a processor.
• Terminated. The process has finished execution.
• Only one process can be running on any processor at any instant. Many
processes may be ready and waiting.
States of Process
Process Control Block
• Each process is represented in the operating system by a process control block
(PCB)—also called a task control block.
• Process state. The state may be new, ready, running, waiting, halted, and so on.
• Program counter. The counter indicates the address of the next instruction to be
executed for this process.
• CPU registers. The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-
purpose registers, plus any condition-code information. Along with the program
counter, this state information must be saved when an interrupt occurs, to allow the
process to be continued correctly afterward
• CPU-scheduling information. This information includes a process priority, pointers
to scheduling queues, and any other scheduling parameters.
Process Control Block (Cont..)

• Memory-management information. This information may include such


items as the value of the base and limit registers and the page tables, or the
segment tables, depending on the memory system used by the operating
system

• Accounting information. This information includes the amount of CPU


and real time used, time limits, account numbers, job or process numbers,
and so on.

• I/O status information. This information includes the list of I/O devices
allocated to the process, a list of open files, and so on.
Diagram showing CPU switch from process to
process
Threads
• Most modern operating systems have extended the process concept to allow a process to have
multiple threads of execution and thus to perform more than one task at a time.
• This feature is especially beneficial on multicore systems, where multiple threads can run in
parallel.
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 */
Process Scheduling
• The objective of multiprogramming is to have some process running at all
times, to maximize CPU utilization.
• The objective of time sharing is to switch the CPU 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 the CPU.
• For a single-processor system, there will never be more than one running
process.
Scheduling Queues
• As processes enter the system, they are put into a job queue, which consists of all processes in the
system.
• The processes that are residing in main memory and are ready and waiting to execute are kept on a
list called the ready queue.
• This queue is generally stored as a linked list.
• The list of processes waiting for a particular I/O device is called a device queue
Queueing-diagram representation of process
scheduling
• Each rectangular box represents a
queue.
• Two types of queues are present: the
ready queue and a set of device queues.
• The circles represent the resources that
serve the queues, and the arrows
indicate the flow of processes in the
system
• New process is initially put in the
ready queue. It waits there until it is
selected for execution, or dispatched
Events occurring after process is allocated the
CPU and executing
✓The process could issue an I/O request and then be placed in an
I/O queue.
✓The process could create a new child process and wait for the
child’s termination.
✓ The process could be removed forcibly from the CPU, due to
an interrupt, and be put back in the ready queue.

A process continues this cycle until it terminates, at which time it


is removed from all queues and has its PCB and resources
deallocated.
Schedulers
• A process migrates among the various scheduling queues throughout its
lifetime.
• The operating system must select, for scheduling purposes, processes from
these queues in some fashion.
• The selection process is carried out by the appropriate scheduler.
• These processes are spooled to a mass-storage device (typically a disk),
where they are kept for later execution.
• The long-term scheduler, or job scheduler, selects processes from this
pool and loads them into memory for execution.
• The short-term scheduler, or CPU scheduler, selects from among the
processes that are ready to execute and allocates the CPU to one of them.
Short-term and Long-term Schedulers
• The short-term scheduler must select a new process for the CPU frequently. A
process may execute for only a few milliseconds before waiting for an I/O
request.
• The long-term scheduler controls the degree of multiprogramming (the
number of processes in memory).
• If the degree of multiprogramming is stable, then the average rate of process
creation must be equal to the average departure rate of processes leaving the
system.
• Because of the longer interval between executions, the long-term scheduler can
afford to take more time to decide which process should be selected for
execution.
Selection of Process by schedulers
• An I/O-bound process is one that spends more of its time doing I/O than it
spends doing computations.
• A CPU-bound process, in contrast, generates I/O requests infrequently,
using more of its time doing computations.
• It is important that the long-term scheduler select a good process mix of
I/O-boundand CPU-bound processes.
• For example, time-sharing systems such as UNIX and Microsoft Windows
systems often have no long-term scheduler but simply put every new
process in memory for the short-term scheduler.
Medium-term scheduler
• It can remove a process from memory and
thus reduce the degree of
multiprogramming.
• Later, the process can be reintroduced into
memory, and its execution can be
continued where it left off called swapping.
• The process is swapped out, and is later
swapped in, by the medium-term
scheduler.
• Swapping may be necessary to improve the
process mix or because a change in
memory requirements has overcommitted
available memory, requiring memory to be
freed up.
Context Switch
• Switching the CPU to another process requires performing a state
save of the current process and a state restore of a different
process. This task is known as a context switch.
• When a context switch occurs, the kernel saves the context of the
old process in its PCB and loads the saved context of the new
process scheduled to run.
• Context-switch time is pure overhead, because the system does
no useful work.
• Some hardware provides multiple sets of registers per CPU where
multiple contexts loaded at once.
Operations on processes
• During execution, a process may create several new processes.
• The creating process is called a parent process, and the new processes are
called the children of that process.
• Each of these new processes may in turn create other processes, forming a
tree of processes.
• Most operating systems (including UNIX, Linux, and Windows) identify
processes according to a unique process identifier (or pid), which is an
integer number.
• pid is used as an index to access various attributes of a process within the
kernel
A Tree of Processes in Linux
Operations on Processes
• On UNIX and Linux systems, we can obtain a listing of processes by using
the ps command. For example, the command
ps –el
• 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
• 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
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 operating systems does not allow a child to
continue if its parent terminates
Zombie and orphan process
• Some operating systems do not allow child to exists 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. The call returns status information and the pid
of the terminated process
pid = wait(&status);
• If no parent waiting (did not invoke wait()) process is a zombie
• If parent terminated without invoking wait(), process is an orphan
Inter-process Communication (IPC)
• A process is independent if it cannot affect or be
affected by the other processes executing in the
system.
• Any process that does not share data with any other
process is independent.
• A process is cooperating if it can affect or be affected
by the other processes executing in the system.
• Clearly, any process that shares data with other
processes is a cooperating process.
Models for IPC (a) Shared memory. (b) Message passing.

• Two models of IPC


• Shared memory
• Message passing
Message Passing vs Shared Memory Models
• Message passing is useful for exchanging smaller amounts of data, because no
conflicts need be avoided.

• Shared memory can be faster than message passing, since message-passing


systems are typically implemented using system calls and thus require the more
time-consuming task of kernel intervention.

• Once shared memory is established, all accesses are treated as routine memory
accesses, and no assistance from the kernel is required.

• Shared memory suffers from cache coherency issues, which arise because shared
data migrate among the several caches
Shared Memory Systems
• The processes are also responsible for ensuring that they are not writing to
the same location simultaneously.
• Producer–consumer problem: Common paradigm for cooperating
processes.
• A producer process produces information that is consumed by a consumer
process.
• The unbounded buffer places no practical limit on the size of the buffer.
The consumer may have to wait for new items, but the producer can
always produce new items.
• The bounded buffer assumes a fixed buffer size. In this case, the
consumer must wait if the buffer is empty, and the producer must wait if
the buffer is full.
Message-Passing Systems
• Message passing provides a mechanism to allow processes to
communicate and to synchronize their actions without sharing the same
address space.
• If processes P and Q want to communicate, they must send messages to
and receive messages from each other: a communication link must exist
between them.
• Message passing may be either blocking or nonblocking— also known as
synchronous and asynchronous.
➢ Blocking send. The sending process is blocked until the message is received by the receiving
process or by the mailbox.
➢ Nonblocking send. The sending process sends the message and resumes operation.
➢ Blocking receive. The receiver blocks until a message is available.
➢ Nonblocking receive. The receiver retrieves either a valid message or a null.
Threads
• A thread is a basic unit of CPU utilization; it comprises a thread ID, a
program counter, a register set, and a stack. It shares with other threads
belonging to the same process its code section, data section, and other
operating-system resources, such as open files and signals.
• An application typically is implemented as a separate process with several
threads of control.
• A web browser might have one thread display images or text while
another thread retrieves data from the network, for example
Single-threaded and multithreaded processes
Multithreaded Server Architecture
Benefits of Multithreaded Architecture
• Responsiveness – may allow continued execution if part of
process is blocked, especially important for user interfaces
• Resource Sharing – threads share resources of process, easier
than shared memory or message passing
• Economy – cheaper than process creation, thread switching
lower overhead than context switching
• Scalability – process can take advantage of multicore
architectures
Multi core programming
• Each core appears as a separate processor to the operating system, whether
the cores appear across CPU chips or within CPU chips, we call these
systems multicore or multiprocessor systems.
• Multithreaded programming provides a mechanism for more efficient use of
these multiple computing cores and improved concurrency.
• A system is parallel if it can perform more than one task simultaneously.
• In contrast, a concurrent system supports more than one task by allowing all
the tasks to make progress. Thus, it is possible to have concurrency without
parallelism.
Parallel and Concurrent Execution
▪ Concurrent execution on single-core system:

▪ Parallelism on a multi-core system:


Multi core programming -Challenges
• Dividing activities
• Balance
• Data splitting
• Data dependency
• Testing and debugging
Types of Parallelism

Data parallelism involves the


distribution of data
across multiple cores and task
parallelism on the distribution
of tasks across
multiple cores.
Multithreading models
• User threads - management done
by user-level threads library
• Three primary thread libraries:
• POSIX Pthreads
• Windows threads
• Java threads
• Kernel threads - Supported by the
Kernel
Multithreading Models
• Many-to-One
• One-to-One
• Many-to-Many
Many-to-One Model
• Many user-level threads mapped to single kernel thread
• One thread blocking causes all to block
• Multiple threads may not run in parallel on multicore system
because only one may be in kernel at a time
• Few systems currently use this model
• Examples:
• Solaris Green Threads
• GNU Portable 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
• Number of threads per process sometimes restricted due to
overhead
• Examples
• Windows
• Linux
Many-to-Many Model
• Allows many user level threads to be mapped to many kernel threads
• One variation on the many-to-many model still multiplexes many
user level threads to a smaller or equal number of kernel threads but
also allows a user-level thread to be bound to a kernel thread. This
variation is referred to as the two-level model
Threading issues
fork() System Call

• Negative Value: The creation of a child process was


unsuccessful.

• Zero: Returned to the newly created child process.

• Positive value: Returned to parent or caller. The value contains


the process ID of the newly created child process.
fork()
• A process p executes the following code
fork()
fork()
fork()
How many child process are created
2^(3) -1=7

No of parent process = 2^(3) =8


Exec()
When the exec system call is invoked, the following steps occur:
• The kernel checks if the specified program exists and is
executable.
• The kernel allocates a new process image and initializes it with
the new program's code and data.
• The kernel sets up the new program's stack and passes the
command-line arguments and environment variables to the new
program.
• The kernel transfers control to the new program's entry point, and
the new program starts executing.
Signal handling
• A signal is used in UNIX systems to notify a process that a particular event
has occurred. A signal received either synchronously or asynchronously
has the following pattern.
1. A signal is generated by the occurrence of a particular event.
2. The signal is delivered to a process.
3. Once delivered signal should be handled
• A signal may be handled by one of two possible handlers:
• Default signal handler
• User-defined signal handler
• Every signal has a default signal handler that the kernel runs when handling that signal.
• This default action can be overridden by a user-defined signal handler that is called to
handle the signal.
Signal Handling options for multithreaded
programs
1. Deliver the signal to the thread to which the signal applies.
2. Deliver the signal to every thread in the process.
3. Deliver the signal to certain threads in the process.
4. Assign a specific thread to receive all signals for the process
Signals in UNIX and Windows
• The standard UNIX function for delivering a signal is kill(pid t pid, int
signal)
• This function specifies the process (pid) to which a particular signal (signal) is to
be delivered.
• POSIX Pthreads provides the following function, which allows a signal to be
delivered to a specified thread (tid): pthread kill(pthread t tid, int
signal).
• Windows does not explicitly provide support for signals, it allows us to emulate
them using asynchronous procedure calls (APCs).
• The APC facility enables a user thread to specify a function that is to be called
when the user thread receives notification of a particular event. APC is delivered to
a particular thread rather than a process.
Thread Cancellation
• Thread cancellation involves terminating a thread before it has completed.
• A thread that is to be canceled is often referred to as the target thread.
Cancellation of a target thread may occur in two different scenarios:
1. Asynchronous cancellation. One thread immediately terminates the target
thread.
2. Deferred cancellation. The target thread periodically checks whether it should
terminate, allowing it an opportunity to terminate itself in an orderly fashion.
• The operating system will reclaim system resources from a canceled thread but
will not reclaim all resources.
• Therefore, canceling a thread asynchronously may not free a necessary system-
wide resource.
Thread Cancellation (Cont..)
• With deferred cancellation, in contrast, one thread indicates that a target
thread is to be canceled, but cancellation occurs only after the target thread
has checked a flag to determine whether or not it should be canceled.
• In Pthreads, thread cancellation is initiated using the pthread cancel()
function.
Modes for Signal Handling

• The default cancellation type is deferred cancellation. Here, cancellation


occurs only when a thread reaches a cancellation point.
• One technique for establishing a cancellation point is to invoke the pthread
testcancel()function.
• If a cancellation request is found to be pending, a function known as a
cleanup handler is invoked.
Thread-Local Storage
• Threads belonging to a process share the data of the
process.
• In some circumstances, each thread might need its own
copy of certain data. We will call such data thread-local
storage (or TLS.)
• TLS data are visible across function invocations. In
some ways, TLS is similar to static data.
Scheduler Activations
• Multithreaded programs concerns communication between the kernel
and the thread library, which may be required by the many-to-many and
two-level models discussed.
• Many systems implementing either the many-to-many or the two-level
model place an intermediate data structure between the user and kernel
threads. This data structure—typically known as a lightweight process,
or LWP
Scheduler Activation (Cont..)
• One scheme for communication between the user-thread library and the kernel is
known as scheduler activation.
• The kernel provides an application with a set of virtual processors (LWPs), and
the application can schedule user threads onto an available virtual processor.
• The kernel must inform an application about certain events. This procedure is
known as an upcall.
• Upcalls are handled by the thread library with an upcall handler.
• Upcall handlers must run on a virtual processor.
Example Scenario
• The kernel makes an upcall to the application informing it that a thread is about
to block and identifying the specific thread.
CPU Scheduling:Basic Concepts
• Maximum CPU utilization obtained with
multiprogramming
• CPU–I/O Burst Cycle – Process execution
consists of a cycle of CPU execution and
I/O wait
• CPU burst followed by I/O burst
• CPU burst distribution is of main concern
Histogram of CPU-burst Duration
• Large number of short bursts

• Small number of longer bursts


CPU Scheduler
• Whenever the CPU becomes idle, the operating system must select one of
the processes in the ready queue to be executed. The selection process is
carried out by the short-term scheduler, or CPU scheduler.
• A ready queue can be implemented as a FIFO queue, a priority queue, a
tree, or simply an unordered linked list.
• All the processes in the ready queue are lined up waiting for a chance to
run on the CPU.
• The records in the queues are generally process control blocks (PCBs) of
the processes.
CPU Scheduler (Cont..)
• The CPU scheduler selects from among the processes in ready
queue, and allocates a CPU core 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
• For situations 1 and 4, there is no choice in terms of scheduling. A
new process (if one exists in the ready queue) must be selected
for execution.
• For situations 2 and 3, however, there is a choice.
Preemptive and nonpreemptive Scheduling
• When scheduling takes place only under circumstances 1 and 4,
the scheduling scheme is nonpreemptive.
• Otherwise, it is preemptive.
• Under nonpreemptive scheduling, once the CPU has been allocated
to a process, the process keeps the CPU until it releases it either by
terminating or by switching to the waiting state.
• Virtually all modern operating systems including Windows, MacOS,
Linux, and UNIX use preemptive scheduling algorithms.
Dispatcher
• Dispatcher module gives control of the
CPU to the process selected by the
CPU scheduler; this involves:
• Switching context
• Switching to user mode
• Jumping to the proper location in the user
program to restart that program
• Dispatch latency – time it takes for the
dispatcher to stop one process and
start another running
Scheduling Criteria
• CPU utilization – keep the CPU as busy as possible
• Throughput – Number of processes that complete their execution
per time unit
• Turnaround time – amount of time to execute a particular
process
• Waiting time – amount of time a process has been waiting in the
ready queue
• Response time – amount of time it takes from when a request
was submitted until the first response is produced
Scheduling Algorithm Optimization Criteria
• Max CPU utilization
• Max throughput
• Min turnaround time
• Min waiting time
• Min response time
Scheduling Algorithms
• CPU scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU. Some of
them are as follows
• First-Come, First-Served Scheduling (FCFS)
• Shortest-Job-First Scheduling (SJF)
• Priority Scheduling
• Round-Robin Scheduling
• Multilevel Queue Scheduling
• Multilevel Feedback Queue Scheduling
Shortest Job First (SJF)
• Associate with each process the length of its next CPU burst
• Use these lengths to schedule the process with the shortest time
• SJF is optimal – gives minimum average waiting time for a given set
of processes
• Preemptive version called shortest-remaining-time-first
Round Robin Scheduling
• Each process gets a small unit of CPU time (time quantum q), 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.
• Timer interrupts every quantum to schedule next process
• Performance
• Time quantum large  FCFS
• Time quantum small  RR
• Note that q must be large with respect to context switch, otherwise
overhead is too high
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

• Problem  Starvation – low priority processes may never execute


• Solution  Aging – as time progresses increase the priority of the
process
Multi-level Queue Scheduling
• The ready queue consists of multiple queues
• Multilevel queue scheduler defined by the following parameters:
• Number of queues
• Scheduling algorithms for each queue
• Method used to determine which queue a process will enter when that process
needs service
• Scheduling among the queues
• A multilevel queue scheduling algorithm partitions the ready queue
into several separate queues The processes are permanently assigned
to one queue, generally based on some property of the process, such
as memory size, process priority, or process type.
Multi-levels queues with priority
Multilevel Feedback Queue Scheduling
• The multilevel feedback queue scheduling algorithm, in contrast,
allows a process to move between queues.
• The idea is to separate processes according to the characteristics of
their CPU bursts.
• If a process uses too much CPU time, it will be moved to a lower-
priority queue.
• Multilevel-feedback-queue scheduler defined by the following
parameters:
• Number of queues
• Scheduling algorithms for each queue
• Method used to determine when to upgrade a process
• Method used to determine when to demote a process
• Method used to determine which queue a process will enter when that process
needs service
Example of Multilevel Feedback Queue
• Three queues:
• Q0 – RR with time quantum 8 milliseconds
• Q1 – RR time quantum 16 milliseconds
• Q2 – FCFS

• Scheduling
• A new process enters queue Q0 which is served in RR
• When it gains CPU, the process receives 8 milliseconds
• If it does not finish in 8 milliseconds, the process is moved to queue Q1
• At Q1 job is again served in RR and receives 16 additional milliseconds
• If it still does not complete, it is preempted and moved to queue Q2
Thread scheduling
• An operating systems that support threads, the kernel-level threads are
being scheduled by the operating system.
• User-level threads are managed by a thread library, and the kernel is
unaware of them.
• To run on a CPU, user-level threads must ultimately be mapped to an
associated kernel-level thread, although this mapping may be indirect
and may use a lightweight process (LWP).
Processor and System Contention Scope
• On systems implementing the many-to-one and many-to-many threading
models, the thread library schedules user-level threads to run on an
available Light Weight Process. This scheme is known as process
contention scope (PCS), since competition for the CPU takes place among
threads belonging to the same process.
• Thread library schedules user threads onto available LWPs, we do not
mean that the threads are running on a CPU.
• That would require the operating system to schedule the kernel thread
onto a physical CPU.
• To decide which kernel-level thread to schedule onto a CPU, the kernel
uses system-contention scope (SCS).
Processor Contention Scope - Implementation
• PCS is done according to priority—the scheduler selects the
runnable thread with the highest priority to run.

• It is important to note that PCS will typically preempt the


thread currently running in favor of a higher-priority thread;
however, there is no guarantee of time slicing among threads of
equal priority.
Multiple-Processor Scheduling
• If multiple CPUs are available, load sharing becomes possible—but scheduling
problems become correspondingly more complex.
• Even with homogeneous multiprocessors, there are limitations on scheduling
• A system with an I/O device is attached to a private bus of one processor.
Processes that wish to use that device must be scheduled to run on that
processor.
Approaches to Multiple-Processor Scheduling
• One approach to CPU scheduling in a multiprocessor system has all
scheduling decisions, I/O processing, and other system activities handled
by a single processor—the master server. The other processors execute
only user code.
• This asymmetric multiprocessing is simple because only one processor
accesses the system data structures, reducing the need for data sharing.
• A second approach uses symmetric multiprocessing (SMP), where each
processor is self-scheduling. All processes may be in a common ready
queue, or each processor may have its own private queue of ready
processes.
Processor Affinity
• When the process migrates from one processor to another processor, the
contents of cache memory must be invalidated for the first processor, and
the cache for the second processor must be repopulated.

• Because of the high cost of invalidating and repopulating caches, most


SMP systems try to avoid migration of processes from one processor to
another and instead attempt to keep a process running on the same
processor. This is known as processor affinity.
Types of Affinity
• Hard affinity: A strong association between a process a specific
CPU, which ensures that the process will only run on that CPU.

• Soft affinity: A preference for a process to run on a specific CPU,


but not a strict requirement, which allows the system to schedule
the process on another CPU if needed.

• Many systems provide both soft and hard affinity


Non-Uniform Memory Access (NUMA)

If the operating system’s CPU


scheduler and memory-placement
algorithms work together, then a
process that is assigned affinity to a
particular CPU can be allocated
memory on the board where that
CPU resides
Load Balancing
• Load balancing attempts to keep the workload evenly distributed across
all processors in an SMP system.
• It is important to note that load balancing is typically necessary only on
systems where each processor has its own private queue of eligible
processes to execute.
• On systems with a common run queue, load balancing is often
unnecessary, because once a processor becomes idle, it immediately
extracts a runnable process from the common run queue.
Load Balancing Implementation
Multicore Processors
• Multiple processor cores on the same physical chip, resulting in a
multicore processor.
• Multicore processors may complicate scheduling issues.
• When a processor accesses memory, it spends a significant amount of time
waiting for the data to become available. This situation, known as a
memory stall, may occur for various reasons, such as a cache miss
(accessing data that are not in cache memory).
Solution to memory stall
• Dual threaded Processor core
Two ways to multithread a processing core
Fine-Grained Multi-Threading
Fine-grained multi-threading refers to a threading model where
each thread executes a small, specific task or a short sequence of
instructions. This approach is often used in applications that
require low-latency and high-throughput.
Coarse-Grained Multi-Threading
Coarse-grained multi-threading refers to a threading model where
each thread executes a larger, more complex task or a longer
sequence of instructions. This approach is often used in
applications that require high-throughput and concurrency
Applications of Fine-Grained Multithreading
Applications of Coarse-Grained Multithreading
Real time CPU scheduling
• CPU scheduling for real-time operating systems involves special
issues.
• Soft real-time systems provide no guarantee as to when a critical
real-time process will be scheduled.
• Hard real-time systems have stricter requirements. A task must
be serviced by its deadline; service after the deadline has expired
is the same as no service at all.
Event latency
• Event latency as the amount of time that elapses from
when an event occurs to when it is serviced.

• Different events have different latency requirements.


• For example, the latency requirement for an antilock
brake system might be 3 to 5 milliseconds.

That is, from the time a wheel first detects that it is sliding,
the system controlling the antilock brakes has 3 to 5
milliseconds to respond to and control.
Types of latencies affect the performance
of real-time systems
• Interrupt latency refers to the period of time from the arrival of an
interrupt at the CPU to the start of the routine that services the interrupt.
• When an interrupt occurs, the operating system must first complete the
instruction it is executing and determine the type of interrupt that
occurred.
• It must then save the state of the current process before servicing the
interrupt using the specific interrupt service routine (ISR). The total time
required to perform these tasks is the interrupt latency.
Interrupt Latency
Dispatch latency
• The amount of time required for the scheduling dispatcher to stop one process and start another is
known as dispatch latency. The most effective technique for keeping dispatch latency low is to
provide preemptive kernels.
• The conflict phase of dispatch latency has two components:
1. Preemption of any process running in the kernel.
2. Release by low-priority processes of resources needed by a high-priority process
Priority-Based Scheduling
• A preemptive, priority-based scheduler only guarantees soft real-time
functionality.
• Hard real-time systems must further guarantee that real-time tasks will be
serviced in accord with their deadline requirements, and making such
guarantees requires additional scheduling features.
• Some processes in a real-time system are considered periodic. That is, they
require the CPU at constant intervals (periods).
• Once a periodic process has acquired the CPU, it has a fixed processing time
t, a deadline d by which it must be serviced by the CPU, and a period p.
• The relationship of the processing time, the deadline, and the period can be
expressed as 0 ≤ t ≤ d ≤ p.
Periodic Task
• The rate of a periodic task is 1/p.
• Figure illustrates the execution of a periodic process over time.
• Schedulers can take advantage of these characteristics and assign
priorities according to a process’s deadline or rate requirements.
Admission Control Algorithm
• A process may have to announce its deadline requirements to the
scheduler.
• Using a technique known as an admission-control algorithm, the
scheduler does one of two things.
1. It either admits the process, guaranteeing that the process will complete
on time,
2. or rejects the request as impossible if it cannot guarantee that the task will
be serviced by its deadline.
Rate-Monotonic Scheduling
• The rate-monotonic scheduling algorithm schedules periodic tasks using a static priority
policy with preemption.
• If a lower-priority process is running and a higher-priority process becomes available to
run, it will preempt the lower-priority process.
• Each periodic task is assigned a priority. The shorter the period, the higher the priority;
the longer the period, the lower the priority.
• The rationale behind this policy is to assign a higher priority to tasks that require the CPU
more often.
• Assumes that the processing time of a periodic process is the same for each CPU burst.
Example of Rate-Monotonic Scheduling
• We have two processes, P1 and P2. The periods for P1 and P2 are 50 and 100, respectively—that
is, p1 = 50 and p2 = 100. The processing times are t1 = 20 for P1 and t2 = 35 for P2. The deadline for
each process requires that it complete its CPU burst by the start of its next period.

• CPU utilization of a process Pi as the ratio of its burst to its period—ti/pi


• the CPU utilization of P1 is 20/50 = 0.40 and that of P2 is 35/100 = 0.35, for a total CPU utilization of
75 percent.
Now suppose we use rate-monotonic scheduling, in which we assign P1
a higher priority than P2 because the period of P1 is shorter than that of P2.
P2 completes its CPU burst at time 75, also meeting its first deadline. The system is idle until time 100, when
P1 is scheduled again.
Earliest-Deadline-First Scheduling
• Earliest-deadline-first (EDF) scheduling dynamically assigns priorities
according to deadline.
• The earlier the deadline, the higher the priority; the later the deadline, the
lower the priority.
• Under the EDF policy, when a process becomes runnable, it must
announce its deadline requirements to the system.
• Priorities may have to be adjusted to reflect the deadline of the newly
runnable process
Proportional Share Scheduling
• Proportional share schedulers operate by allocating T shares among all applications.
• An application can receive N shares of time, thus ensuring that the application will have
N/T of the total processor time.
• Assume that a total of T = 100 shares is to be divided among three processes, A, B, and C.
• A =50 shares,
• B =15 shares,
• and C is assigned 20 shares.
• This scheme ensures that A will have 50 percent of total processor time, B will have 15
percent, and C will have 20 percent.
• Proportional share schedulers must work in conjunction with an admission-control policy
to guarantee that an application receives its allocated shares of time.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy