Chapter 2 Part I Process Management
Chapter 2 Part I Process Management
process management
_________
Process Concept
Process
Also called task
The entity that can be assigned to and executed on a processor
A unit of activity characterized by a single sequential thread of
execution, a current state, and an associated set of system
resources
a program in execution; process execution must progress in
sequential fashion
Can be traced
• list the sequence of instructions that execute
2
Contd.
A process has the following:
In reality, of course, the real CPU switches back and forth from process to
process.
A process is an activity of some kind which has a program, input, output, and a
state.
3
Process Concept…
Aprocess includes:
― program counter
― stack
― data section Process in memory
4
Program Vs Process
• Program
o It is sequence of instructions defined to
perform some task
o It is a passive entity
• Process
o It is a program in execution
o It is an instance of a program running on a
computer
o It is an active entity
o A processor performs the actions defined by a
process 1-5
Analogy
Real life example: Consider a computer scientist who is
baking a birthday cake for her daughter and who is
interrupted by her daughter’s bleeding accident
• Sequence of actions
– Bringing ingredients i.e. flour, sugar, eggs, etc
– Placing the mixture into the oven
– Following the baking processes
– Hearing a cry and analyzing it to be because of bleeding
– Recording baking processes state and switching to
provide first aid service
– Providing first aid service
– Coming back and resuming the baking process
1-6
Contd.
Analysis
– Processes Baking Cake First Aid
– Processor Comp Scientist Comp Scientist
– Program Recipe First Aid Book
– Input Ingredients First Aid Kit
– Output Cake First Aid Service
– States Running, Idle Running, Idle
1-7
Two-State Process Model
• Process may be in one of the
two states
– Running
– Not-running
• Ready to execute
• Blocked, waiting for I/O
8
Process States
As a process executes, it changes state
1. New: A process just created but not yet admitted to the pool
of executable processes by the operating system
A code for the process is not yet loaded to memory.
Ready Exit
Blocked Exit
The system call tells the operating system to create a new process and
System initialization.
te s
d Sta
e
e nd
u s p
: S
. 2 -9
F ig 19
Operating System Control Structures
Information about the current status of each process and resource must
be kept
Tables are constructed for each entity the operating system manages.
1. Memory Tables:- maintains information about
Allocation of main memory to processes
Allocation of secondary memory to processes
Protection attributes for access to shared memory regions
Information needed to manage virtual memory
2. I/O Tables:- Maintains information about the
I/O device available or assigned
Status of I/O operation
Location in main memory being used as the source or destination of
the I/O transfer 20
Contd.
3. File Tables
Existence of files
Location on secondary memory
Current Status
Attributes
Sometimes this information is maintained by a file-management system
4. Process Table
Where process is located
Attributes necessary for its management
Process ID
Process state
Location in memory
21
Process Location
• Process includes set of programs to be executed
Data locations for local and global variables
Stack
Process image
Collection of program, data, stack, and attributes 22
Contd.
represent a process.
The OS groups all information that needs about particular process in the PCB.
When more processes are there in the ready state than the number
run first.
The part of the operating system that makes the choice is called the
28
When to Schedule?
There are a variety of situations in which scheduling may occur.
First, scheduling is absolutely required on two occasions:
When a process exits.
In each of these cases the process that had most recently been
running becomes unready, so another must be chosen to run next.
There are three other occasions when scheduling is usually done.
When a new process is created.
When an I/O interrupt occurs.
When a clock interrupt occurs.
29
Process Scheduling Queues
o The objective of multi-programming
31
A Queuing Discipline
33
Schedulers
o A process in a job-queue is selected in some fashion
and assigned to memory/CPU.
o The selection process is carried out by a scheduler.
Schedulers are of three types:
1. Long-term(Job)scheduler :- selects which programs or
processes should be brought into the ready queue.
Controls degree of multiprogramming (the number of
programs in memory).
It may take long time
2. Medium-term(Emergency) scheduler:- selects which
job/process should be swapped out if system is
overloaded.
Sometimes it controls swapping programs from main
memory to disk based virtual memory.
34
3. Short-term(CPU) scheduler :- selects which process should
Long/Short-Term Scheduling
Short-
term
Long-
term
35
Long-Term Scheduling
Determines which programs are admitted to the system for processing.
Its primary objective is to provide a balanced mix of jobs, such as
processor-bound and I/O-bound processes.
Controls the degree of multiprogramming.
If the degree of multiprogramming is stable, then the average rate
of process creation must be equal to the average departure rate of
processes leaving the system.
On most systems the long-scheduler may be absent or minimal.
Time-sharing resources have no long-term scheduler.
When a process changes the state from new state to ready state, then
there is a long-term scheduler.
36
Short-Term Scheduling
Also known as CPU scheduler or dispatcher (which is part of it).
Determines which process is going to be executed next.
Its main objective is increasing the system performance in accordance with
the chosen set of criteria.
It selects a process from among the processes that are ready to be
executed and allocates the CPU to one of them.
It is faster than long-term scheduler.
Is invoked on an event that may lead to choose another process for execution:
clock interrupts
I/O interrupts
operating system calls and traps
signals 37
Medium-Term Scheduling
Most processes are either I/O bound or CPU bound.
– I/O bound process spends more time doing I/O than it spends
doing computation.
– CPU bound process spends most of the time doing computation.
The long-term scheduler should select a good mix of I/O-bound and
CPU-bound processes.
Example:
– If all the processes are I/O bound, the ready queue will be empty
– If all the processes are CPU bound, the I/O queue will be empty, the
devices will go unutilized and the system will be imbalanced.
Best performance: best combination of CPU-bound and I/O-bound
process.
38
Contd.
Since CPU is much faster than an I/O operation even if the process mix
done by the LT scheduler is good at the start of execution ;there is a
tendency for CPU to be idle(no one is ready, all are blocked)
For this reason some OS introduced a Medium-term scheduler which
performs using Swapping technique.
― Key idea: it can be advantageous, to remove the processes from
the memory and reduce the multiprogramming.
Swapping:- removal of process from main memory to disk to improve
the performance.
At some later time, the process can be reintroduced into main
memory and its execution can be continued when it left off.
Swapping improves the process mix (I/O and CPU), when main memory
is unavailable.
MT scheduling reduces the degree of multiprogramming.
Schematic View of Swapping
40
Swapping Example
41
Addition of Medium Term Scheduling
Medium-
term
Short-
term
Long-
term
42
Dynamics of Swapping
1. Backing store – fast disk large enough to accommodate copies of
all memory images for all users; must provide direct access to
these memory images.
45
Process Scheduling
A scheduler starts to work by assigning a new process to the
processor from the ready queue
Once a process is allocated CPU, the following events may occur
A process could issue an I/O request
Short Term
Scheduler
Long Term
Scheduler
Interrupt Handler
47
Dispatcher (short-term scheduler)
to another.
time.
49
When to Switch a Process?
• A process switch may occur whenever the OS has gained control of CPU.
i.e., when:
1. Supervisor Call
• explicit request by the program (example: file open)
• the process will probably be blocked.
2. Trap
• an error resulted from the last instruction
• it may cause the process to be moved to terminated state.
3. Interrupt
• the cause is external to the execution of the current instruction
• control is transferred to Interrupt Handler.
50
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.
while switching. 51
Context switch between processes (1)
52
Context switch between processes (2)
53
Steps in Context Switch
1. Save context of a process including program counter and other
registers.
2. Update the PCB of the running process with its new state and
55
Operations on processes
Process creation
Process termination
56
A. Process Creation
When an operating system is booted, often several processes
(either foreground or/and back ground processes ) are created.
Foreground processes, are processes that interact with (human)
users and perform work for them.
Background processes, which are not associated with particular
users, but instead have some specific function.
Processes that stay in the background to handle some activity
such as web pages, printing, and so on are called daemons.
In addition to the processes created at boot time, new processes
can be created afterward as well.
57
Operations on processes(cont)
o A process can be created when
—User logs on
—There is a need to provide a service such as printing
— a batch job is submitted
—One process creates another one
o A system call is used to create process.
—for a created process:
A unique id is assigned Create or expand other data
Space is allocated. structures
PCB is initialized. Ex: maintain an accounting file
appropriate linkages setup is made
Ex: add new process to linked
list used for scheduling queue
62
UNIX: fork() system call
Parent Child
void main(void) void main(void)
{ PID=3456 {
pid=fork(); pid=fork();PID=0
if (pid==0) if (pid==0)
ChildProcess(); ChildProcess();
else else
ParentProcess();
} ParentProcess();
} }
Void ChildProcess(void) }
{ Void ChildProcess(void)
} {
Void ParentProcess(void) }
{ Void ParentProcess(void)
} { 63
Processes Tree on a UNIX System
64
B Process Termination:
A process is terminated when
Batch job issues Halt instruction
User logs off
User quit an application
Error and fault conditions occurs
• Process executes last statement and asks the operating system to
decide it (exit).
Output data from child to parent (via wait).
Process’ resources are deallocated by operating system.
• Parent may terminate the execution of children processes (abort).
Child has exceeded allocated resources.
Task assigned to child is no longer required.
Parent is exiting.
• Operating system does not allow child to continue if its parent
terminates.
• Cascading termination.
65
Reasons for Process Termination
•Normal completion •Invalid instruction
•Time limit exceeded –happens when try to execute
•Memory unavailable data
•Privileged instruction
•Bounds violation Data misuse
•I/O failure
66
Cooperating Processes
o processes can be independent or cooperating processes.
o Independent process cannot affect or be affected by the execution
of another process; the opposite is true in cooperating processes.
o Cooperating processes need inter-process communication (IPC)
o Advantages of process cooperation
i. Information sharing
ii. Computation speed-up
• Break into several subtasks and run in parallel
iii. Modularity
• Constructing the system in modular fashion.
iv. Convenience
• User will have many tasks to work in parallel
– Editing, compiling, printing
67
Cooperating of Processes can be by:
1. Sharing Memory:- Processes use and update shared data such as
shared variables, memory, files, and databases.
Writing must be mutually exclusive to prevent a race condition
leading to inconsistent data views.
Critical sections are used to provide this data integrity.
A process requiring the critical section must not be delayed
indefinitely; no deadlock or starvation.
2. Message Passing(Communication):- Message passing systems
require the synchronization and communication between the two
processes.
Possible to have deadlock :-each process waiting for a message
from the other process.
Possible to have starvation:- two processes sending a message to
each other while another process waits for a message. 68
Issues in Cooperating Processes
69
Communication Models
Producer-Consumer Problem:
Two processes share a common, fixed-size buffer. One of them, the
producer, puts information into the buffer, and the other one, the
consumer, takes it out.
We need a buffer to hold items that are produced and later consumed:
unbounded-buffer:- places no practical limit on the size of the
buffer.
• Producer can produce any number of items.
• Consumer may have to wait
bounded-buffer:- assumes that there is a fixed buffer size.
Trouble arises when the producer wants to put a new item in the
buffer, but it is already full.
The solution is for the producer to go to sleep, to be awakened
when the consumer has removed one or more items.
Similarly, if the consumer wants to remove an item from the buffer
and sees that the buffer is empty, it goes to sleep until the producer
71
Producer-Consumer
Producer Consumer
Shared Memory
• Implementation of Producer-consumer
problem using shared memory
72
//Producer //Consumer
#define N 100 void consumer(void)
int count = 0; {
void producer(void) int item;
{ while (TRUE)
int item; {
while (TRUE) if (count == 0)
{ sleep();
item = produce_item(); item=remove_item(item);
if (count == N) count = count -1;
sleep(); if (count == 0)
insert_item(item); wakeup(producer);
count = count + 1; consume_item(item);
if (count == 1) }
wakeup(consumer); }
}
}
73
Inter-process Communication (IPC): Message Passing
o Message system – processes communicate with each other
without resorting to shared variables.
o IPC facility provides two operations:
– send(message) – message size fixed or variable
– receive(message)
o If P and Q wish to communicate, they need to:
– establish a communication link between them
– exchange messages via send/receive
o Os facilitates such communication
o Implementation of communication link may be
– physical (e.g., shared memory, hardware bus)
– logical (e.g., logical properties)
74
Implementation Questions
75
Message passing systems
• Direct or Indirect communication
• Synchronous or asynchronous communication
• Automatic or explicit buffering
76
Direct Communication
o Processes must name each other explicitly:
– send (P, message) – send a message to process P
– receive(Q, message) – receive a message from process Q
o Properties of communication link
– Links are established automatically.
– A link is associated with exactly one pair of communicating processes.
– Between each pair there exists exactly one link.
– The link may be unidirectional, but is usually bi-directional.
o This exhibits both symmetry and asymmetry in addressing
– Symmetry: Both the sender and the receiver processes must name the
other to communicate.
– Asymmetry: Only sender names the recipient, the recipient is not
required to name the sender.
• The send and receive primitives are as follows.
– Send (P, message)– send a message to process P.
– Receive(id, message)– receive a message from any process.
o Disadvantages: Changing a name of the process creates problems.
77
Indirect Communication
o The messages are sent and received from mailboxes (also
referred to as ports).
o A mailbox is an object where
– Process can place messages
– Process can remove messages.
o Two processes can communicate only if they have a shared
mailbox.
o Operations
– create a new mailbox, each mailbox has a unique id.
– send and receive messages through mailbox
– destroy a mailbox
o Primitives are defined as:
send(A, message) – send a message to mailbox A
receive(A, message) – receive a message from mailbox A
78
Indirect Communication
o Mailbox sharing
o Solutions
– Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.
o Properties of a link:
o OS provides a facility
– To create a mailbox
83
Sockets
o A socket is defined as an endpoint for communication.
o A pair of processes communicating over a network
employees a pair of sockets– one for each process.
o Socket: Concatenation of IP address and port
The socket 161.25.19.8:1625 refers to port 1625 on host
161.25.19.8
o Servers implementing specific services listen to well-known
ports
– telnet server listens to port 80
– ftp server listens to port 21
– http server listens to port 80.
o The ports less than 1024 are used for standard services.
o The port for socket is an arbitrary number greater than1024 .
o Communication exists between a pair of sockets. 84
Socket Communication
85
Types
• Java provides three types of sockets
1. Connection oriented(TCP) sockets
2. Connectionless (datagram) sockets
3. Multicast sockets-a type of datagram socket
that allows to send data to multiple recipients
• Problem of Socket type communication.
– it is primitive type and communicated data is not
structured. But it’s common and efficient
86
Remote Procedure Calls
87
Execution of RPC
88
Remote Method Invocation
89
Marshalling Parameters
90
RPC Vs RMI
Two subtle differences
oRPCs support procedural programming, only procedures or
functions can be called. But RMIs are object based.
o The parameters to remote procedures are ordinary data
structures in RPCs; with RMIs it is possible to pass objects as
a parameter.