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

Chapter 2 Part I Process Management

Chapter 2 discusses the concept of processes in operating systems, defining a process as a program in execution characterized by a sequential thread of execution, state, and resources. It outlines the different states a process can be in, such as running, waiting, and terminated, and describes how processes are created and terminated. Additionally, the chapter covers process scheduling, including the roles of long-term, medium-term, and short-term schedulers in managing process execution and resource allocation.

Uploaded by

fikadu.meu.edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Chapter 2 Part I Process Management

Chapter 2 discusses the concept of processes in operating systems, defining a process as a program in execution characterized by a sequential thread of execution, state, and resources. It outlines the different states a process can be in, such as running, waiting, and terminated, and describes how processes are created and terminated. Additionally, the chapter covers process scheduling, including the roles of long-term, medium-term, and short-term schedulers in managing process execution and resource allocation.

Uploaded by

fikadu.meu.edu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 91

Chapter 2

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:

1. The code (text section)

2. Program counter and contents of the registers

3. A data section for global variables.

 Conceptually, each process has its own virtual CPU.

 In reality, of course, the real CPU switches back and forth from process to

process.

 This rapid switching back and forth is called multiprogramming.

 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

Figure 2-1: 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.

2. Running: Instructions are being executed


3. Waiting(Blocked): The process is waiting for some event to
occur.
4. Ready: A process that is not currently executing but that is ready
to be executed as soon as the operating system dispatches it
5. Terminated(Exited): The process has finished execution
9
Process State Change
 The main process state transitions are
 Null  New
 It occurs when a new process is created to execute a program
 New  Ready
 It occurs when the operating system is prepared to take on additional
process and a new process is admitted
 Ready  Running
 It occurs when the operating system chooses one of the processes in
the ready state for running
 The process is called dispatching and it is caused by the dispatcher
(scheduler)
Dispatch (process name) 10
Contd.
 Running  Blocked
 It occurs when a process requests something for which it must wait
 Blocked  Ready
 It occurs when the event for which a process has been waiting
occurs
Wakeup (process name)
 Running  Ready
 It occurs when a process is preempted by the operating system
 Examples
 When time is expired
 When a blocked process with higher priority is moved to ready state
Timeout (process name)
11
Contd.
 Running  Exit

 It occurs when a currently running process is terminated

» Halt (process name) or Abort (process name)

 Ready  Exit

 Blocked  Exit

 It occurs due to two most common reasons

• When a parent process terminates a child process

• When a parent process is terminated


 The next slide shows a five state model of process state transition
12
A Five-State Model
• Running • New
• Ready • Exit
• Blocked

Figure 2-4: A Five state Process Model


13
Creating a Process
 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.
14
Contd.
 A new process is created by having an existing process (a running user

process, a system process invoked from the keyboard or mouse, or a

batch manager process) execute a process creation system call.

 The system call tells the operating system to create a new process and

indicates, directly or indirectly, which program to run in it.

 There are four principal events that cause processes to be created:

 System initialization.

 Execution of a process creation system call by a running process.

 A user request to create a new process.


15
 Initiation of a batch job.
Process Termination
 After a process has been created, it starts running and does whatever

its job is.

 Processes terminate usually due to one of the following conditions:

1. Normal exit (voluntary).

2. Error exit (voluntary).

3. Fatal error (involuntary).

4. Killed by another process (involuntary).

 Most processes terminate because they have done their work. 16


Suspended Processes
 In multiprogramming systems
 memory can hold multiple processes at a time.
 the processor moves to another process when one process is
blocked(maximizes CPU utilization).
 But processor is faster than I/O so all processes could be waiting for
I/O(CPU becomes idle)
 So what should be done?
– Have large sized memory
o problem: cost and larger memory does not mean that large
number of processes in memory rather a large process in memory.
– Swap blocked/suspended processes to disk to free up more memory
 Blocked state becomes suspend state when swapped to disk
 Two new states
o Blocked, suspend
o Ready, suspend
17
Contd.
 There are four states based on whether a process is waiting on an
event (blocked or not) and whether a process has been swapped out
of main memory (suspended or not).
1. Ready: The process is in main memory and available for
execution.
2. Blocked: The process is in main memory and awaiting an event.
3. Blocked/Suspend: The process is in secondary memory and
awaiting an event.
4. Ready/Suspend: The process is in secondary memory but is
available for execution as soon as it is loaded into main memory.
 The next two slides depicts the seven process transition states 18
Contd.

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

 Any defined constants

 Stack

Process control block


 Collection of attributes, includes
 Process identification(using identifiers eg. Process id)

 Processor State Information(PC value, Condition codes, etc)

 Process Control Information

– eg. Memory management information

Process image
 Collection of program, data, stack, and attributes 22
Contd.

Figure 2-11 General structure of Operating system Control Tables


23
Process Control Block (PCB)
 A Process Control Block (PCB) is the data structure that the OS uses to

represent a process.

 The OS groups all information that needs about particular process in the PCB.

