0% found this document useful (0 votes)
5 views

CS Notes

Notes of module 2

Uploaded by

shaniyanajeem002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

CS Notes

Notes of module 2

Uploaded by

shaniyanajeem002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 65

Module 2:

Processes &
Process Scheduling
Topics to Study

u Processes u Process Scheduling


u Process states u Basic concepts
u Process control block u Scheduling criteria
u Threads u Scheduling algorithms
u Scheduling u First Come First Served
u Operations on processes u Shortest Job First
u process creation and u Priority scheduling
termination u Round robin scheduling
u Inter-process communication
u Shared memory systems
u Message passing systems
Process Concept
u What to call the various activities executed by an operating
system ???
u Batch system executes – jobs

u Time-shared systems executes – user programs or tasks

u Even on a single-user system, a user can run several programs at the same
time:

ua word processor, a Web browser, and an e-mail package.

u As all these activities are similar, we call all of them Processes.

u The terms job and process are used almost interchangeably in


this text, because process has superseded job.
Process
u Process is a program in execution.
u Includes :-
u The program code, also called text section
u Current activity represented by values in
program counter and CPU registers
u Stack containing temporary data
u Function parameters,
u return addresses,
u local variables
u Data section containing global variables
u Heap containing memory dynamically allocated
during run time
Process Concept (Cont.)

u A program by itself is not a process.

u A program is a passive entity, such as a file containing a list of


instructions stored on disk (often called an executable file).

u In contrast, a process is an active entity, with a program counter


specifying the next instruction to execute and a set of associated
resources.

u A program becomes a process when an executable file is


loaded into memory.

u Two common techniques for loading executable files are


u double-clicking an icon representing the executable file
u entering the name of the executable file on the command line
Process State
u As a process executes, it changes it’s state.

u The state of a process is partly defined by the current activity of


that process.

u A process may be in one of the following states:

u new: The process is being created


u running: Instructions are being executed
u waiting: The process is waiting for some event to occur (an
I/O completion or reception of a signal)
u ready: The process is waiting to be assigned to a processor
u terminated: The process has finished execution
Process State Diagram

u Only one process can be running on any processor at any instant.

u Many processes may be ready and waiting, however.


Process Control Block (PCB)
u Information associated with each process is represented in the
operating system by a process control block (PCB) also called a
task control block.
u PCB contains :-
u Process state – running, waiting, etc
u Program counter – address of the next instruction to be executed
u CPU registers – include accumulators, index registers, stack pointers,
and general-purpose registers
u CPU scheduling information- priorities, scheduling queue pointers …
u Memory-management information – memory allocated to the
process, page tables …
u Accounting information – CPU time used, clock time elapsed since
start, time limits …
u I/O status information – I/O devices allocated to process, list of open
files …
CPU Switch From Process to Process

u The state information


stored in various CPU
registers along with the
program counter, must
be saved into PCB when
an interrupt occurs.

u This is done, in order to


allow the process to be
continued correctly
afterward
CPU Switch From Process to Process :- Context Switch

u When CPU switches to another process, the system must save the
state of the old process and load the saved state for the new
process via a context switch
u Context of a process represented in the PCB
u Context-switch time is overhead; the system does no useful work
while switching
u The more complex the OS and the PCB è the longer the context
switch
u Time is dependent on hardware support
u Some hardware provides multiple sets of registers per CPU è
multiple contexts loaded at once
Process Representation in Linux
The PCB in the Linux operating system is represented by the C structure task_struct
pid t_pid; /* process identifier */
long state; /* state of the process */
unsigned int time_slice /* scheduling information */
struct task_struct *parent; /* this processʼs parent */
struct list_head children; /* this processʼs children */
struct files_struct *files; /* list of open files */
struct mm_struct *mm; /* address space of this process */
Within the Linux kernel, all active processes are represented using a doubly linked list of task_struct.
The pointer current points to the process currently executing.

