Unit 2
Unit 2
• 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
• 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:
• 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.
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.