Transcript Lecture5

Advanced Operating Systems - Fall 2009
Lecture 5 – January 26, 2009
Dan C. Marinescu
 Email: [email protected]
 Office: HEC 439 B.
 Office hours: M, Wd 3 – 4:30.

1
Last, Current, Next Lecture

Last time:





Today




Butler Lampson’s hints for system design
The complexity of computing and communication systems
State
Processes
Process handling by the kernel
Inter-process communication
Threads
Next time:

Process synchronization
2
Process Control Block (PCB)
3
Process scheduling
4
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
Context switch time dependent on hardware support
5
Inter-process communication

The need to communicate:




User process - kernel process  to invoke kernel functions
Kernel process - kernel process  the kernel is not monolithic
User process - user process  users have rights too!!
Basic strategies


Shared memory
Message passing
6
Shared memory


Producer – consumer (reader - writer) model.
Shared buffer  buffer(BUFFER_SIZE)



Unbounded: the produce could always write;
the consumer may have to wait for new item
Bounded: the produce may have to wait for buffer space
the consumer may have to wait for new item
Shared state between producer and consumer



Count – the number of items written by the producer and yet
to be read by the consumer
Producer state: in  pointer to the next available location
Consumer state: out  pointer to location of the next item
7
Producer
while (true) {
/* produce an item and put in nextProduced */
while (count == BUFFER_SIZE)
; // do nothing
buffer [in] = nextProduced;
in = (in + 1) % BUFFER_SIZE;
count++;
}
8
Consumer
while (true) {
while (count == 0)
; // do nothing
nextConsumed = buffer[out];
out = (out + 1) % BUFFER_SIZE;
count--;
/* consume the item in nextConsumed
}
9
Race conditions

Compiler translates producer’s process P instruction count++ as:
R1 = count
R1 = R1 +1
count = R1

Compiler translates consumer’s process C instruction count-- as:
R2 = count
R2 = R2 - 1
count = R2

Initially count = 5, in =12, out=7
P is scheduled to run
P executes R1 = count
(R1 = 5)
P : executes R1 = R1 + 1 (R1 = 6)
An interrupt occurs and P is suspended and C is dispatched
C executes R2 = count
(R2 = 5)
C executes R2 = R2-1
(R2 = 4)
P executes count = R1 (count = 6)
P writes the new item in buffer(12); in=13
P has finished and is suspended and C is dispatched
C executes count = R2 r(count = 4}
C read the new item from buffer(7); out=8
10
Message passing

The entities involved:





Sender – process P
Receiver – process Q
Logical communication channel
Message
Issues



Identification of the communicating processes (direct/indirect)
Type of communication (synchronous/asynchronous)
Buffering (implict/explicit)
11
Direct process identification

Name the destination/source explicitely



Sender (P)  send(Q, message)
Receiver (Q)  receive(P,message)
Alternative: receiever accepts a message from any
sender:


Sender (P)  send(Q, message)
Receiver (Q)  receive(id,message)
12
Indirect identification – Mailboxes/Ports




A mailbox/port – shared among several processes
Mailboxes in user or in system address space.
One or more channel may be connected to a mailbox.
Each channel may be shared by a number oif
processes.



Sender (P)  send(mailboxA, message)
Receiver (Q)  receive(mailboxA,message)
Q shares its inbox with one or more processes or may have
one mailbox per communication partner.
13
Sockets – communication in the Internet


Two transport protocols: TCP and UDP
Sockets




TCP – sockets
UDP sockets
To send a message to a process running on a host with IP
address 173.25.19.1 at TCP port 80 you specify the IP anddess
and the port: 173.25.19.1:80.
Java sockets



Connection-Oriented  TCP – sockets
Socket class
Connectionless
 UDP sockets
DatagramSocket class
Multicast sockets
MulticastSocket class
14
More about ports

The port numbers are divided into three ranges:




Well Known Ports from 0 through 1023,
Registered Ports
Dynamic and/or Private Ports
Examples






20/tcp File Transfer [Default Data]
20/udp  File Transfer [Default Data]
21/tcp File Transfer [Control]
21/udp File Transfer [Control]
80/tcp  World Wide Web HTTP
80/udp World Wide Web HTTP
15
Message passing in Mach (MAC OS X)

At creation time each user process (task) is assigned two ports:





To create a new port a process uses: port_allocate().
The queue of messages at a port is owned by the process which
created the port. To find the number of messages: port_status().
System calls for message passing:




Kernel  for communication with the Kernel
Notify  to notify the process about the occurrence of events
msg_send()
msg_receive()
msg_rpc()
The system guarantees that the messages from the same sender are
queued in a FIFO (First In First Out) manner. Messages from different
senders could be queued in any order.
16
Synchronous/Asynchronous communication

Synchronous or blocking send/receive

Asynchronous (non-blocking) send/receive
17
Remote Procedure Call (RPC)






Supports inter-process communication of remotely located processes
and allows implementation of client-server systems (RFC 1831)
Preserve the semantics of a local procedure call.
To use an RPC a process may use a special service: PORTMAP or
RPCBIND available at port 111. A new RPC service uses the
portmapper to register. The portmapper also allows a service lookup.
If the process knows the port number of the RPC it may call directly.
RPC/TCP and also RPC/UDP
Messages




must be well-structured; contain the identification of the specific RPC
are addressed to an RPC demon listening at an RPC port.
A stub hides the details of the implementation of the RPC.
A machine independent representation of data  external data
representation standard (XDR).
18
RPC semanitcs

At most once  a message is acted upon at most once.



The server must keep a history of the time-stamps of all messages.
Messages may arrive out of order…..
What if the RPC fails?
Exactly once  implement the at most once and request an
acknowledgment from the server.
19
Threads

Overview
 Multithreading Models
 Threading Issues
 Pthreads
 Windows XP Threads
 Linux Threads
 Java Threads
20
Single and Multithreaded Processes
21
Benefits of multithreading




Responsiveness
Resource Sharing
Economy
Takes advantage of multi-processor and multi-core
architectures
22
User Threads

Thread management done by user-level threads
library

Thread libraries:
 UNIX, Linux  POSIX Pthreads


Microsoft Windows Win32 threads
Java threads
23
Kernel Threads





Windows XP/2000
Solaris
Linux
Tru64 UNIX
Mac OS X
24
Multithreading Models

Many-to-One

One-to-One

Many-to-Many
25
Many-to-One


Many user-level threads mapped to single kernel
thread
Examples:


Solaris Green Threads
GNU Portable Threads
26
Many-to-One
27
One-to-One


Each user-level thread maps to one kernel thread
Examples



Windows NT/XP/2000
Linux
Solaris 9 and later
28
One-to-one
29
Many-to-Many

Allows



a user level thread to be mapped to several kernel
threads
the operating system to create a sufficient number
of kernel threads
Examples


Solaris prior to version 9
Windows NT/2000 with the ThreadFiber package
30
Many-to-Many
31
Two-level Model


Similar to M:M, except that it allows a user
thread to be bound to kernel thread
Examples




IRIX
HP-UX
Tru64 UNIX
Solaris 8 and earlier
32
Two-level Model
33
Threading Issues

Semantics of fork() and exec() system calls
 Thread cancellation
 Signal handling
 Thread pools
 Thread specific data
 Scheduler activations
34
Semantics of fork() and exec()

Does fork() duplicate only the calling thread or all
threads?
35
Thread Cancellation


Terminating a thread before it has finished
Two general approaches:


Asynchronous cancellation terminates the target
thread immediately
Deferred cancellation allows the target thread to
periodically check if it should be cancelled
36