u The state of the process currently running can be changed to the value new_state with the
following: current->state = new_state;
Threads
u So far, a process is a program that performs a single thread of
execution.
u Ie., a single thread of instructions is being executed. This allows the
process to perform only one task at a time.
u 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.
u For example, when a process is running a word-processor program, the
user can simultaneously type in characters, run the spell checker within the
same process and do Auto-Save.
u Here multiple threads can run in parallel.
u On a system that supports threads, the PCB is expanded to
include information for each thread, such as multiple program
counters.
Process Scheduling
u Basic Concepts
u Scheduling Criteria
u Scheduling Algorithms
Basic Concepts
u In a single-processor system, only one process can run at a
time.
u A process is executed until it must wait, typically for the
completion of some I/O request. Till then, the CPU just sits idle.
u All this waiting time is wasted with no useful work
u The objective of multiprogramming is to have some process
running at all times, to maximize CPU utilization.
u Several processes are kept in memory at one time.
u When one process has to wait, the operating system takes the CPU
away from that process and gives the CPU to another process.
u This pattern continues.
u Every time one process has to wait, another process can take over
use of the CPU.
Basic Concepts :- CPU–I/O Burst Cycle

u Process execution consists of a cycle of


CPU execution and I/O wait
u Processes alternate between these two
states.
u Process execution begins with a CPU
burst .
u That is followed by an I/O burst,
u which is followed by another CPU burst,
u then another I/O burst, and so on.
u Eventually, the final CPU burst ends with
a system request to terminate execution
Histogram of CPU-burst Times

u The frequency curve is generally


characterized as exponential or hyper
exponential, with
u a large number of short CPU bursts
and
u a small number of long CPU bursts.
u An I/O-bound program typically has
many short CPU bursts.
u A CPU-bound program might have a
few long CPU bursts.
u I/O-bound process – spends more time doing
I/O than computations
u CPU-bound process – spends more time doing
computations
Basic Concepts :- CPU Scheduler

! Whenever the CPU becomes idle, the operating system must


select one of the processes in the **ready queue to be
executed.

! This selection process is carried out by the **Short-term


scheduler, or CPU scheduler.

! The scheduler selects a process from among the processes


in ready queue (that are ready to execute) and allocates
the CPU to that process.

! A ready queue can be implemented as a FIFO queue, a priority


queue, a tree, or simply an unordered linked list

** Refer next slide for details


Basic Concepts :- CPU Scheduler

u Maintains scheduling queues of processes


u Job queue – set of all processes in the system
u Ready queue – set of all processes residing in main memory, ready and waiting to
execute
u Device queues – set of processes waiting for an I/O device
u Processes migrate among the various queues
u Short-term scheduler (or CPU scheduler) – selects which process should be executed
next and allocates CPU
u Sometimes the only scheduler in a system
u Short-term scheduler is invoked frequently (milliseconds) Þ (must be fast)
u Long-term scheduler (or job scheduler) – selects which processes should be brought
into the ready queue
u Long-term scheduler is invoked infrequently (seconds, minutes) Þ (may be slow)
u The long-term scheduler controls the degree of multiprogramming
Basic Concepts :- Preemptive & Nonpreemptive Scheduling

! CPU scheduling decisions may take place, when a process:


1. Switches from running to waiting state
2. Switches from running to ready state
3. Switches from waiting to ready
4. Terminates

! Scheduling scheme under circumstances 1 and 4 is nonpreemptive or


cooperative
! 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
! All other scheduling is preemptive
Basic Concepts :- Preemptive & Nonpreemptive Scheduling
! Preemptive Scheduling
! A process is paused before completion: CPU is forcefully taken
away from that process and given to another process by the
scheduler
! Requires some special hardware (for example, a timer)
! Unfortunately, preemptive scheduling can result in severe problems
in the following circumstances:
" preemption while accessing some shared data
! preemption while in kernel mode
! preemption while interrupts occurring during crucial OS activities
! All previous versions of the Microsoft Windows and Mac OS X operating
system relied on cooperative scheduling and all subsequent versions
have used preemptive scheduling
Basic concepts : Dispatcher