PCB Contains information associated with each process.

1. Process state:- can be new, ready, running, waiting,


terminated.

2. Pointer:- points to another process control block.


 Pointer is used for maintaining the scheduling list.

3. Program counter:- indicates the address of the next


instruction to be executed.

4. CPU registers:- includes general-purpose registers, stack


Pointers, index registers and accumulators.
24
Contd.

5. Memory-management information:- includes the value


of base and limit register.
 The information is useful for reallocating the memory
when the process terminates.
6. CPU scheduling information:- includes the CPU
scheduling information for each and every process(Eg.
process priorities, pointers to scheduling queues, etc.)
7. Accounting information:- includes the amount of CPU and
real time used, time limits, job or process numbers, account
numbers etc.
8. I/O status information:- includes list of opened files
9. Event information:- for a process in the blocked (wait)
25
Contd.

Figure 2-12: Process Control Block 26


CPU Switch From Process to Process

Figure 2-13:CPU switching/context switching/


27
Process Scheduling
 When a computer is multiprogrammed, it frequently has multiple

processes competing for the CPU at the same time.

 When more processes are there in the ready state than the number

of available CPUs, the operating system must decide which process to

run first.

 The part of the operating system that makes the choice is called the

scheduler and the algorithm it uses is called the scheduling algorithm.

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.

 When a process blocks on I/O.

 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

– To have some process running at all times.


o Timesharing: Switch the CPU frequently that users can interact the
program while it is running.
o If there are many processes, the rest have to wait until CPU is free.

o Scheduling is to decide which process to execute and when.

o Scheduling queues:-Several queues used for scheduling:

a) Job queue:- holds sets of all processes in the system.


 When the process enters into the system then it is put
into a job queue
30
Contd.
b) Ready queue:- set of all processes residing in main memory,
ready and waiting to execute.
 A newly arrived process is put into this queue.

 Processes are waiting in this queue for allocating the CPU.


Once the CPU is assigned to the process, then the process will
be executed.

c) Device queues:- set of processes waiting for an I/O device.


• Each device has its own queue.
o Process migrates between the various queues during its life time.

31
A Queuing Discipline

 When event n occurs, the corresponding process is


moved into the ready queue
32
Ready Queue and various I/O Device Queues

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.

2. Roll out, roll in – swapping variant used for priority-based


scheduling algorithms; lower-priority process is swapped out so
higher-priority process can be loaded and executed.
 Major part of swap time is transfer time; total transfer time is
directly proportional to the amount of memory swapped.
 Modified versions of swapping are found on many systems (i.e.,
UNIX, Linux, and Windows).
43
Support for Swapping

 The OS may need to suspend some processes, i.e., to swap

them out to disk and then swap them back in.

 We add 2 new states:

1. Blocked Suspend:- blocked processes which have been

swapped out to disk.

2. Ready Suspend:- ready processes which have been

swapped out to disk.


44
Queuing Diagram for Scheduling

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

 A process could create a new process

 The process could be removed forcibly from CPU, as a result of


an interrupt.
 When process terminates, it is removed from all queues.

PCB and its other resources are de-allocated


46
Reasons for Process Switch

Short Term
Scheduler

Medium Term Short Term


Scheduler Scheduler

Long Term
Scheduler

Interrupt Handler

47
Dispatcher (short-term scheduler)

 Is an OS program that moves the processor from one process

to another.

 It prevents a single process from monopolizing processor

time.

 It decides who goes next according to a scheduling algorithm.

 The CPU will always execute instructions from the dispatcher

while switching from process A to process B.


48
Process Switch

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.

 This is called context switch.

 Context of a process represented in the PCB.

 The time it takes is dependent on hardware support.

 Context-switch time is overhead; the system does no useful work

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

other associate information.

3. Move PCB to appropriate queue – ready, blocked,

4. Select another process for execution.

5. Update PCB of the selected process.

6. Restore CPU context from that of the selected process.


54
Mode Switch
 It may happen that an interrupt does not produce a context
switch.
 The control can just return to the interrupted program.
 Then only the processor state information needs to be saved on
stack.
 This is called mode switch (user to kernel mode when going into
Interrupt Handler).
 Less overhead: no need to update the PCB like for context
switch.

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

– The creating process is called parent process.


– Parent process creates children processes, which, in turn create other processes,
forming a tree of processes.
 In UNIX pagedaemon, swapper, and init are root process. Users are children of
