Chapter 2. Process and Process Management
Chapter 2. Process and Process Management
Chapter-2
1 6/27/2019
Objectives
To introduce the concept of a process -- a program in
execution, which forms the basis of all computation
To describe the various features of processes,
including scheduling, creation and termination, and
communication
To describe communication in client-server systems
2 6/27/2019
Points to cover- Ref book-Galvin
Process concept and process states
3 6/27/2019
Process concepts-Aim
Process is program in execution.
4 6/27/2019
Process concept
An operating system executes a variety of programs:
Batch system – jobs
Time-shared systems – user programs or tasks
The terms job and process are same
Process – a program in execution; process
Execution must progress in sequential fashion
Multiple parts of process in memory:
The program code, also called text section,
Current activity including program counter,
processor registers
Process Stack containing temporary data
Function parameters, return addresses, local variables
Data section containing global variables
Heap containing memory dynamically allocated during run time
5
6/27/2019
Process concept
Program is passive entity stored on disk (executable file),
process is active
Program becomes process when executable file loaded
into memory
Execution of program started via GUI mouse clicks,
command line entry of its name, etc a.exe or a.out
Two processes can be associated with same program,
are considered separate execution sequences.
E.g several user ---runs different copies of mail-
program
User---runs different copies of web browser
6
6/27/2019
Process in Memory
7
6/27/2019
How process is in Memory
8 6/27/2019
Process State
As a process executes, it changes state
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
terminated: The process has finished execution
9
6/27/2019
Diagram of Process State
Process Control Block (PCB)
Information associated with each process
(also called task control block)
Process state – new, running, waiting, etc
Program counter – location(address) of
instruction to next execute
CPU registers – contents of all process-
centric registers
CPU scheduling information- priorities,
scheduling queue pointers
Memory-management information –
memory allocated to the process
Accounting information – CPU used, clock
time elapsed since start, time limits
I/O status information – I/O devices
allocated to process, list of open files
Context Switch
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
12 6/27/2019
CPU Switch From Process to Process
Threads
So far, process has a single thread of execution e.g. word
processor
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.
On a system that supports threads, the PCB is expanded
to include information for each thread.
Process Scheduling
Definition:
If all processes are I/O bound, the ready queue will almost
always be empty, and the short-term scheduler will have little to
do.
If all processes are CPU bound, the I/O waiting queue will
almost always be empty, devices will go unused and again the
system will be unbalanced.
25 6/27/2019
Differences between schedulers
S.N Long Term Scheduler Short Term Scheduler Medium Term Scheduler
Speed is lesser than short Speed is fastest among other Speed is in between both short and
2
term scheduler. two. long term scheduler.
It controls the degree of It provides lesser control over It reduces the degree of
3
multiprogramming. degree of multiprogramming. multiprogramming.
It is almost absent or
It is also minimal in time
4 minimal in time sharing It is a part of Time sharing systems.
sharing system.
system.
emacs tcsch
ps
pid = 9204 pid = 4005
pid = 9298
Process Creation (Cont.)
Address space
Child duplicate of parent
Child has a program loaded into it(new process)
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.
Example of fork
32 6/27/2019
C Program Forking Separate Process
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
Process Termination
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
Threads (ch-4 Galvin)
Thread is a unit of CPU utilization.
A traditional (or heavyweight) process has a single thread of control. If
a process has multiple threads of control, it can perform more than one
task at a time.
It consists of
Thread ID
Program counter
Registers
Stack
38 6/27/2019
Process vs Threads
Benefits
Responsiveness: Multithreading an interactive application may
increases responsiveness to the user.
Many-to-One: maps many user level threads into one kernel level
thread.
45 6/27/2019
One-to-one Model
There is one to one relationship of user level thread to
the kernel level thread. This model provides more concurrency
than the many to one model. It also another thread to run when a
thread makes a blocking system call. It support multiple
thread to execute in parallel on microprocessors.
Disadvantage of this model is that creating user thread
requires the corresponding Kernel thread. OS/2, windows
NT and windows 2000 use one to one relationship model.
Many-to-Many Model
In this model, many user level threads multiplexes to the
Kernel thread of smaller or equal numbers. The number of
Kernel threads may be specific to either a particular application or
a particular machine.
Following diagram shows the many to many model. In this model,
developers can create as many user threads as necessary and the
corresponding Kernel threads can run in parallels on a
multiprocessor.
Threading Issues
Semantics of fork() and exec() system calls.
In a multithreaded program, when a fork() system call is executed by a thread,
does the new process duplicate all the threads or is the new process single
threaded.
Depends on the implementation and application
The exec () system call works the same way we saw earlier – i.e, it replaces the
the entire process including all threads
Synchronous signals: e.g. signals delivered to the same process that performed the
operation causing the signal. For example, division by zero, illegal memory access
Asynchronous signals: signals generated by an event external to the process are received
by the process asynchronously. For example, <control> <C>.
Signal handlers:
Default signal handlers provided by the OS kernel
User-defined signal handlers
When a request for a service arrives, the service is passed to one of the
threads waiting in the pool.
After the request is serviced, the thread is returned to the pool awaiting
more work
If a request arrives and there are no threads awaiting in the queue, the
server waits until a thread becomes free
57 6/27/2019
Shortest-Job-First (SJF) Scheduling
Associate with each process the length of its next CPU
burst
Use these lengths to schedule the process with the shortest
time
SJF is optimal – gives minimum average waiting time for a
given set of processes
The difficulty is knowing the length of the next CPU request
Could ask the user
Two types
1. SJF (Non preemption)
2. SJF(Preemption) or Shortest Remaining Time First.
Determining Length of Next CPU Burst
Can only estimate the length – should be similar to the
previous one
Then pick process with shortest predicted next CPU burst
Commonly, α set to ½
Preemptive version called shortest-remaining-time-first
Example of Shortest-remaining-time-first or SJF with
Preemption
Now we add the concepts of varying arrival times and preemption to
the analysis
ProcessAarri Arrival TimeT Burst Time
P1 0 8
P2 1 4
P3 2 9
P4 3 5
Preemptive SJF Gantt Chart
P1 P2 P4 P1 P3
0 1 5 10 17 26
64 6/27/2019