u Dispatcher module gives control of the CPU to the process


selected by the short-term scheduler; this involves:
u switching context
u switching to user mode
u jumping to the proper location in the user program to
restart that program

u Dispatch latency – time it takes for the dispatcher to stop


one process and start another running
u The dispatcher should be as fast as possible
Scheduling Criteria
u Different CPU-scheduling algorithms have different properties

u In choosing which algorithm to use in a particular situation, we must consider :

u CPU utilization – keep the CPU as busy as possible (Conceptually, 0 to 100


percent. In reality, 40 to 90 percent)

u Throughput – Number of processes that complete their execution per time unit

u Turnaround time – amount of time to execute a particular process. The interval


from the time of submission of a process to the time of completion

u Waiting time – amount of time a process has been waiting in the ready queue.
The sum of the periods spent waiting in the ready queue.

u Response time – amount of time it takes from when a request was submitted
until the first response is produced, not output.
Scheduling Criteria (contd)

u Optimization Criteria for a good scheduling algorithm:

u Max CPU utilization

u Max throughput

u Min turnaround time

u Min waiting time

u Min response time


Scheduling Algorithms

u Scheduling algorithms

uFirst Come First Served

uShortest Job First

uPriority scheduling

uRound robin scheduling


First-Come, First-Served (FCFS) Scheduling

u Simplest CPU-scheduling algorithm


u The process, that requests the CPU first, is allocated the CPU
first.
u FCFS is implemented with a FIFO queue.
u When a process enters the ready queue, its PCB is linked onto the
tail of the queue.
u When the CPU is free, it is allocated to the process at the head of
the queue.
u The running process is then removed from the queue.
u Disadvantage:-
u The average waiting time under the FCFS policy is often quite long.
First-Come, First-Served (FCFS) Scheduling

Process CPU Burst Time (milliSeconds)


P1 24
P2 3
P3 3
u Suppose that the processes arrive in the order: P1 , P2 , P3
The Gantt Chart for the schedule is:

u Waiting time for P1 = 0; P2 = 24; P3 = 27


u Average waiting time: (0 + 24 + 27)/3 = 17 milliSeconds
FCFS Scheduling (Cont.)
Suppose that the processes arrive in the order:
P2 , P3 , P1
! The Gantt chart for the schedule is:

! Waiting time for P1 = 6; P2 = 0; P3 = 3


! Average waiting time: (6 + 0 + 3)/3 = 3
! Much better than previous case
! Convoy effect - all the other short processes have to wait for
the one big process to get off the CPU
" Consider one CPU-bound and many I/O-bound processes
Shortest-Job-First (SJF) Scheduling

u Associates with each process, the length of its next CPU burst
u When the CPU is available, it is assigned to the process that
has the smallest next CPU burst.
u If the next CPU bursts of two processes are the same, FCFS
scheduling is used to break the tie.
u Shortest-next- CPU-burst algorithm

u SJF is optimal – gives minimum average waiting time for a given


set of processes
u The difficulty is knowing the length of the next CPU request
u Could ask the user
Example of SJF

ProcessArriveBurst Time
P1 0.0 6
P2 2.0 8
P3 4.0 7
P4 5.0 3
u SJF scheduling chart

u Waiting time for. P1 = 3; P2 = 16; P3 = 9; P4 = 0


u Average waiting time = (3 + 16 + 9 + 0) / 4 = 7 msec
Shortest-remaining-time-first Scheduling

! The SJF algorithm can be either preemptive or nonpreemptive.

! The choice arises when a new process arrives at the ready


queue while a previous process is still executing.

! The next CPU burst of the newly arrived process may be


shorter than what is left of the currently executing process.

! A preemptive SJF algorithm will preempt the currently


executing process, whereas a nonpreemptive SJF algorithm
will allow the currently running process to finish its CPU burst.

! Preemptive SJF scheduling is sometimes called shortest-


remaining-time-first scheduling.
Example of Shortest-remaining-time-first

