Title: CSS434: Parallel
1CSS434 Operating System Support Textbook Ch6
Professor Munehiro Fukuda
2Outline
- Processes
- Threads
- Pthread
- Java Thread
- Multithreaded client and server design
- The role of OS/Network for RPC
- Microkernel
3System Layers
4ProcessesDefinition and Aspects
- An environment to execute a program
- A process consists of
- A CPU register set (including program counter)
- An dependent address space including
- Text (code)
- Data (global data)
- Stack (local variables)
- Heap (dynamic data)
- Files
- Communication resources (sockets)
- Threads and their synchronization facility
socket
5ProcessesCreation and Resource Sharing
- A parent process creates child processes through
fork( ). - A child process overloads a new program on it
through execve( ). - Execution
- They run in concurrent.
- A parent may wait for the termination of a child
through wait( ). - Resource sharing
- Resource inherited by children file descriptors,
shared memory and system queues - Resource not inherited by children address space
shared
shared
socket
shared
6ProcessesCreation and Copy-on-Write
- Upon creating a child process
- A virtual address space is allocated to a child.
- Corresponding physical memory is still mapped to
its parent. - Upon a write operation, physical memory is
allocated to a child and data is copied.
Physical address space
Parents virtual address space
data
Shared memory
w
data
stack
text
Shared memory
w
Childs virtual address space
data
w
text
Shared memory
w
stack
stack
text
7ProcessesCreation and Load Balancing
- Transfer policy Create a new process
- Locally
- Remotely ? Location Policy
- Static transfer processes to predefined
destinations - Adaptivetransfer processes to destinations based
on run-time information - Centralized load-sharing A load manager take
care of correcting info and migrating processes. - Hierarchical load-sharing A system consists of a
tree structure where each node takes care of its
child processes for load balancing - Decentralized load-sharing
- Sender-initiated A heavy-loaded node sends out a
new process. - Receiver-initiated A light-loaded node
advertises its existence.
8Threads
- Threads
- The basic unit of CPU utilization
- Control of Program
- Process can have two or more parallel controls of
program ? multiple threads. - Belong to the same process.
- No protection between threads.
- Advantages
- Light creation
- Light context switch
- Suitable to parallel computing
- Natural form of resource sharing
9Threads v.s. Multiple Processes
- Maybe, we can implement a server of multiple
processes?
10Thread ImplementationLibrary and Class
11Thread ImplementationC Example
include ltiostreamgt include ltstringgt using
namespace std include ltpthread.hgt include
ltunistd.hgt void thread_func( void param )
for ( int i 0 i lt 5 i ) sleep( 2 )
cout ltlt "I'm a slave " ltlt ( (string )param )
ltlt endl return NULL void main( int
argc, char argv ) pthread_t child
string arg cout ltlt "enter message " cin
gtgt arg pthread_create( child, NULL,
thread_func, (void )arg ) for ( int i 0 i
lt 10 i ) sleep( 1 ) cout ltlt "I'm a
master " ltlt arg ltlt endl pthread_join(
child, NULL ) cout ltlt "Master synched with
slave" ltlt endl
Compilation and Execution
Source Code
g thread.cpp -lpthread a.out enter message
hello! I'm a master hello! I'm a slave
hello! I'm a master hello! I'm a master
hello! I'm a slave hello! I'm a master
hello! I'm a master hello! I'm a slave
hello! I'm a master hello! I'm a master
hello! I'm a slave hello! I'm a master
hello! I'm a master hello! I'm a slave
hello! I'm a master hello! Master synched with
slave
12Thread ImplementationJava Example
MyThread.java
public class MyThread public static void
main( String args ) String arg
args0 ThreadFunc child new ThreadFunc( arg
) child.start( ) for ( int i 0 i lt 10 i
) try Thread.sleep( 1000 )
catch ( InterruptedException e )
System.out.println( "I'm a master " arg
) try child.join( ) catch (
InterruptedException e ) System.out.println(
"Master synched with slave" ) public
class ThreadFunc extends Thread public
ThreadFunc( String param ) this.param
param public void run( ) for ( int
i 0 i lt 5 i ) try Thread.sleep(
2000 ) catch ( InterruptedException e )
System.out.println( "I'm a slave "
param ) String param
Compilation and Execution
ls MyThread.java ThreadFunc.java javac
MyThread.java java MyThread hello! I'm a
master hello! I'm a slave hello! I'm a master
hello! I'm a master hello! I'm a slave
hello! I'm a master hello! I'm a master
hello! I'm a slave hello! I'm a master
hello! I'm a master hello! I'm a slave
hello! I'm a master hello! I'm a master
hello! I'm a slave hello! I'm a master
hello! Master synched with slave
ThreadFunc.java
13Client and Server with Threads
- Client
- Can avoid a block on RPC by having two threads.
- Server
- Assume A request consists of 2ms processing and
4ms disk I/O. - Single-threaded Server
- Needs 6ms for a request
- Can process 166 request/second
- If three threads pick up and work on a request in
turn - Can overlap processing and I/O
- Can process 1000/4 250 request/second.
14Multithreaded ClientsArchitecture
- Why Multithreaded Clients
- Hide network latency
- Main Thread
- Interacts with a client user
- Work on computation
- Dispatch a request to a child thread
- Child threads
- Sends a request to a given server through TCP or
RPC. - Waits for a response
- Forwards a response to the main.
Client
Main thread
Child Threads
User
Pick up
response
RPC or TCP
Server 4
request
Enqueue
15Multithreaded ClientsExample
Web browser
- Web browser
- Requests and reads the main HTML file.
- For each ltimg srcgt, ltobjectgt, ltappletgt, etc.,
- Spawns a child thread
- Child threads
- Sets up an HTTP connection
- Reads each web part.
Main thread
Child Threads
HTTP
User
Request the main HTML
Server
lthtmlgt
Scan the file
HTTP
Server
Spawn a thread
img
HTTP
Spawn a thread
Server
applet
16Multithreaded ServersArchitecture
- Dispatcher-worker model
- The worker pool (a)
- Per-request threads (b)
- Per-connection threads (c)
- Team model
- Per-object threads (d)
- Pipeline model (e)
(a)
(b)
Pick up a request
Spawn for each request
accept
accept
queue
(c)
(d)
(e)
Session
accept
request
accept
Spawn for each session
request
process
request
response
17Multithreaded ServersExample
- Object Server
- Instantiates remote objects
- Associates each with an independent thread
- Let a thread maintain and protect its object
- Requests
- Accepted at request dispatcher
- Forwarded to an object wrapper including objects
residing under the same policy - Picked up by the destination thread
Thread Group
Daemon thread may Server for per-object threads
18Thread/OS Interaction in RPC
19OS and Network Interaction in RPC
Why does this gap occurs if a RPC arguments grow
beyond a packet size?
20Monolithic kernel and microkernel
Mach, Chorus modularity, portability,
and extensibility
Unix, Sprite non-modular way,
intractable WindowsNT layering and OO design
but still massive.
21Role of the microkernel
Microkernel facilitates only address spaces,
threads and local inter-process
communication. Middleware can use directly
Microkernel for better performance or system
processes for convenience and flexible operations
22Exercises (No turn-in)
- Textbook p262, Q6.7 Explain the advantage of
copy-on-write region copying for Unix, where a
call to fork is typically followed by a call to
exec. What should happen if a region that has
been copied using copy-on-write is itself copied? - Why can threads perform their context switch
faster than processes? - What are the thread groups? What are the daemon
threads? How can those contribute to the
multithreaded server? - If you comment out the following statement from
the C code on Slide 11, what change will you
observer in the execution? - pthread_join( child, NULL )
- If you comment out the following statement from
the Java code on Slide 12, what change will you
observe in the execution? - child.join( )
- Textbook p262, Q6.8 A file server uses caching,
and achieves a hit rate of 80. File operations
in the server costs 5ms of CPU time when the
server finds the requested block in the cache,
and take an additional 15ms of disk I/O time
otherwise. Explaining any assumptions you made,
estimate the servers throughput capacity
(average requests/sec) if it is - Signle-threaded
- Two-threaded, running on a single processor
- Two-threaded, running on a two-processor
computer. - Textbook p263 Q6.16 Network transmission time
accounts for 20 of a null RPC and 80 of an RPC
that transmits 1024 user bytes (less than the
size of a network packet). By what percentage
will the times for these two operations improve
if the network is upgraded from 10Mbps to 100Mbps.
23Exercises (No turn-in)
- Q8. The following server code assumes that each
client program asks three user inputs - (1) the id of a file to operate on 0 or 1,
each corresponding file0 and file1 - (2) a file operation type r as a file read
or w as a file write - (3) if the file operation is w, a 100-byte
message to be read from a keyboard. - The client sends those inputs to the server
through a socket. If the file operation type is
r, it receives a 100-byte content of the
corresponding file from the server and prints it
out. - The server spawns two child threads
child_thread0 and childe_thread1, each
associated with socket descriptor sd0 and sd1
respectively. Every time the server accepts a new
socket request from a client, it receives a file
id from the client, and passes this socket
request to the corresponding thread. The thread
opens its file, checks a file operation type, and
reads/writes the file according to the type. - Q8-1. Which server model was used in this tcp.cpp
program, worker pool, per-request threads,
per-connection threads, per-object threads, or
pipeline model? Choose one of these models and
justify your choice. - Q8-2. The server in the above program is not so
scalable. In other words, it cant handle many
client requests concurrently. Why? Explain the
reason. - Q8-3. Which server model(s) are scalable? If you
change this program into such server model(s),
what side effect will occur? Describe the major
problem you have conceived.
24Exercises (No turn-in)
- include "Socket.h"
- include "pthread.h"
- define PORT 10000
- define SIZE 100 // message
size - int sd2 // socket
descriptor - void thread_func( void arg ) // thread to
read and write a given file - int id (int )arg // my thread
id - char fileName (id 0) ? "file0" "file1"
// if I'm 0, operate on file0 - while ( true )
- if ( sdid NULL_FD ) // check if a
new socket request came to me. - continue
- int fd open( fileName, O_RDWR )// open my
file - char op // a file
operation type 'r' or 'w' - read( sdid, op, 1 ) // read an
operation type from the socket - if ( opid 'r' ) // a read
operation - read( fd, message, SIZE ) //
read 100 bytes from my file - write( sdmyId, message, SIZE ) //
send them back to the client - else if ( opid 'w' ) // a write
operation - read( sdmyId, message, SIZE ) //
receive 100 bytes from the client
25Exercises (No turn-in)
- int main( int argc, char argv )
- int sd NULL_FD // socket
descriptor - char id 0 // a file
(thread) id - if ( argc 1) // I'm a
server - pthread_t child2
- for ( int i 0 i lt 2 i ) // create
two child threads - sdi NULL_FD
- pthread_create( child0, NULL,
thread_func, (void )i ) -
-
- while ( true ) // keep
receiving a client socket request - if ( ( sd sock.getServerSocket( ) )
NULL_FD ) - return -1
- read( sd, id, 1 ) // receive
a file (thread) id - while ( sdid ! NULL_FD )
- // wait
for cihld_threadid to be ready - sdid sd // pass
this client socket to the thread -
-