Professional Documents
Culture Documents
Reentrant code is non-self-modifying code; it never changes during execution. Thus, two or more processes can
execute the same code at the same time. Each process has its own copy of registers and data storage to hold the
data for the processs execution. The data for two different processes will, of course, be different.
A solution to the critical-section problem must satisfy the following three requirements:
1. Mutual exclusion. If process Pi is executing in its critical section, then no other processes can be executing in their
critical sections.
2. Progress. If no process is executing in its critical section and some processes wish to enter their critical sections, then
only those processes that are not executing in their remainder sections can participate in the decision on which will enter
its critical section next, and this selection cannot be postponed indefinitely.
3. Bounded waiting. There exists a bound, or limit, on the number of times that other processes are allowed to enter their
critical sections after a process has made a request to enter its critical section and before that request is granted.
Two general approaches are used to handle critical sections in operating systems:
(1) Preemptive kernels and (2) Non-Preemptive kernels.
A preemptive kernel allows a process to be preempted while it is running in kernel mode.
A non-preemptive kernel does not allow a process running in kernel mode to be preempted; a kernel-mode process will
run until it exits kernel mode, blocks, or voluntarily yields control of the CPU.
Semaphores
A semaphore S is an integer variable that, apart from initialization, is accessed only through two standard
atomic operations: wait () and signal ().
Wait(S){
While(S<=0)
; //no op
S--;
}
Signal(S){
S++;
}
Operating systems often distinguish between counting and binary semaphores. The value of a counting semaphore can
range over an unrestricted domain. The value of a binary semaphore can range only between 0 and 1. On some systems,
binary semaphores are known as mutex locks, as they are locks that provide mutual exclusion.
We can use binary semaphores to deal with the critical-section problem for multiple processes. The n processes share a
semaphore, mutex, initialized to 1. Each process P, is organized as shown.
Counting semaphores can be used to control access to a given resource consisting of a finite number of instances. The
semaphore is initialized to the number of resources available. Each process that wishes to use a resource performs a wait()
operation on the semaphore (thereby decrementing the count). When a process releases a resource, it performs a signal()
operation (incrementing the count). When the count for the semaphore goes to 0, all resources are being used. After that,
processes that wish to use a resource will block until the count becomes greater than 0.
Processes
A process is a program in execution.
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 generally also 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 memorythat is dynamically
allocated during process run time.
A program is a passive entity, such as a file containing a list of instructions stored on disk (often called an
executable file), whereas 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.
Process 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.
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.
Schedulers
A process migrates among the various scheduling queues throughout its lifetime. The operating system must
select, for scheduling purposes, processes from these (ready queue, job queue etc.,) queues in some fashion.
The selection process is carried out by the appropriate scheduler.
The long-term scheduler:
The long-term scheduler, or job scheduler, selects processes from the job pool and loads them into
memory for execution.
It determines which programs are admitted to the system for processing. It controls the degree of
multiprogramming (the number of processes in memory). Once admitted, a job becomes a process.
The long-term scheduler may need to be invoked only when a process leaves 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.
It is important that the long-term scheduler select a good process mix of I/O-bound (I/O-bound process is
one that spends more of its time doing I/O than it spends doing computations) and CPU-bound (using
more of its time doing computations) processes.
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.
Limitation:
If all processes are I/O bound, the ready queue will almost always be empty, and the short-term scheduler
will have little to do. If all processes are CPU bound, the I/O waiting queue will almost always be empty,
devices will go unused, and again the system will be unbalanced. The system with the best performance
will thus have a combination of CPU-bound and I/O-bound processes.
The primary distinction between these two schedulers lies in frequency of execution.
The long-term scheduler executes much less frequently;
The short-term scheduler must select a new process for the CPU frequently. Hence short term scheduler
also known as dispatcher.
The long-term scheduler may be absent or minimal in time-sharing systems.
The short-term scheduler is minimal in time-sharing systems.
The medium-term scheduler
The key idea behind a medium-term scheduler is that sometimes it can be advantageous to remove
processes 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. This scheme is 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.
Scheduling Criteria
CPU utilization. We want to keep the CPU as busy as possible.
Throughput. The number of processes that are completed per time unit, called throughput.
Turnaround time. The important criterion is how long it takes to execute that process. The interval from the time of
submission of a process to the time of completion is the turnaround time. Turnaround time is the sum of the periods spent
waiting to get into memory, waiting in the ready queue, executing on the CPU, and doing I/O.
Waiting time. Waiting time is the sum of the periods spent waiting in the ready queue.
Response time. In an interactive system, turnaround time may not be the best criterion. The time from the submission of
a request until the first response is produced. This measure, called response time, is the time it takes to start responding.