! Now we add the concepts of varying arrival times and preemption to the analysis
Processri Arrival Time Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
! At time 0, process P1 is started, since it is the only process in the queue.
! At time 1, Process P2 arrives.
Preemptive SJF Gantt Chart
Example of Shortest-remaining-time-first
Process Arrival Time Burst Time. t =0 1 2 3
P1 0 8 8✅ 7 7 7
P2 1 4. 4✅ 3✅ 2✅

P3 2 9 9 9
P4 3 5. 5

! The remaining time for process P1 (8-1= 7 milliseconds) is larger than the time
required by process P2 (4 milliseconds).
! So process P1 is preempted, and process P2 is scheduled.
! Average waiting time = [(10-1)+(1-1)+(17-2)+5-3)]/4 = 26/4 = 6.5 msec
Priority Scheduling
u A priority number (integer) is associated with each process
u The CPU is allocated to the process with the highest priority
**(smallest integer implies highest priority)

u Preemptive or Nonpreemptive
u SJF is a priority scheduling algorithm, where priority is the inverse of
predicted next CPU burst time
u Problem : Starvation or Indefinite blocking
ua steady stream of higher-priority processes can prevent a low-
priority process from ever getting the CPU, leaving some low-
priority processes waiting indefinitely
u low priority processes may never execute
u Solution : Aging - gradually increasing the priority of processes that
wait in the system for a long time
Example of Priority Scheduling

ProcessA Burst TimeTPriority


P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
u Priority scheduling Gantt Chart

u Average waiting time = (6+0+16+18+1) / 5 = 8.2 msec


Round Robin (RR) Scheduling
u Each process gets a small unit of CPU time (time quantum or
time slice q), usually 10-100 milliseconds.

u After this time has elapsed, the process is preempted and


added to the end of the ready queue.

u If there are n processes in the ready queue and the time


quantum is q, then each process gets 1/n of the CPU time in
chunks of at most q time units at once. No process waits more
than (n-1)q time units.

u Timer interrupts every quantum to schedule next process


Example of RR with Time Quantum = 4

Process Burst Time


P1 24
P2 3
P3 3 Time Quantum = 4
u The Gantt chart is:

u Waiting time for P1 = 6 (10 - 4), P2 = 4 , and. P3 = 7 msec.


u Average waiting time = 17/3 = 5.66 milliseconds.
Time Quantum and Context Switch

u Performance
u If q large Þ same as FIFO
u If q small Þ results in a large number of context switches, overhead is too high
u Typically, higher average turnaround than SJF, but better response
u q should be large compared to context switch time
Operations on Processes

u System must provide mechanisms for:


u process creation,
u process termination,
u and so on as detailed next
Process Creation

u A process may create several new processes.

u The creating process is called a Parent process, and the


new processes are called the children of that process.

u Each of these new processes may in turn create other


processes, forming a tree of processes.

u Generally, process is identified and managed via a process


identifier (pid)
A Tree of Processes in Linux

init
pid = 1

login kthreadd sshd


pid = 8415 pid = 2 pid = 3028

bash khelper pdflush sshd


pid = 8416 pid = 6 pid = 200 pid = 3610

emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
Process Creation (Cont.)

u Resource sharing possibilities


u Parent and children share all resources
u Children share subset of parentʼs resources
u Parent and child share no resources

u Execution possibilities
u Parent and children execute concurrently
u Parent waits until children terminate

u Address space possibilities


u Child is a duplicate of parent (it has the same program and
data as the parent)
u Child has a new program loaded into it
Process Creation (Cont.) UNIX examples

u fork() system call creates new process


u 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
u exec() system call is used after a fork() to replace the
processʼ memory space with a new program
u wait() system call is issued by the parent to move itself off
the ready queue until the termination of the child
C Program Forking Separate Process
Creating a Separate Process via Windows API
Process Termination
u A process terminates when it finishes executing its final statement
and then asks the operating system to delete it using the exit()
system call.
u Returns status data from child to parent (via wait())
u Processʼ resources are deallocated by operating system

