Module 2
Module 2
Process
Management
Process Management
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
Address space
Child duplicate of parent
Child has a program loaded into it
UNIX examples
fork() system call creates new process
exec() system call used after a fork() to replace the process’
memory space with a new program
C Program Forking Separate Process
Creating a Separate Process via Windows API
Process Termination
Process executes last statement and then asks the operating system to delete it
using the exit() system call.
Returns status data from child to parent (via wait())
Process’ resources are deallocated by operating system
Parent may terminate the execution of children processes using the abort() system
call. Some reasons for doing so:
Child has exceeded allocated resources
Task assigned to child is no longer required
The parent is exiting and the operating systems does not allow a child to
continue if its parent terminates
Some operating systems do not allow child to exists if its parent has terminated. If a
process terminates, then all its children must also be terminated.
cascading termination. All children, grandchildren, etc. are
terminated.
The termination is initiated by the operating system.
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);
If no parent waiting (did not invoke wait()) process is a zombie
If parent terminated without invoking wait , process is an orphan
Interprocess Communication
Processes within a system may be independent or cooperating
Independent - if it cannot affect or be affected by the other processes executing
in the system – do not share data with any
Cooperating process can affect or be affected by other processes, including sharing data
Reasons for cooperating processes:
Information sharing
Computation speedup
Modularity
Convenience
Cooperating processes need interprocess communication (IPC) to exchange the data and
information between each other.
Two models of IPC : 1) Shared memory 2) Message passing
Communication Models
(a) Message passing. (b) shared memory.
The communication is under the control of the users processes not the operating system.
Major issues is to provide mechanism that will allow the user processes to
synchronize their actions when they access shared memory.
item buffer[BUFFER_SIZE];
int in = 0;
int out = 0;
; /* do nothing */ ; /* do nothing */
next_consumed = buffer[out];
buffer[in] = next_produced;
out = (out + 1) % BUFFER_SIZE;
in = (in + 1) % BUFFER_SIZE;
}
/* consume the item in next consumed */
}
Interprocess Communication – Message
Passing
Mechanism for processes to communicate and to synchronize their actions without sharing
the address space
Useful for distributed systems where processes reside on different systems
IPC facility provides two operations:
send(message)
receive(message)
The message size is either fixed or variable
Fixed size easy to implement, programming becomes difficult
Variable size difficult to implement, easy for programming
If processes P and Q wish to communicate, they need to:
Establish a communication link between them
Exchange messages via send/receive
Implementation of communication link
Physical:
Shared memory
Hardware bus
Network
Logical:
Direct or indirect
Synchronous or asynchronous
Automatic or explicit buffering
Overview,Multithreading Models,
Thread Libraries,Threading Issues
Overview
● A thread is a basic unit of CPU utilization, consisting of a program
counter, a stack, and a set of registers, ( and a thread ID. )
● Traditional (heavyweight) processes have a single thread of control - There
is one program counter, and one sequence of instructions that can be carried
out at any given time.
● As shown in Figure 4.1, multi-threaded applications have multiple threads
within a single process, each having their own program counter, stack and
set of registers, but sharing common code, data, and certain structures such as
open files.
Motivation
● Threads are very useful in modern programming whenever a process has multiple
tasks to perform independently of the others.
● This is particularly true when one of the tasks may block, and it is desired to allow
the other tasks to proceed without blocking.
● For example in a word processor, a background thread may check spelling and
grammar while a foreground thread processes user input ( keystrokes ), while yet a
third thread loads images from the hard drive, and a fourth does periodic automatic
backups of the file being edited.
● Another example is a web server - Multiple threads allow for multiple requests to be
satisfied simultaneously, without having to service requests sequentially or to fork off
separate processes for every incoming request.
Benefits
There are four major categories of benefits to multi-threading:
● Responsiveness - One thread may provide rapid response while other threads are
blocked or slowed down doing intensive calculations.
● Resource sharing - By default, threads share common code, data, and other
resources, which allows multiple tasks to be performed simultaneously in a single
address space.
● Economy - Creating and managing threads (and context switches between them )
is much faster
● Scalability i.e. Utilization of multiprocessor architectures - A single
threaded process can only run on one CPU, whereas the execution of a multi-
threaded application may be split amongst available processors.
Multithreading Models
● Thread libraries provide programmers with an API for creating and managing
threads.
● Thread libraries may be implemented either in user space or in kernel space. The
former involves API functions implemented solely within user space, with no kernel
support. The latter involves system calls, and requires a kernel with thread library
support.
● There are three main thread libraries in use today:
1. POSIX Pthreads - may be provided as either a user or kernel library, as an
extension to the POSIX standard.
2. Win32 threads - provided as a kernel-level library on Windows systems.
3. Java threads - Since Java generally runs on a Java Virtual Machine (JVM) ,
the implementation of threads is based upon whatever OS and hardware the
JVM is running on, i.e. either Pthreads or Win32 threads depending on the
system.
Pthreads
● The POSIX standard (IEEE 1003.1c) defines the specification for pThreads,
not implementation
● pThreads are available on Solaris, Linux, Mac OSX, Tru64, and via public
domain shareware for Windows.
● One thread can wait for the others to rejoin before continuing
● The multi-threaded applications can use the Win32 API library similar to
Pthread library.
Q: If one thread forks, is the entire process copied, or is the new process
single-threaded?
A: System dependant.
A: If the new process execs right away, there is no need to copy all the other
threads. If it doesn't, then the entire process should be copied.
A: Many versions of UNIX provide multiple versions of the fork call for this
purpose.
Continued
● UNIX systems supports two versions of fork(), one that duplicates all
threads and another that duplicates only the thread that invoked the fork()
system call.
2. Deferred cancellation
● One thread indicates that a target thread is to be canceled, but target thread
can check whether it should be terminated or not
● Given an opportunity to terminate. If it wants to terminate it will look for
the point where it can terminate safely
● Pthreads refers to such points as cancellation points
Signal Handling
● The default signal handler will run by the kernel which can perform the
default action to handle the signal.
Continued
● The User-defined handler will override the default actions to handle the signals.
● Some signals can be handled by simply ignoring them and others can be
handled by terminating the program.
● When a process has ‘n’ threads, signals can be delivered to
1. Deliver the signal to the thread to which the signal applies
2. Deliver the signal to every thread in the process
3. Deliver the signal to certain threads in the process
4. Assign a specific thread to receive all signals for the process
Thread Pools
● Issues of thread are the amount of time required to create the thread, unlimited
threads could exhaust system resources, such as CPU time or memory. One
solution to this issue is to use a thread pool.
● The general idea behind a thread pool is to create a number of threads at process
startup and place them into a pool.
● When a server gets a request for the thread, it awakens a thread from the pool and
if thread is available it assigns the request to it. Once the thread completes its job it
can return back to the pool.
● If thread is not available server can wait.
● Advantages are
1. Servicing a request with an existing thread is usually faster.
2. A thread pool limits the number of threads that exist at any point of time.
Thread Specific Data
● In some circumstances, a thread may need to have its own copy of data
known as thread specific data.
● Each LWP is attached to a kernel thread, and it is kernel threads that the
operating system schedules to run on physical processors.
● If a kernel thread blocks, the LWP blocks and intern user-level thread
attached to the LWP also blocks.
● The communication between the user-thread library and the kernel is known
as scheduler activation.
Continued
Ex. When an application thread is about to block, a kernel makes an upcall to notices the
application about the blocking of specific thread. The kernel then allocates a new
processor (LWP) to the application on which it can run an upcall handler, which saves
the state of the blocking thread and relinquishes the virtual processor on which the
blocking thread is running. The upcall handler then schedules another thread that is
eligible to run on the new virtual processor.
Continued
Process Scheduling
Module 2
Introduction
● By switching the CPU among processes, the OS can make the computer
more productive
● When more than one process is runnable, the operating system must decide
which one first.
● The part of the operating system concerned with the above decision is called
the scheduler
● Process execution consists of a cycle of a CPU time burst and an I/O time
burst (i.e. wait).
● Processes alternate between these two states (i.e., CPU burst and I/O
burst). Eventually, the final CPU burst ends with a system request to
terminate execution
Figure: Alternating sequence of CPU and I/O bursts & CPU
Burst Histogram
CPU Scheduler
● Whenever the CPU becomes idle, the OS must select the processes in the
ready queue to be executed
● The selection process is carried out by the short-term scheduler (or CPU
Scheduler)
● The scheduler selects the process from the processes in memory that are
ready to execute and allocate the CPU to that process
Preemptive & Non-Preemptive Scheduling
● CPU scheduling decision may take place under the following four
circumstances:
1. When a process switches from running to waiting state.
For ex: as a result of I/O request or the invocation of wait for the termination
of one of the child processes
2. When a process switches from running to ready state.
For ex: when an interrupt occurs
3. When a process switches from waiting to ready state.
For example at completion of I/O
4. When a processes switches from running to terminated state.
Continued
4. Waiting time –The sum of amount of time a process wait in ready queue
(doesn’t affect the amount of time during which a process executes or does I/O;)
5. Turnaround time – The amount of time to execute a particular process from
the time of submission through the time of completion. (completion time – arrival
time)
Scheduling Algorithm Optimization Criteria
● Max CPU utilization
● Max throughput
● Min turnaround time
● Min waiting time
● Min response time (minimize the variance)
Scheduling Algorithms
● CPU Scheduling deals with the problem of deciding which of the
processes in the ready queue is to be allocated the CPU
Terminologies for CPU scheduling algorithms:
1. Arrival Time: Time at which the process arrives in the ready queue.
2. Completion Time: Time at which process completes its execution.
3. Burst Time: Time required by a process for CPU execution.
4. Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
5. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
First-Come, First-Served Scheduling
1) Assume processes arrives in the order P1,P2,P3 at t=0. Show the FCFS
result on Gantt chart.
Process Burst time
P1 24
P2 3
P3 3
Gantt chart:
The average waiting time= ((0-0)+(24-0)+(27-0))/3= 17 ms
The average turnaround time=((24-0)+(27-0)+(30-0))/3= 27 ms
Shortest-Job-First Scheduling
● The SJF algorithm associates with each process the length of its next CPU
burst.
● When the CPU becomes available, it is assigned to the process that has the
smallest next CPU burst (in the case of matching bursts, FCFS is used)
● The SJF scheduling algorithm is provably optimal.
● The difficulty with the SJF algorithm is knowing the length of the next
CPU request.
● SJF algorithm cannot be implemented at the level of short-term CPU
scheduling. There is no way to know the length of the next CPU burst.
Determining Length of Next CPU Burst
● SJF algorithm Can only estimate the length of the next CPU burst– should be
similar to the previous one
○ Then pick process with shortest predicted next CPU burst
● Can be done by using the length of previous CPU bursts, using exponential
averaging
1. t n actual length of n th CPU burst
2. n 1 predicted value for the next CPU burst
3. , 0 1
4. Define : n+1 t n 1 n .
● Commonly, α set to ½
● =0
○ n+1 = n
○ Recent history does not count
● =1
○ n+1 = tn
○ Only the actual last CPU burst counts
P4 P1 P3 P2
0 3 9 16 24
P1 P2 P4 P1 P3
0 1 5 10 17 26
P1 0.0 6
P2 0.0 4
P3 0.0 1
P4 0.0 5
p1 p2 p3 p4
● The SJF algorithm is a special case of the general priority scheduling algorithm.
● A priority number (integer) is associated with each process.
● The CPU is allocated to the process with the highest priority (smallest CPU burst =
highest priority)
● Priority scheduling can be either preemptive or non-preemptive.
○ A preemptive approach will preempt the CPU if the priority of the newly-arrived
process is higher than the priority of the currently running process
○ A non-preemptive approach will simply put the new process (with the highest
priority) at the head of the ready queue
Contd
● The main problem with priority scheduling is starvation, that is, low priority
processes may never get a chance to execute.
● A solution is aging. As time progresses, the priority of a process waiting for
the long time in the ready queue is increased
Problems on priority Scheduling
Ex. Consider the following set of processes arriving at t=0, in the order p1, p2,p3,p4,p5
with the burst time as shown. Using priority scheduling we would schedule these
processes as shown in the Gantt chart.(Non-preemptive)
Process Burst time Priority
P1 10 3
P2 1 1
P3 2 4
P4 1 5
P5 5 2
Gantt chart
Ex. Process Burst time (Assume time quantum=4ms, arrival time is t=0)
P1 24
P2 3
P3 3
The Gantt chart is:
● One rule of thumb is that 80% of the CPU bursts should be shorter than the
time quantum.
Problems on Round Robin Scheduling
Ex.2: Process Burst time (Assume time quantum=20ms, arrival time is t=0)
P1 53
P2 17
P3 68
P4 24
● A multi-level queue scheduling algorithm partitions the ready queue into several
separate queues.
● The processes are permanently assigned to one queue, generally based on some
property of the process such as memory size, process priority, or process type .
Contd
● The foreground queue may have absolute priority over the background
queue.
● One example of a multi-level queue are the five queues as shown below.
● Each queue has absolute priority over lower priority queues. For example, no
process in the batch queue can run unless the queues above it are empty. And
whenever a new interactive process enters while executing batch processes, the
batch process would be preempted.
● However, this can result in starvation for the processes in the lower priority
queues.
● The solution is to introduce time slice among the queues.
● Each queue gets a certain portion of the CPU time, which it can then
schedule among its various processes.
o The foreground queue can be given 80% of the CPU time for RR
scheduling.
o The background queue can be given 20% of the CPU time for FCFS
scheduling.
Multilevel Feedback Queue scheduling
● The multilevel queue scheduling has the advantage of low scheduling
overhead, but it is inflexible. The multilevel feedback-queue scheduling
algorithm, in contrast, allows a process to move between queues.
● The idea is to separate processes according to the characteristics of their CPU
bursts. If a process uses too much CPU time,it will be moved to a lower-
priority queue. This scheme leaves I/O-bound and interactive processes in the
higher-priority queues.
● A process that waits too long in a lower-priority queue may be moved to a
higher-priority queue. This form of aging prevents starvation.
Contd