OS Module-2
OS Module-2
Chapter 2: PROCESS
MANAGEMENT
● Process concept
● Process scheduling
● Operations on processes
● Inter process communication
The Process
A process is a program under execution.
● Wait for the child process to terminate and then continue execution. The parent makes a wait()
system call.
● Run concurrently with the child, continuing to execute without waiting.
Two possibilities for the address space of the child relative to the parent:
Process Termination
● A process terminates when it finishes executing its last statement and asks the operating
system to delete it, by using the exit () system call. All of the resources assigned to the process
like memory, open files, and I/O buffers, are deallocated by the operating system.
● A process can cause the termination of another process by using appropriate system call. The
parent process can terminate its child processes by knowing of the PID of the child.
A parent may terminate the execution of children for a variety of reasons, such as:
• The child has exceeded its usage of the resources, it has been allocated.
• The task assigned to the child is no longer required.
• The parent is exiting, and the operating system terminates all the children. This is called cascading
termination.
Interprocess Communication
Interprocess Communication- Processes executing may be either co-operative or
independent processes.
● Independent Processes – processes that cannot affect other processes or
be affected by other processes executing in the system.
● Cooperating Processes – processes that can affect other processes or be
affected by other processes executing in the system.
Co-operation among processes are allowed for following reasons –
Information Sharing - There may be several processes which need to access the same file.
So the information must be accessible at the same time to all users.
Computation speedup - Often a solution to a problem can be solved faster if the problem
can be broken down into sub-tasks, which are solved simultaneously (particularly when
multiple processors are involved.)
Modularity - A system can be divided into cooperating modules and executed by sending
information among one another.
Convenience - Even a single user can work on multiple tasks by information sharing.
● Cooperating processes require some type of inter-process communication.
This is allowed by two models:
1. Shared Memory systems
2. Message passing systems.
Producer-Consumer Example Using Shared Memory
This is a classic example, in which one process is producing data and another process is consuming the data.
There are two types of buffers into which information can be put –
• Unbounded buffer
• Bounded buffer
•With Unbounded buffer, there is no limit on the size of the buffer, and so on the data produced by producer.
But the consumer may have to wait for new items.
•With bounded-buffer – As the buffer size is fixed. The producer has to wait if the buffer is full and the
consumer has to wait if the buffer is empty.
This example uses shared memory as a circular queue. The in and out are two pointers to the
array.
Note in the code below that only the producer changes "in", and only the consumer changes "out".
Message-Passing Systems
A mechanism to allow process communication without sharing address space. It is used
in distributed systems.
- Message passing systems uses system calls for "send message" and "receive message".
- A communication link must be established between the cooperating processes before
messages can be sent.
There are three methods of creating the link between the sender and the receiver-
o Direct or indirect communication (naming)
o Synchronous or asynchronous communication (Synchronization)
o Automatic or explicit buffering.
1. Naming Processes that want to communicate must have a way to refer
to each other. They can use either direct or indirect communication.
a) Direct communication the sender and receiver must explicitly know each
other’s name. The syntax for send() and receive() functions are as follows-
• send (P, message) – send a message to process P
• receive(Q, message) – receive a message from process Q
Properties of communication link:
● A link is established automatically between every pair of processes
that wants to communicate. The processes need to know only each
other's identity to communicate.
● A link is associated with exactly one pair of communicating processes
● Between each pair, there exists exactly one link.
Types of addressing in direct communication –
Symmetric addressing and Asymmetric addressing
send (P, message) --- Send a message to process P
receive (id, message). Receive a message from any process
Indirect communication
A mailbox or port is used to send and receive messages. Mailbox is an object into
which messages can be sent and received. It has a unique ID. Using this
identifier messages are sent and received.
send (A, message) – send a message to mailbox A
receive (A, message) – receive a message from mailbox A
Properties of communication link:
● A link is established between a pair of processes only if they have a shared mailbox
● A link may be associated with more than two processes
● Between each pair of communicating processes, there may be any number of links,
each link is associated with one mailbox.
● A mailbox can be owned by the operating system. It must take steps to –
- create a new mailbox
- send and receive messages from mailbox
- delete mailboxes.
Synchronization
The send and receive messages can be implemented as either blocking or non-blocking.
● Blocking (synchronous) send
● Non-blocking (asynchronous) send
● Blocking (synchronous) receive
● Non-blocking (asynchronous) receive
Buffering
When messages are passed, a temporary queue is created. Such queue can be of three
capacities:
● Zero capacity
● Bounded capacity
● Unbounded capacity
Multi-threaded Programming
● Multithreading models
● Thread Libraries
● Threading issues.
MULTITHREADED PROGRAMMING
- A thread is a basic unit of CPU utilization.
- It consists of thread ID, PC, register-set and stack.
Motivation for Multithreaded Programming
kernel threads.
Kernel thread.
2. Deferred cancellation: The target thread periodically checks whether it should be terminated.
Signal Handling
In UNIX, a signal is used to notify a process that a particular event has occurred. All signals follow this
pattern:
1. A signal is generated by the occurrence of a certain event.
2. A generated signal is delivered to a process.
3. Once delivered, the signal must be handled.
A signal handler is used to process signals. A signal may be received either synchronously or
asynchronously, depending on thesource.
Synchronous signals and Asynchronous signals
Every signal can be handled by one of two possible handlers:
A Default Signal Handler and A User-defined SignalHandler
THREAD POOLS
The basic idea is to
- create a no. of threads at process-startup and
- place the threads into a pool (where they sit and wait for work).
Procedure:
1. When a server receives a request, it awakens a thread from the pool.
2. If any thread is available, the request is passed to it for service.
3. Once the service is completed, the thread returns to the pool.
THREAD SPECIFIC DATA
● Threads belonging to a process share the data of the process.
● This sharing of data provides one of the benefits of multithreaded programming.
● In some circumstances, each thread might need its own copy of certain data. We
will call such data thread-specific data.
For example, in a transaction-processing system, we might service each transaction in
a Separate thread.
SCHEDULER ACTIVATIONS
● Both M:M and Two-level models require communication to maintain the appropriate number of kernel
threads allocated to the application.
● Scheduler activations provide upcalls a communication mechanism from the kernel to the thread
library
● This communication allows an application to maintain the correct number kernel threads
● One scheme for communication between the user-thread library and the kernel is known as scheduler
activation.
PROCESS SCHEDULING
CPU-I/0 Burst Cycle
Process execution consists of a cycle of
● Once the CPU has been allocated to a process, the process keeps the CPU until it releases the CPU
either by terminating or by switching to the waiting state.
Preemptive Scheduling
● This is driven by the idea of prioritized computation. Processes that are runnable may be temporarily
suspended
Dispatcher
It gives control of the CPU to the process selected by the short-term scheduler.
SCHEDULING CRITERIA
1. CPU utilization - CPU as busy as possible
2. Throughput - number processes that are completed per time Unit
3. Turnaround time - Completion time - Arrival time
4. Waiting time - Turnaround time - burst time
5. Response time - time from the submission of a request until the first
response is produced.
SCHEDULING ALGORITHMS
CPU scheduling deals with the problem of deciding which of the processes in the ready-queue is to be allocated
the CPU.
Following are some scheduling algorithms:
1. FCFS scheduling (First Come First Served)
2. Round Robin scheduling
3. SJF scheduling (Shortest Job First)
4. SRT scheduling
5. Priority scheduling
6. Multilevel Queue scheduling and
7. Multilevel Feedback Queue scheduling
FCFS Scheduling
The process that requests the CPU first is allocated the CPU first. The implementation is easily
done using a FIFOqueue.
Procedure:
1. When a process enters the ready-queue, its PCB is linked onto the tail of the queue.
2. When the CPU is free, the CPU is allocated to the process at the queue’s head.
3. The running process is then removed from the queue.
Advantage: Code is simple to write & understand.
Disadvantages: Convoy effect: All other processes wait for one big process to get off the CPU.
FCFS
Suppose that the processes arrive in the order P2, P3,P1.
SJF Scheduling
The CPU is assigned to the process that has the smallest next CPU burst.
If two processes have the same length CPU burst, FCFS scheduling is used to break the tie.
Advantage: The SJF is optimal, i.e. it gives the minimum average waiting time for a given set of
processes.
Disadvantage: Determining the length of the next CPU burst.
- No process is allocated the CPU for more than 1 time quantum in a row.
- If a process' CPU burst exceeds 1 time quantum, that process is preempted and is put back in the
ready- queue.
The performance of algorithm depends heavily on the size of the time quantum.
2. If time quantum=very small, RR approach appears to the users as though each of n processes has its
own processor running at l/n the speed of the real processor.
Multilevel Queue Scheduling
Useful for situations in which processes are easily classified into different groups.
For example, a common division is made between
- foreground (or interactive) processes and
- background (or batch) processes.
The processes are permanently assigned to one queue based on some property like
- memory size
- process priority or
- process type.
Multilevel Feedback Queue Scheduling
MULTIPLE PROCESSOR SCHEDULING
If multiple CPUs are available, the scheduling problem becomes more complex. Two approaches:
Asymmetric Multiprocessing
The basic idea is:
● A master server is a single processor responsible for all scheduling decisions, I/O processing and other
system activities.
● The other processors execute only user code.
Advantage: This is simple because only one processor accesses the system data structures, reducing the need
for data sharing.
Symmetric Multiprocessing
In SMP systems,
2. Instead processes are kept running on same processor. This is known as processor affinity.
Load Balancing
This attempts to keep the workload evenly distributed across all processors in an SMP system.