u Parent may terminate the execution of children processes using


the abort() system call (or TerminateProcess() in Windows)
u Some reasons for doing so:
u Child has exceeded the usage of allocated resources
u Task assigned to child is no longer required
u The parent is exiting and the operating systems does not allow a child to
continue if its parent terminates
Process Termination

u Some operating systems do not allow child to exist if its parent


has terminated.
u If a process terminates, then all its children must also be
terminated. It is called
u Cascading termination. All children, grandchildren, etc. are
terminated.
u The termination is initiated by the operating system.
u The parent process may wait for termination of a child process
by using the wait()system call. The call returns status
information and the pid of the terminated process
pid = wait(&status);
Process Termination

u When a process terminates, its resources are deallocated by


the operating system.
u However, its entry in the process table must remain there until
the parent calls wait(), because the process table contains the
process’s exit status.

u A process that has terminated, but whose parent has not yet
called wait(), is known as a zombie process.

u If parent terminated without invoking wait , child process is


an orphan.
Interprocess Communication
u Processes executing concurrently in the operating system may be either
independent processes or cooperating processes.

Independent Processes
u A process is independent if it cannot affect or be affected by
the other processes executing in the system.
u Any process that does not share data with any other process is
independent.
Cooperating Processes
u A process is cooperating if it can affect or be affected by the
other processes executing in the system.
u Clearly, any process that shares data with other processes is a
cooperating process.
Advantages of Cooperating Processes

Reasons for allowing cooperating processes (Advantages):


u Information sharing.
u When several users are interested in the same piece of information, we
need to allow concurrent access to such information.
u Computation speedup.
u If we want a particular task to run faster, we must break it into subtasks,
each of which will be executing in parallel with the others.
u Modularity.
u We may want to construct the system in a modular fashion, dividing the
system functions into separate processes or threads.
u Convenience.
u Even an individual user may work on many tasks at the same time. For
instance, a user may be editing, listening to music, and compiling in
parallel.
Interprocess Communication (IPC)

u Cooperating processes need interprocess communication (IPC)


mechanism that will allow them to exchange data and information
u There are two fundamental models of interprocess communication :
u Shared memory and Message passing
u Shared memory
u A region of memory that is shared by cooperating processes is
established.
u Processes can then exchange information by reading and writing data
to the shared region.
u Message passing
u Communication takes place by means of messages exchanged between
the cooperating processes.
Communications Models
(a) Message passing. (b) shared memory.

process A process A

process B shared memory

process B

message queue
m0 m1 m2 m3 ... mn
kernel
kernel

(a) (b)
u Message passing is useful for exchanging smaller amounts of data
u Shared memory can be faster than message passing, since message-passing
systems are typically implemented using system calls
Interprocess Communication – Shared Memory
Producer-Consumer Problem

u To illustrate the concept of cooperating processes, let’s consider


the producer–consumer problem, which is a common paradigm
for cooperating processes.
u A producer process produces information that is consumed by a
consumer process.
u For example, a compiler may produce assembly code that is
consumed by an assembler.
u The assembler, in turn, may produce object modules that are
consumed by the loader.
u It also provides a useful metaphor for the client–server paradigm.
u We generally think of a server as a producer and a client as a consumer.
Producer-Consumer Problem – Solution using shared memory

u One solution to the producer–consumer problem uses shared memory.


u Here, we must have available a buffer of items that can be filled by
the producer and emptied by the consumer.
u This buffer will reside in a region of memory that is shared by the
producer and consumer processes.
u Two types of buffers can be used
u unbounded-buffer places no practical limit on the size of the buffer
u Theconsumer may have to wait for new items, but the producer
can always produce new items
u bounded-buffer assumes that there is a fixed buffer size
u The
consumer must wait if the buffer is empty, and the producer
must wait if the buffer is full.
Bounded-Buffer – Shared-Memory Solution

out
in
0

