You are on page 1of 74

Chapter 4 Processes

os4

Outline
Process Concept Process Scheduling Operations on Processes Cooperating Processes Interprocess Communication Communication in Client-Server Systems
os4 2

roc ss o c
 

t( )
x cutio (i formally).

roc ss (job): a rogram i

th u it of work i most syst ms A syst m co sists of a coll ctio of roc ss s (syst m roc ss s a d us r roc ss s) x cuti g co curr tly, with U multi l x d amo g th m

A roc ss is mor tha th rogram cod (k ow as th t xt s ctio ). It also i clud s: (mor formal d fi itio )
   

curr t activity ( rogram cou t r, co t t of r gist rs) stack co tai i g t m orary data ( .g., aram t rs) data s ctio co tai i g global variabl s a s t of associat d r sourc s.
os4 3

roc ss o c

t( )

rogram cou t r: xt i structio to b x cut d A rogram is a assiv tity but a roc ss is a activ tity Two roc ss s may b associat d with th sam rogram, th y ar co sid r d as s arat x cutio s qu c s.


Th y hav th sam t xt s ctio , but th ir data s ctio will vary.


.g., s v ral us rs may ru rogram. i g th co i s of th mail/ ditor

A roc ss may s aw ma y roc ss s as it ru s.


os4 4

Process State
Process state
  

new: the process is being created running: instructions are being executed waiting: the process is waiting for some event to occur (such as an I/O completion or reception of a signal) ready: The process is waiting to be assigned to a processor terminated: The process has finished execution

Only one process is running on any processor at any instant. However, many processes may be ready or waiting.
os4 5

Diagram of Process State


new admitted interrupt terminated exit ready running

I/O or event completion

scheduler dispatch waiting

I/O or event wait

os4

Process Control Block (PCB)


Each process control block (task control block) represents a process. It includes
process state (N, R, T, W, Run)  program counter  CPU registers  CPU scheduling information (e.g., priority)  memory-management information (e.g., base/limit register)  accounting information  I/O status information

os4

to next PCB

pointer

process state

process number program counter registers memory limits list of open files

. . .
7

CPU Switching
process P0 executing . . . . . . . . . . . . . . idle . . . . . . . . executing operating system interrupt or system call save state into PCB0 . . . reload state from PCB1 interrupt or system call save state into PCB1 . . . reload state from PCB0
os4 8

process P1

. . . . . . idle . . executing . . . . . idle .

Threads
Thread


Sometimes is called a lightweight process: is a basic unit of CPU utilization; it comprises a thread ID, a program counter, a register set, and a stack.  It shares with other threads belonging to the same process its code section, data section, and other OS resources, such as open files and signals.  A web browser might have one thread display images or text while another thread retrieves data from the network.  Modern OS has extend the process concept to allow a process to have multiple threads of execution.
os4 9

Process Scheduling
jecti e of ulti rogr ing: P runs rocess t ll ti es to i ize P utiliz tion. jecti e of ti e sh ring: s itch P fre uentl such th t users c n inter ct ith e ch rogr hile it is running. For uni- rocessor s ste , there is onl one running rocess. he rest should it until P free nd is rescheduled.
os4 10

Scheduling Queues
Scheduling queues
 

job queue: containing all processes ready queue


 usually, stored as a linked list

device queues: each containing processes waiting for it

Queuing diagram: a common representation for process scheduling


  

rectangle: queue circle: server for a queue arrow: flow


os4 11

PCB7

PCB2
registers

ready queue

head tail head tail

registers

mag tape unit 1 disk unit 0

PCB3

PCB14
registers

PCB8
registers

head tail

registers

PCB6

terminal unit 0

head tail

registers

Ready queue and various I/O device queues


os4 12

Queuing-diagram Representation of Process Scheduling


ready queue CPU
each device has one

I/O

I/O queue

I/O request

time slice expired Child terminates child executes interrupt occurs


os4

fork a child wait for an INT

13

