Unit 2 Os
Unit 2 Os
There are also two address-space possibilities for the new process:
1. The child process is a duplicate of the parent process (it has the same
program and data as the parent).
2. The child process has a new program loaded into it.
Operations on Process
1. Process Creation:
• A new process is created by the fork() system call. The new process consists of
a copy of the address space of the original process.
• This mechanism allows the parent process to communicate easily with its
child process.
• Both processes (the parent and the child) continue execution at the
instruction after the fork(), with one difference: the return code for the fork()
is zero for the new (child) process, whereas the (nonzero) process identifier of
the child is returned to the parent.
• After a fork() system call, one of the two processes typically uses the exec()
system call to replace the process’s memory space with a new program. The
exec() system call loads a binary file into memory (destroying the memory
image of the program containing the exec() system call)
Operations on Process
Operations on Process
2. Process Termination
• A process terminates when it finishes executing its final statement and asks the
operating system to delete it by using the exit() system call.
• At that point, the process may return a status value (typically an integer) to its waiting
parent process (via the wait() system call). All the resources of the process —including
physical and virtual memory, open files, and I/O buffers—are deallocated and reclaimed
by the operating system.
• A parent may terminate the execution of one of its children for a variety of reasons,
such as these:
i. The child has exceeded its usage of some of the resources that it has been allocated.
(To determine whether this has occurred, the parent must have a mechanism to
inspect the state of its children.)
ii. The task assigned to the child is no longer required.
iii. The parent is exiting, and the operating system does not allow a child to continue if
its parent terminates.
Operations on Process
3. Round Robin(RR) –
Process, burst time, time quantum.
4. Priority Scheduling –
Process, burst time, priority
Scheduling Algorithms
5. Multilevel queue scheduling 6. Multilevel feedback queue
Multi thread models
Many-to-One One-to-one Many-to-many
• Maps many user level threads to • Allows multiple threads to run • Many user level threads are
one kernel level thread. parallel on multiprocessor. mapped to small or equal
number of kernel threads
• Thread management is done by • Drawback: creating a user depending of specific
the thread library in user space. thread require creating application and machine the
However, entire process will corresponding kernel thread. number of kernel threads differ.
block if a thread makes a Large number of kernel thread
blocking system call. may burden the whole system. • Two level model – variant of
many- to-many model.
• Only one thread can access • Application: Linux along with
kernel at a time. family of windows OS • one user thread is mapped to
implement one to one model. small or equal number of kernel
thread also allows user level
threads to bound to kernel
threads
Multi thread models
Many-to-one One-to-one Many-to-many
• Application: Green thread • Concurrency: As many user • Applications: API for creating
library for solarisis systems and threads and kernel threads may and managing threads, POSIX,
adopted in early versions of run if there is a blocking system Pthreads, windows, java.
java. call, the kernel can schedule
another thread of execution. • Concurrency: As many user
• Concurrency: Allows users to threads and kernel threads may
develop as many user threads as run if there is a blocking system
they wish if it does not result in call, the kernel can schedule
parallelism because kernel can another thread of execution.
schedule only one thread at a
time. • Many-to-many is not practical
but looks flexible. Limiting
number of kernel threads less
important cause of increasing
number of process cores.
Threading Issues
1. The fork() and exec() system call:
Fork() process either duplicates all threads or duplicates only the thread that initiates fork()..
Version of fork() depends on application that executes next.
Exec() program specified in parameter will replace entire process including all threads.
Calling exec() after fork() then duplicates all the threads is unnecessary duplication only the
calling thread is appropriate.
2. Signal handling:
Signal in UNIX system to notify a process that a particular event has occurred. Signal can be
synchronized or asynchronous follows same pattern:
• Signal is generated by occurance of particular events.
• Signal is delivered to a process
• once delivered signal must be handled
Threading Issues
Syncronous signals: if signals are delivered to the same process that
creates it then it is synchronous signal. Example illegal memory access,
divided by zero.
Asyncronous signals: generated by event external to running process.
Example terminating process using ctrl +c and having timer expire.
A signal is handled by one of 2 possible handlers:
• A default signal handler
• A user defined signal handler
Every signal have default signal handler overwritten by default signal
handler over written by user defined signal handler.
Threading Issues
• Delivering signal in multi thread environment:
• Deliver signal to thread to which the signal applies
• Deliver every thread in process
• Certain thread in process
• Assign a specific thread to receive all signals
UNIX function to deliver signals: Kill(pid_t pid, int signal)
Windows: Asyncronous procedure call specifies function to be called when a signal
is delivered in windows.
3. Thread Cancellation:
Closing browser, searching a database with multiple threads in which only one
thread can access and others cancel is example of thread cancellation.
The thread that is to be cancelled is called target thread.
Threading Issues
• Asyncronous cancellation: one thread immediately terminates target thread.(ex. middle of
uploading data)
• Deffered cancellation: The target thread should check wether it should terminate itself in an
orderly fashion(check for flags).
Pthreads allows thread to enable or disable cancellaion. Cancellaion occurs only when thread
reaches cancellation point.
Cleanup handler is pthread function that allows the thread to release resource that is acquired.
4. Thread local storage:
Each thread shares one copy of data of a process(ex. Transaction processing) Java provide
Thread Local<T> class with get(), set() methods.
5. Schedular Activations:
Final issue is communication between the kernel and thread library. Data structure between
user ad kernel thread is called light weight process(LWP). LWP is a virtual process to schedule
user threads.
Threading Issues
• Schedular activation is the scheme of communication between user
and thread library and kernel thread library.
• Upcall procedure used by kernel to inform about certain events to
applications.
• Event that triggers upcall occurs when application thread is about to
be blocked. Kernel makes upcall to information that thread is about to
be blocked. The kernel that allocates new virtual processor to the
app. This will run upcall handler that saves states of blocking thread.
Process Syncronization
Race condition: several process access and manipulate the same data
consistently and outcome of execution depends on particular order in
which the process takes place is called race condition.
Example: producer consumer problem.
Code for producer process Code for consumer process
Process Syncronization
As an illustration, suppose that the value of the variable count is currently 5 and
that the producer and consumer processes.
Notice that we have arrived at the incorrect state “count == 4”, indicating that four
buffers are full, when, in fact, five buffers are full. If we reversed the order of the
statements at T4 and T5, we would arrive at the incorrect state “count ==
6”concurrently execute the statements “count++” and “count--”.
A situation like this, where several processes access and manipulate the same data
concurrently and the outcome of the execution depends on the particular order in
which the access takes place, is called a race condition.
Process Syncronization
Critical Section problem
• Consider a system consisting of n processes {P0, P1, ..., Pn−1}. Each process
has a segment of code, called a critical section, in which the process may be
accessing — and updating — data that is shared with at least one other
process.
• The important feature of the system is that, when one process is executing in
its critical section, no other process is allowed to execute in its critical section.
That is, no two processes are executing in their critical sections at the same
time. The critical-section problem is to design a protocol that the processes
can use to synchronize their activity so as to cooperatively share data.
• Each process must request permission to enter its critical section. The section
of code implementing this request is the entry section. The critical section may
be followed by an exit section. The remaining code is the remainder section.
Critical Section problem
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 deciding 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.
Critical Section problem
Critical Section problem
Two general approaches are used to handle critical sections in operating
systems:
A preemptive kernel: Allows a process to be preempted while it is
running in kernel mode. Preemptive kernels, must be carefully designed
to ensure that shared kernel data are free from race conditions.
Preemptive kernels are especially difficult to design for SMP
architectures, since in these environments it is possible for two kernel-
mode processes to run simultaneously on different CPU cores.
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. A non-
preemptive kernel is essentially free from race conditions on kernel data
structures, as only one process is active in the kernel at a time.
Hardware support for syncronization
1. Memory Barriers
System may reorder instructions, a policy that can lead to unreliable data states. How a
computer architecture determines what memory guarantees it will provide to an application
program is known as its memory model. In general, a memory model falls into one of two
categories: 1. Strongly ordered, where a memory modification on one processor is
immediately visible to all other processors. 2. Weakly ordered, where modifications to memory
on one processor may not be immediately visible to other processors.
2. Hardware Instructions
Many modern computer systems provide special hardware instructions that allow us either to
test and modify the content of a word or to swap the contents of two words atomically— that
is, as one uninterruptible unit.
We can use these special instructions to solve the critical-section problem in a relatively simple
manner.
Rather than discussing one specific instruction for one specific machine, we abstract the main
concepts behind these types of instructions by describing the test and set() and compare and
swap() instructions.
Hardware support for syncronization
1. test and set() instruction: The important characteristic of test and
set() instruction is that it is executed atomically. Thus, if two test and
set() instructions are executed simultaneously (each on a different
core), they will be executed sequentially in some arbitrary order. If the
machine supports the test and set() instruction, then we can
implement mutual exclusion by declaring a boolean variable lock,
initialized to false.
Hardware support for syncronization
• The compare and swap() instruction (CAS), operates on two words
atomically, but uses a different mechanism that is based on swapping
the content of two words. The operand value is set to new value only
if the expression (*value == expected) is true. Regardless, CAS always
returns the original value of the variable value.
• The important characteristic of this instruction is that it is executed
atomically. Thus, if two CAS instructions are executed simultaneously
(each on a different core), they will be executed sequentially in some
arbitrary order.
Hardware support for syncronization
Hardware support for syncronization
3. Automic Variables:
Atomic variables are basic building block for constructing other tools that
solve the critical-section problem. One such tool is an atomic variable,
which provides atomic operations on basic data types such as integers and
Booleans.
Atomic variables can be used in to ensure mutual exclusion in situations
where there may be a data race on a single variable while it is being
updated, as when a counter is incremented.
Most systems that support atomic variables provide special atomic data
types as well as functions for accessing and manipulating atomic variables.
Software - Mutex Locks
Hardware based solutions are inaccessible to applications
programmers. Mutex locks are high level programs
software to solve critical section problem and to avoid
race condition.
Acquire() - Process must acquire a lock before entering
critical section.
Release() – returns lock when process exits critical
section.
A process that attempts acquire an unavailable lock is
blocked until lock is released.
CPU utilization is high because bust waiting.
Software - Spin lock
Spin lock - When lock is to be held for short duration while thread goes
for waiting state and restored the general rule is to use spin lock . Spin
lock will be held for the duration of 2 context switch.
Semaphores: A semaphore S is a integer variable apart from
initialization it is accessed only through 2 standard atomic operations
wait() and signal().
Software - Semaphores
Semaphores:
Semaphores can solve various synchronization problems. For example, consider two concurrently
running processes: P1 with a statement S1 and P2 with a statement S2. Suppose we require that S2
be executed only after S1 has completed.
We can implement this scheme readily by letting P1 and P2 share a common semaphore synch,
initialized to 0. In process P1, we insert the statements S1; signal(synch);
In process P2, we insert the statements wait(synch); S2; Because synch is initialized to 0, P2 will
execute S2 only after P1 has invoked signal(synch), which is after statement S1 has been executed.
Semaphore Implementation:
To avoid busy waiting in mutex lock modify wait() and signal() add the waiting process in the
waiting queue and control transfer to CPU scheduler.
The sleep() operation suspends the process that invokes it. The wakeup(P) operation resumes the
execution of a suspended process P. These two operations are provided by the operating system as
basic system calls.
Negative list indicates the number of waiting process.
Semaphore
Monitors
Monitors:
All process share binary semaphore
variable mutex which is initialized to 1.
Each process must execute wait()
mutex and signal() mutex.
Monitor usage: An abstract datatype or
ADT encapsulates data with set of
functions to operate independently of
any specific implementaion of ADT.
The monitor constructor ensures that
only one process is executed inside
monitor.
Monitors
We assume that the pool consists of n buffers, each capable of holding one item. The mutex binary semaphore provides
mutual exclusion for accesses to the buffer pool and is initialized to the value 1. The empty and full semaphores count the
number of empty and full buffers. The semaphore empty is initialized to the value n; the semaphore full is initialized to the
value 0.
Classical problem of syncronization
2. The readers writer problem
Suppose that a database is to be shared among several concurrent processes. Some of these processes
may want only to read the database, whereas others may want to update (that is, read and write) the
database between these two types of processes by referring to the former as readers and to the latter
as writers.
Obviously, if two readers access the shared data simultaneously, no adverse effects will result. However,
if a writer and some other process (either a reader or a writer) access the database simultaneously,
chaos may ensue. This synchronization problem is referred to as the readers–writers problem.
The readers–writers problem has several variations, all involving priorities.
• The simplest one, referred to as the first readers–writers problem, requires that no reader be kept
waiting unless a writer has already obtained permission to use the shared object. In other words, no
reader should wait for other readers to finish simply because a writer is waiting.
• The second readers–writers problem requires that, once a writer is ready, that writer perform its
write as soon as possible. In other words, if a writer is waiting to access the object, no new readers
may start reading.
A solution to either problem may result in starvation. In the first case, writers may starve; in the second
case, readers may starve. For this reason, other variants of the problem have been proposed
Classical problem of syncronization
In the solution to the first readers–writers problem, the reader processes share the
following data structures:
semaphore rw mutex = 1; semaphore mutex = 1; int read count = 0;
The binary semaphores mutex and rw mutex are initialized to 1; read count is a
counting semaphore initialized to 0.
The semaphore rw mutex is common to both reader and writer processes.
The mutex semaphore
is used to ensure mutual exclusion when the variable read count is updated. The read
count variable keeps track of how many processes are currently reading the object.
The semaphore rw mutex functions as a mutual exclusion semaphore for the writers.
It is also used by the first or last reader that enters or exits the critical section.
Classical problem of syncronization
Classical problem of syncronization
Reader–writer locks are most useful in the following situations:
• In applications where it is easy to identify which processes only read shared data and which processes
only write shared data.
• In applications that have more readers than writers. This is because reader–writer locks generally
require more overhead to establish than semaphores or mutual-exclusion locks. The increased
concurrency of allowing multiple readers compensates for the overhead involved in setting up the
reader–writer lock.
Available
3 2 2
T1 0 1 0
3 3 2
Deadlock
Available
3 3 2
T1 2 0 0
5 3 2
T3 2 1 1
7 4 3
Deadlock
Available
3 3 2
T1 2 0 0
<T1, T3, T0, >
5 3 2
T3 2 1 1
7 4 3
T4 0 0 2
7 4 5
Deadlock
Available
3 3 2
T1 2 0 0
5 3 2
T3 2 1 1 <T1, T3, T4, T2, T0>
7 4 3
T4 0 0 2
7 4 5
T0 0 1 0
7 5 5
Deadlock
Available
3 3 2
T1 2 0 0
5 3 2
T3 2 1 1
7 4 3
T4 0 0 2
7 4 5
<T1, T3, T4, T0, T2> T0 0 1 0
7 5 5
T2 3 0 2
10 5 7
Deadlock
Deadlock detection
1. Single Instance of Each Resource Type
Resource Ri -> tx -> Rj
allocation Graph
Wait – for graph Ri -> Rj
Deadlock
2. Several Instances of a Resource Type
Deadlock
Detection-Algorithm Usage
1. How often is a deadlock likely to occur?
2. How many threads will be affected by deadlock when it happens?
Recovery from Deadlock
Abort all deadlocked processes: This method clearly will break the
deadlock cycle, but at great expense. The deadlocked processes may have
computed for a long time, and the results of these partial computations
must be discarded and probably will have to be recomputed later.
Abort one process at a time: until the deadlock cycle is eliminated. This
method incurs considerable overhead, since after each process is aborted,
a deadlock-detection algorithm must be invoked to determine whether
any processes are still deadlocked.
Deadlock
Many factors may affect which process is chosen, including:
1. What the priority of the process
2. How long the process has computed and how much longer the process will
compute before completing its designated task.
3. How many and what types of resources the process has used (for example,
whether the resources are simple to preempt)
4. How many more resources the process needs in order to complete.
5. How many processes will need to be terminated.
Resource pre-emption:
i. Selecting a victim
ii. Rollback
iii. starvation
Deadlock
1. Selecting a victim: Which resources and which processes are to be preempted? As in process
termination, we must determine the order of preemption to minimize cost. Cost factors may
include such parameters as the number of resources a deadlocked process is holding and the
amount of time the process has thus far consumed.
2. Rollback: If we preempt a resource from a process, what should be done with that process?
Clearly, it cannot continue with its normal execution; it is missing some needed resource. We must
roll back the process to some safe state and restart it from that state. Since, in general, it is difficult
to determine what a safe state is, the simplest solution is a total rollback: abort the process and
then restart it. Although it is more effective to roll back the process only as far as necessary to
break the deadlock, this method requires the system to keep more information about the state of
all running processes.
3. Starvation: How do we ensure that starvation will not occur? That is, how can we guarantee that
resources will not always be preempted from the same process? In a system where victim
selection is based primarily on cost factors, it may happen that the same process is always picked
as a victim. As a result, this process never completes its designated task, a starvation situation any
practical system must address. Clearly, we must ensure that a process can be picked as a victim
only a (small) finite number of times. The most common solution is to include the number of
rollbacks in the cost factor