init process. 58
Operations on processes(cont)
• A When a process creates a new process,
1. Resource sharing possibilities.
• Parent and children share all resources.
• Children share subset of parent’s resources.
• Parent and child share no resources.
2. Execution possibilities
• Parent and children execute concurrently.
• Parent waits until children terminate.
• 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. Next 59
Process Creation (Cont.)
– The new process is a copy of the original process.
– The exec system call is used after a fork by one of the two processes
to replace the process memory space with a new program.
• DEC VMS
– Creates a new process, loads a specified program into that process,
and starts it running.
• WINDOWS NT supports both models:
– Parent address space can be duplicated or
– parent can specify the name of a program for the OS to load into the
address space of the new process.
60
UNIX: fork() system call
• fork() is used to create processes. It takes no arguments and returns a
process ID.
• fork() creates a new process which becomes the child process of the
caller.
• After a new process is created, both processes will execute the next
instruction following the fork() system call.
• checking the return value, one can distinguish the parent from the
child.
• fork()
– Returns a negative value, if the creation is unsuccessful.
– Returns 0 to the newly created child process.
– Returns positive value to the parent.
• Process ID is of type pit_t defined in sys/types.h
• getpid() can be used to retrieve the process ID.
• The new process consists of a copy of address space of the original
process. 61
UNIX: fork() system call
• If the fork() is executed successfully, Unix will
– Make two identical copies of address spaces; one for the
parent and one for the child.
– Both processes start their execution at the next statement
after the fork().

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

•Protection error  Operating system


–example write to read-only file intervention
 Such as when deadlock
•Arithmetic error occurs
•Time overrun  Parent terminates so child
–process waited longer than a processes terminate
specified maximum for an  Parent request
event.

•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

 There are three issues in cooperating processes:

1. How one process can pass information to another?

2. How to make sure that two or more processes do not get

into each other's way when engaging in critical activities?

3. The proper sequencing when dependencies are present: if

process A produces data and process B prints it, B has to wait

until A has produced some data before starting to print.

69
Communication Models

Msg Passing Shared Memory


70
Inter-process Communication (IPC): shared Memory

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

o How are links established?


o Can a link be associated with more than two processes?
o How many links can there be between every pair of
communicating processes?
o What is the capacity of a link?
--- (How much is the buffer space ?)
o Is the size of a message that the link can accommodate fixed
or variable?
o Is a link unidirectional or bi-directional?

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

– P1, P2, and P3 share mailbox A.

– P1, sends; P2 and P3 receive.

– Who gets a message ?

o Solutions

– Allow a link to be associated with at most two processes.

– Allow only one process at a time to execute a receive operation.

– Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

o Properties of a link:

– A link is established if they have a shared mailbox

– A link may be associated with more than two boxes.

– Between a pair of processes they may be number of links

– A link may be either unidirectional or bi-directional.

o OS provides a facility

– To create a mailbox

– Send and receive messages through mailbox

– To destroy a mail box.

o The process that creates mailbox is a owner of that mailbox 79


Synchronous or asynchronous
o Message passing may be either blocking or non-blocking.
o Blocking is considered synchronous
o Non-blocking is considered asynchronous
o send and receive primitives may be either blocking or
non-blocking.
– Blocking send: The sending process is blocked until the
sender receives a message from the receiver(via direct/by
the mailbox.
– Non-blocking send: The sending process sends the message
and resumes operation.
– Blocking receive: The receiver blocks until a message is
available.
– Non-blocking receive: The receiver receives either a valid
message or a null.
80
Automatic and explicit buffering
o Whether communication is direct or indirect messages exchanged by
communicating processes reside in a temporary queue.
o A link has some capacity that determines the number of messages that can
reside in it temporarily. Basically such queue can be implemented in three
ways.
1. Zero capacity – 0 messages:-Sender must wait for receiver (rendezvous).
2. Bounded capacity – finite length of n messages
-Sender must wait if link full.
3. Unbounded capacity – infinite length
Sender never waits.
o In non-zero capacity cases a process does not know whether a message has
arrived after the send operation.
o The sender must communicate explicitly with receiver to find out whether the
later received the message.
o Example: Suppose P sends a message to Q and executes only after the message
has arrived.
o Process P:
 send (Q. message) : send message to process Q
 receive(Q,message) : Receive message from process Q
o Process Q
 Receive(P,message)
 Send(P,”ack”)
81
Exception conditions
• When a failure occurs error recovery (exception handling) must
take place.
 Process termination
• A sender or receiver process may terminate before a message is
processed. (may be blocked forever)
• A system will terminate the other process or notify it.
 Lost messages
• Messages may be lost over a network
• Timeouts; restarts.
 Scrambled messages
• Message may be scrambled on the way due to noise
• The OS will retransmit the message
• Error-checking codes (parity check) are used.
82
Client-Server Communication
• Sockets
• Remote Procedure Calls
• Remote Method Invocation (Java)

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

• Remote procedure call (RPC) abstracts procedure calls


between processes on networked systems.
• Stubs – client-side proxy for the actual procedure on the
server.
• The client-side stub locates the server and marshals the
parameters.
– Marshalling: Parameters must be marshaled into a standard
representation.
• The server-side stub receives this message,
unpacks the marshaled parameters, and
performs the procedure on the server.

87
Execution of RPC

88
Remote Method Invocation

o Remote Method Invocation (RMI) is a Java mechanism


similar to RPCs.
o RMI allows a Java program on one machine to invoke a
method on a remote object.

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.

END OF CHAPTER 2(part I)


PROCESS
MANAGMENT 91

You might also like