Schedulers (1)
Lo -ter Scheduler (jo Scheduler): selecti rocesses fro the jo ool d lo ds the i to e or for e ecutio .  ecute less fre ue tl (e. ., o ce se er l i utes)  o trol the de ree of ulti ro r i  Select ood rocess i of I/ - ou d rocesses d - ou d rocesses  So e s ste , such s ti e-sh ri s ste , do ot h e. heir ulti ro r i de ree re co trolled h rd re li it tio (e. ., # of ter i ls) or o the self- djusti ture of users.
os4 14

Schedulers (2):

Short-term scheduler

(CPU scheduler) selecting one of the ready processes, and allocates the CPU to it.
 

Execute quite frequently (e.g., once per 100ms) must be very fast
e.g., if it takes 10 ms, then 10/(100+10) percent of CPU is wasted.
long-term ready queue short-term CPU end

I/O

I/O queue(s)
os4 15

Schedulers (3):
(Swapping)

Medium-term Scheduler

 swap out: removing processes from memory to reduce


the degree of multiprogramming.  swap in: reintroducing swap-out processes into memory  for improving process mix or for memory not enough
medium-term swap in partially executed swapped-out processes ready queue I/O
os4

swap out

CPU I/O waiting


16

Schedulers (4)
co te t s itch: s i the st te of the old rocess d lo di the s ed st te of the e rocess.
 

his is ure o erhe d s itch ti e ( out 1~1 s) de e ds o  e or s eed  u er of re isters  e iste ce of s eci l i structio s
e. ., si le i structio to s e/lo d ll re isters

 h rd
e. .,

re su

ort

ulti le sets of re isters


os4 17

Operation on Processes: Creation and Termination


Process creation


A parent process creates children processes via a system call (such as fork in UNIX) A child process may obtain resource by
1. ask new ones from OS, or 2. use a subset of its parents
:prevent system overloading (by too many sub-processes) P2 P1

P3

P4

P4
18

P5

process graph (a tree)


os4

Two possibilities of execution


 parent execute concurrently  parent waits until some/all children terminate.

Two possibilities of the address space of the new process


 The child process is a duplicate of the parent process. (communication via sharing variables)  The child process has a program loaded into it. (communication via message passing)

e.g.,

UNIX : both (fork, execve) DEC VMS: load


Windows/NT: both
os4 19

Process termination


two termination system calls


 exit (parent can use wait to get the output)  abort: usually only the parent can abort a

child (except OS). The reasons may be:




the child has exceeded its usage of some resources  the assigned task is no longer required  the parent is exiting, and OS does not allow its children to continue cascading termination (initiated by OS): a terminated process causes all its children (and their children in turn) to terminate.
os4 20

Cooperating Processes
Two type of processes


independent process: not affect or be affected by others cooperating processes


 reasons:
   

information sharing (a shared file) computation speedup (CPUs, I/O channels) modularity (construct a system in a modular fashion) convenience (A user can performs several tasks at one time (editing, printing, compiling))

 need communication/synchronization mechanisms


os4 21

An Example: The producer-consumer problem




producer produces items (into buffer) for consumer e.g., print program printer driver compiler assembler loader unbounded-buffer problem
 producer: no wait  consumer: wait when buffer is empty

bounded-buffer problem
 producer: wait when buffer is full  consumer: wait when buffer is empty
os4 22

A shared-memory solution for synchronization




implement the buffer as a circular array var buffer: array[1..n] of item; in, out: 0..n-1; (initialized to 0)

   

next free: in first full: out empty: in=out at most n-1 items out in

os4

23

Shared-memory solution for the producer-consumer problem


Import java.util.*; Public class BoundedBuffer { public BoundedBuffer () { // Buffer in initially empty count = 0; in = 0; out = 0; buffer = new Object[BU ER_SIZE]; // producer calls this method public void enter (Object item) { // in next slice} // consumer calls this method public Object remove() { // in next slice} public static final int NAP_TIME = 5; private static final int BU ER_SIZE = 5; private private private private

volatile int count; volatile int in; //points to the next free position volatile int out; //points to the next free position Object [] buffer;

os4

24

public void enter(Object item) { while (count == BUFFER_SIZE) ; // do nothing // add an item to the buffer ++count; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; if (count == BUFFER_SIZE) system.out.println(Producer Entered + item + Buffer FULL); else system.out.println(Producer Entered + item + Buffer Size + count); }

public Object remove () { Object item; while (count == 0) ; // do nothing // remove an item from the buffer - -count; item == buffer[out]; out = (out + 1) % BUFFER_SIZE; if (count == 0) system.out.println(Consumer Consumed + item + Buffer EMPTY); else system.out.println(Consumer Consumed + item + Buffer Size + count); return item; }

enter()

os4

remove()

25

Inter-process Communication (IPC)


Two ways for communication
shared-memory (e.g., thread facility)  IPC (e.g., message-passing system)


An IPC provides a mechanism to allow processes to communicate and to synchronize their actions without sharing the same address space. An IPC is particularly useful in a distributed environment where communicating processes may reside on different computers connected with a network. chat on WWW. An IPC facility provides at least two operations: send(message) os4 26 receive(message)

Several methods for logically implementing a link and the send/receive operations:
    

Nami g

Direct or indirect communication Symmetric or asymmetric communication Automatic or explicit buffering Send by copy or send by reference ixed-sized or variable-sized messages

Bufferi g

os4

27

Naming: direct communication


Use process identities (symmetry in addressing)
send(P, message) receive(Q, message) identity

Asymmetry (receiving from any process)


send(P, message) receive(id, message)

Properties


from whom

 

A link is established automatically between every pair of processes. A link is associated with exactly two processes. Between each pair of processes, there exists exactly one link.
os4 28

The link may be unidirectional, but is usually bidirectional.

Example: producer processto the producer-consumer a solution The The consumer process problem
repeat ... produce an item in nextp ... send(consumer,nextp); until false; repeat receive(producer,nextc); ... consume the item in nextc ... until false;

: limited modularity: if the name of a process is changed, all old names should be found. It is not easy for separate compilation. os4 29

Naming: indirect communication


Use mailboxes (or called ports, each has a unique id). send(A, message) receive(A, message) Properties of such a link:


A link is established between every pair of processes only if they have a shared mailbox. A link may be associated with more than two processes. Between each pair of communicating processes, there may be a number of different links, each corresponding to one mailbox. os4 30

The link may be either unidirectional or bidirectional.

One Problem: Which process, P2 or P3, will receive the message sent by P1?
send P1 (1) mailbox A (2) P3 receive P2 receive

Solutions:


Allow a link to be associated with at most two processes. At most one process at a time to execute a receive operation. The OS arbitrarily selects one (not both).

os4

31

Synchronization
Message passing may be either blocking or nonblocking and also as synchronous and asynchronous.


Blocking send: SP is blocked until the message is received by RP or by the mailbox. Nonblocking send: SP sends the message and resumes operation. Blocking receive: RP is blocked until the message is available. Nonblocking receive: RP receives a valid message or a null.
os4 32

Buffering
Capacity of a link: the number of messages that can reside (queued) temporarily.buffering No Three ways for implementation:


zero capacity: queue length = 0.


 The sender must wait until the recipient receives the message.  This synchronization is called a rendezvous.

bounded capacity: queue length = n.


 Sender should wait if queue is full.

unbounded capacity.
 The sender is never delayed.
os4

Automatic buffering

33

In nonzero-capacity cases, two processes communicate asynchronously. or asynchronous communication, one way to make sure (or to wait) that the receiver has received the sent message is as follows.
process P (sender)
send(Q, message) receive (Q, message)

process Q (receiver)

receive(P, message) send(P,acknowledgement")


os4 34

Production-Consumer Example Mailbox for message passing


The buffer is implemented using the java.util.Vector class (unbounded capacity) Note: both the send() and receive() are nonblocking A multi-thread version is discussed in Chapter 5.
import java.util.*; public class MessageQueue { public MessageQueue() { queue = new Vector (); } //This implements a nonblocking send public void send(Object item) { queue.addElement(item); } //This implements a nonblocking receive public void receive() { object item; if (queue.size() == 0) return null; else { item = queue.firstElelemt(); queue.removeElementAt(0); return item; } } private Vector queue; os4 35 }

MessageQueue mailBox; While (true) { Date message = new Date(); mailBox.send (message); }

MessageQueue mailBox; While (true) { Date message = (Date) mailBox.receive (); if (message != null) //consume the message }

The producer process

The consumer process


os4 36

An Example: Mach
Developed at Carnegie Mellon University Most communications are carried out by messages. Message are sent to and received from mailboxes, called ports. Even system calls are made by messages. When each task is created, two special mailboxes the Kernel mailbox and the Notify mailbox are also created.


The Kernel mailbox is used by the kernel to communication with the task. The kernel sends notification of event occurrences to the Notify port.
os4 37

Three system calls using message transfer


  

msg_send: sends a message to a mailbox msg_receive msg_rpc: Remote Procedure Calls (PRCs)

port_allocate: creates a new mailbox and allocates space for its queue of messages. The task that creates the mailbox is that mailboxs owner. Messages are copied into mailbox. Multiple messages from the same sender are queued in I O order. If the mailbox is full, the sending thread has 4 options:
   

Wait indefinitely until there is a room in the mailbox. Wait at most n milliseconds. Do not wait at all, but rather return immediately. Temporarily cache a message.
os4 38

The receive operation must specify from which mailbox or mailbox set to receive a message. port_status: returns of messages in a given mailbox. If no message is waiting to be received, the receiving thread may wait, wait at most n milliseconds, or not wait. The Mach message system attempts to avoid double copy operations by using virtual-memory management techniques. Mach maps the address space containing the senders message into the receivers address space.
os4 39

os4

40

Process Control Block (PCB)


Information associated with each process. Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information

os4

41

Process Control Block (PCB)

os4

42

CPU Switch rom Process to Process

os4

43

Process Scheduling Queues


Job queue set of all processes in the system. Ready queue set of all processes residing in main memory, ready and waiting to execute. Device queues set of processes waiting for an I/O device. Process migration between the various queues.

os4

44

Device Queues

os4

45

Representation of Process Scheduling

os4

46

Schedulers
ong-term scheduler (or job scheduler) selects which processes should be brought into the ready queue. Short-term scheduler (or CPU scheduler) selects which process should be executed next and allocates CPU.

os4

47

Addition of Medium Term Scheduling

os4

48

Schedulers (Cont.)
Short-term scheduler is invoked very frequently (milliseconds) (must be fast). ong-term scheduler is invoked very infrequently (seconds, minutes) (may be slow). The long-term scheduler controls the degree of multiprogramming. Processes can be described as either:


I/O-bound process spends more time doing I/O than computations, many short CPU bursts. CPU-bound process spends more time doing os4 49 computations; few very long CPU bursts.

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. Context-switch time is overhead; the system does no useful work while switching. Time dependent on hardware support.

os4

50

Process Creation
Parent process create children processes, which, in turn create other processes, forming a tree of processes. Resource sharing
  

Parent and children share all resources. Children share subset of parents resources. Parent and child share no resources. Parent and children execute concurrently. Parent waits until children terminate.
os4 51

Execution
 

Process Creation (Cont.)


Address space
 

Child duplicate of parent. Child has a program loaded into it. fork system call creates new process exec system call used after a fork to replace the process memory space with a new program.
os4 52

UNIX examples
 

System

os4

53

Process Termination
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 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. os4 54  Cascading termination.

Cooperating Processes
Independent process cannot affect or be affected by the execution of another process. Cooperating process can affect or be affected by the execution of another process Advantages of process cooperation
   

Information sharing Computation speed-up Modularity Convenience

os4

55

Producer-Consumer Problem
Paradigm for cooperating processes, producer process produces information that is consumed by a consumer process.


unbounded-buffer places no practical limit on the size of the buffer. bounded-buffer assumes that there is a fixed buffer size.
os4 56

Bounded-Buffer Shared-Memory Solution


Shared data
#define B FF SI y edef struct { ... } item; item uffer[B FF SI int in ; int out ;

];

Solution is correct, ut can only use SI - elements B FF

os4

57

Bounded-Buffer Producer Process


item nextProduced; while (1) { while (((in + 1) % BU ER_SIZE) == out) ; /* do nothing */ buffer[in] = nextProduced; in = (in + 1) % BU ER_SIZE; }
os4 58

Bounded-Buffer Consumer Process


item nextConsumed; while (1) { while (in == out) ; /* do nothing */ nextConsumed = buffer[out]; out = (out + 1) % BU ER_SIZE; }

os4

59

Interprocess Communication (IPC)


Mechanism for processes to communicate and to synchronize their actions. Message system processes communicate with each other without resorting to shared variables. IPC facility provides two operations:
 

send(message) message size fixed or variable receive(message)

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


 

establish a communication link between them os4 exchange messages via send/receive

60

Implementation Questions
How are links established? Can a link be associated with more than two processes? How many links can there be between every pair of communicating processes? What is the capacity of a link? Is the size of a message that the link can accommodate fixed or variable? Is a link unidirectional or bi-directional?
os4 61

Direct Communication
Processes must name each other explicitly:
 

send (P, message) send a message to process P receive(Q, message) receive a message from process Q 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 bidirectional.
os4 62

Properties of communication link


 

 

Indirect Communication from Messages are directed and received


mailboxes (also referred to as ports).
 

Each mailbox has a unique id. Processes can communicate only if they share a mailbox. Link established only if processes share a common mailbox A link may be associated with many processes. Each pair of processes may share several communication links. Link may be unidirectional or bi-directional.
os4 63

Properties of communication link




Indirect Communication
Operations
  

create a new mailbox send and receive messages through mailbox destroy a mailbox

Primitives are defined as: send(A, message) send a message to mailbox A receive(A, message) receive a message from mailbox A

os4

64

Indirect Communication
Mailbox sharing
  

P1, P2, and P3 share mailbox A. P1, sends; P2 and P3 receive. Who gets the message? 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.
os4 65

Solutions


Synchronization
Message passing may be either blocking or non-blocking. Blocking is considered synchronous Non-blocking is considered asynchronous send and receive primitives may be either blocking or non-blocking.
os4 66

Buffering
Queue of messages attached to the link; implemented in one of 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.
os4 67

Client-Server Communication
Sockets Remote Procedure Calls Remote Method Invocation (Java)

os4

68

Sockets
A socket is defined as an endpoint for communication. Concatenation of IP address and port The socket 161.25.19.8:1625 refers to port 1625 on host 161.25.19.8 Communication consists between a pair of sockets.
os4 69

Socket Communication

os4

70

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 marshalls the parameters. The server-side stub receives this message, unpacks the marshalled parameters, and peforms the procedure on the server.
os4 71

Execution of RPC

os4

72

Remote Method Invocation


Remote Method Invocation (RMI) is a Java mechanism similar to RPCs. RMI allows a Java program on one machine to invoke a method on a remote object.

os4

73

Marshalling Parameters

os4

74

You might also like