Professional Documents
Culture Documents
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).
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.
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
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
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
PCB7
PCB2
registers
ready queue
registers
PCB3
PCB14
registers
PCB8
registers
head tail
registers
PCB6
terminal unit 0
head tail
registers
I/O
I/O queue
I/O request
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
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
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
e.g.,
Process termination
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
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))
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
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
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
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
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
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
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
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:
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)
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 }
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
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
os4
41
os4
42
os4
43
os4
44
Device Queues
os4
45
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
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
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.
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
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
];
os4
57
os4
59
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
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
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
Execution of RPC
os4
72
os4
73
Marshalling Parameters
os4
74