u The shared buffer is implemented as a circular array with two logical pointers: in and out.
u in points to the next free position in the buffer;
u out points to the first full position in the buffer.
u The buffer is empty when in == out;
u the buffer is full when ((in + 1) % BUFFER SIZE) == out.
u Solution is correct, but can only use BUFFER_SIZE-1 elements
Producer - using Bounded-Buffer

item next_produced;
while (true) {
/* produce an item in next produced */
while (((in + 1) % BUFFER_SIZE) == out)
; /* do nothing */
buffer[in] = next_produced;
in = (in + 1) % BUFFER_SIZE;
}
Consumer - using Bounded-Buffer

item next_consumed;
while (true) {
while (in == out)
; /* do nothing */
next_consumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;

/* consume the item in next consumed */


}
Interprocess Communication – Message Passing

u Provides a mechanism to allow processes to communicate and


to synchronize their actions without sharing the same address
space

u A message-passing facility provides two operations:


u send(message)
u receive(message)

u The message size is either fixed or variable


Message Passing (Cont.)

u If processes P and Q wish to communicate, they need to:


u Establish a communication link between them
u Exchange messages via send/receive

u Implementation issues:
u How are links established?
u Can a link be associated with more than two processes?
u How many links can there be between every pair of communicating
processes?
u What is the capacity of a link?
u Is the size of a message that the link can accommodate fixed or variable?
u Is a link unidirectional or bi-directional?
Message Passing (Cont.)

Implementation of communication link


u Physical Implementation :
u Sharedmemory
u Hardware bus
u Network
u Logical Implementation :
u Direct or indirect
u Synchronous or asynchronous
u Automatic or explicit buffering
Direct Communication

u Processes must name each other explicitly:


u send (P, message) – send a message to process P
u receive(Q, message) – receive a message from process Q

u Properties of communication link


u A link is established automatically between every pair of
processes that want to communicate.
u A link is associated with exactly one pair of communicating
processes
u Between each pair there exists exactly one link
u The link may be unidirectional, but is usually bi-directional
Indirect Communication

u Messages are sent to and received from mailboxes or ports


u Each mailbox has a unique id
u Processes can communicate only if they share a mailbox
u send(A, message) — Send a message to mailbox A.
u receive(A, message) — Receive a message from mailbox A

u Properties of communication link


u Link is established only if processes share a common mailbox
u A link may be associated with many processes
u Each pair of processes may share several communication links
u Link may be unidirectional or bi-directional
Indirect Communication

u Suppose processes P1, P2, and P3 all share mailbox A.


u Process P1 sends a message to A, while both P2 and P3 execute
a receive() from A.
u Which process will receive the message sent by P1?

u The answer depends on which of the following methods we


choose:
u Allow a link to be associated with at most two processes
u Allow only one process at a time to execute a receive
operation
u Allow the system to select arbitrarily the receiver. Sender
is notified who the receiver was.
Synchronization

! Message passing may be either blocking or non-blocking


! Blocking is considered synchronous
" Blocking send -- the sender is blocked until the message is received
" Blocking receive -- the receiver is blocked until a message is available
! Non-blocking is considered asynchronous
" Non-blocking send -- the sender sends the message and continues
" Non-blocking receive -- the receiver receives: A valid message, or Null
message
! Different combinations of send() and receive() are possible
" If both send and receive are blocking, we have a rendezvous between
the sender and the receiver.
Synchronization (Cont.)

! Producer-consumer problem
becomes trivial when we use
blocking send() and receive()
statements.

! The producer merely invokes


the blocking send() call and
waits until the message is
delivered to either the
receiver or the mailbox.

! Likewise, when the


consumer invokes receive(),
it blocks until a message is
available.
Buffering

u Whether communication is direct or indirect, messages


exchanged by communicating processes reside in a temporary
queue.

u Basically, such queues can be implemented in three ways:


1.Zero capacity – no messages are queued waiting in a link.
Sender must wait for receiver (rendezvous)
2.Bounded capacity – finite length of n messages
If the link is full, the sender must block until space is available
in the queue.
3.Unbounded capacity – infinite length, any number of messages
Sender never waits

You might also like