0% found this document useful (0 votes)
11 views101 pages

OS Module-2

Uploaded by

archanabhat
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)
11 views101 pages

OS Module-2

Uploaded by

archanabhat
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/ 101

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.

● Process memory is divided into four sections


Process State
A Process has 5 states. Each process may be in one of the following states –
1. New - The process is in the stage of being created.
2. Ready - The process has all the resources it needs to run. It is waiting to be assigned to
the processor.
3. Running – Instructions are being executed.
4. Waiting - The process is waiting for some event to occur. For example, the process may be
waiting for keyboard input, disk access request, inter-process messages, a timer to go off, or
a child process to finish.
5. Terminated - The process has completed its execution.
Process Control Block
● Process State – The state of the process may be new, ready, running, waiting, 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. 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.
● Memory-management information – This includes information such as the value of the base and limit
registers, the page tables, or the segment tables.
● 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.
Process Scheduling
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.
● A ready-queue header contains pointers to the first and final PCBs in the list. Each
PCB includes a pointer field that points to the next PCB in the ready queue.
Ready Queue and Various I/O Device Queues
Schedulers
● A long-term scheduler or Job scheduler – selects jobs from the job pool and
loads them into the memory.
● The short-term scheduler, or CPU Scheduler – selects job from memory and
assigns the CPU to it.
● The medium-term scheduler - selects the process in ready queue and
reintroduced into the memory.
● Processes can be described as either:
I/O-bound process and CPU-bound process
An efficient scheduling system will select a good mix of CPU-bound processes and I/O bound processes.
- If the scheduler selects more I/O bound process, then I/O queue will be full and ready queue will be
empty.
- If the scheduler selects more CPU bound process, then ready queue will be full and I/O queue will
be empty.
Time sharing systems employ a medium-term scheduler. It swaps out the process from ready queue and
swap in the process to ready queue.
Context switching - The task of switching a CPU from one process to another process is called context
switching. Context-switch times are highly dependent on hardware support (Number of CPU registers).
Operations on Processes
There are two options for the parent process after creating the child:

● 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

● The software-packages that run on modern PCs are multithreaded. An


application is implemented as a separate process with several threads of
control. For ex: A word processor
● In some situations, a single application may be required to perform
several similar tasks.
● RPC servers are multithreaded.
● Most OS kernels are multithreaded
Benefits of Multithreaded Programming
● Responsiveness
● Resource Sharing
● Economy
● Utilization of Multiprocessor Architectures
MULTITHREADING MODELS
1. Support for threads may be provided at either
1. The user level, for user threads or
2. By the kernel, for kernel threads.
Three ways of establishing relationship between user-threads & kernel-threads:
1. Many-to-one model
2. One-to-one modeland
3. Many-to-many model.
Many-to-One Model

Many user-level threads are mapped to one


kernel thread.

Adv: Thread management is done by the thread


library in user space, so it is efficient.

Disadv: Multiple threads are unable to run in


parallel on multiprocessors.

Eg: Solaris green threads


GNU portable threads.
One-to-One Model
Each user thread is mapped to a kernel thread.

Adv: Multiple threads can run in parallel on


Multiprocessors.

Disadv: Creating a user thread requires creating


the corresponding kernel thread.

Eg: Windows NT/XP/2000, Linux


Many-to-Many Model
Many user-level threads are multiplexed to a smaller number of

kernel threads.

Adv: Developers can create as many user threads as necessary

The kernel threads can run in parallel on a multiprocessor.


Two Level Model
A variation on the many-to-many model is the two level-model

Similar to M:N, except that it allows a user thread to be bound to

Kernel thread.

Eg: HP-UX, Tru64 UNIX


Thread Libraries
It provides the programmer with an API for the creation and management of
threads. Two ways of implementation:
First Approach and Second Approach
Three main thread libraries:
1. POSIX Pthreads
2. Win32
3. Java.
Pthreads
● This is a POSIX standard API for thread creation and synchronization.
● This is a specification for thread-behavior, not an implementation.
● OS designers may implement the specification in any way theywish.
● Commonly used in: UNIX and Solaris.
Pthreads Example
Pthreads Example (Cont.)
Win32 threads
1. Implements the one-to-one mapping. Each thread contains - A
threadid, Registerset , Separate user and kernel stacks, Private data
storage area
2. The register set, stacks, and private storage area are known as the
context of the threads The primary data structures of a thread
include:
● ETHREAD (executive thread block)
● KTHREAD (kernel thread block)
● TEB (thread environment block)
Win32 API Multithreaded C Program
Win32 API Multithreaded C Program (Cont.)
Java Threads
● Threads are the basic model of program-execution in Java program and Java language.
● The API provides a rich set of features for the creation and management of threads.
● All Java programs comprise at least a single thread of control.
● Two techniques for creating threads:
1. Create a new class that is derived from the Thread class and override its run() method.
2. Define a class that implements the Runnable interface.
Java Multithreaded Program
Java Multithreaded Program (Cont.)
THREADING ISSUES
fork() and exec() System-calls
fork() is used to create a separate, duplicate process.
If one thread in a program calls fork(),then
1. Some systems duplicates all threads and
2. Other systems duplicate only the thread that invoked the forkO.
Thread Cancellation
This is the task of terminating a thread before it has completed.

Target thread is the thread that is to be cancelled

Thread cancellation occurs in two different cases:

1. Asynchronous cancellation: One thread immediately terminates the target 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

- CPU execution and I/O wait

Process execution begins with a CPU burst,

followed by an I/O burst, then another CPU burst, etc…


CPU Scheduler
This scheduler
- selects a waiting-process from the ready-queue and
- allocates CPU to the waiting-process.
CPU Scheduling
Four situations under which CPU scheduling decisions take place:
1. When a process switches from the running state to the waiting state. For ex; I/O request.
2. When a process switches from the running state to the ready state. For ex: when an
interrupt occurs.
3. When a process switches from the waiting state to the ready state. For ex: completion of
I/O.
4. When a process terminates.

Scheduling under 1 and 4 is non- preemptive. Scheduling under 2 and 3 is preemptive.


Non Preemptive Scheduling

● 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.

SJF algorithm may be either 1) non-preemptive or 2)preemptive.


Example (for non-preemptive SJF): Consider the following set of processes,
with the length of the CPU-burst time given in milliseconds.
Priority Scheduling
A priority is associated with each process. The CPU is allocated to the process with the highest priority.
Equal-priority processes are scheduled in FCFS order.

Priorities can be defined either internally or externally.

Priority scheduling can be either preemptive or non-preemptive.


Round Robin Scheduling
Designed especially for time sharing systems. It is similar to FCFS scheduling, but with preemption. A small
unit of time is called a time quantum(or timeslice). Time quantum is ranges from 10 to 100ms. The
ready-queue is treated as a circular queue.

The CPU scheduler

- goes around the ready-queue and


- allocates the CPU to each process for a time interval of up to 1 time quantum.
Example: Consider the following set of processes that arrive at time 0, with
the length of the CPU-burst time given in milliseconds.
The RR scheduling algorithm is preemptive.

- 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.

1. If time quantum=very large, RR policy is the same as the FCFS policy.

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

The basic idea is:

● Each processor is self-scheduling.


● To do scheduling, the scheduler for each processor
● Examines the ready-queue and
● Selects a process to execute.
Processor Affinity

In SMP systems,

1. Migration of processes from one processor to another are avoided and

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.

Two approaches: Push Migration and Pull Migration


Contention Scope
1. Process-Contention scope
On systems implementing the many-to-one and many-to-many models, the thread library schedules user-level
threads to run on an available LWP.
Competition for the CPU takes place among threads belonging to the same process.
2. System-Contentions cope
The process of deciding which kernel thread to schedule on the CPU.
Competition for the CPU takes place among all threads in the system.
Systems using the one-to-one model schedule threads using only SCS.
Pthread Scheduling
Pthread API that allows specifying either PCS or SCS during threadcreation.
Pthreads identifies the following contention scope values:
1 . PTHREAD_SCOPEJPROCESS schedules threads using PCS scheduling.
2 . PTHREAD-SCOPE_SYSTEM schedules threads using SCS scheduling.
Pthread IPC provides following two functions for getting and setting the contention scope policy:
1. pthread_attr_setscope(pthread_attr_t *attr, intscope)
2. pthread_attr_getscope(pthread_attr_t *attr, int*scop)
THANK YOU

You might also like