css434 os support1 css434 operating system support textbook ch7 professor: munehiro fukuda
Post on 20-Dec-2015
218 views
TRANSCRIPT
CSS434 OS Support 1
CSS434 Operating System SupportCSS434 Operating System SupportTextbook Ch7Textbook Ch7
Professor: Munehiro Fukuda
CSS434 OS Support 2
Outline Processes Threads
Pthread Java Thread
Multithreaded client and server design
The role of OS/Network for RPC Microkernel
CSS434 OS Support 3
System Layers
Applications, services
Computer &
Platform
Middleware
OS: kernel,libraries & servers
network hardware
OS1
Computer & network hardware
Node 1 Node 2
Processes, threads,communication, ...
OS2Processes, threads,communication, ...
CSS434 OS Support 4
ProcessesDefinition 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
text
heap
stack
data
0
2N
Address Space
IDProcess Control Block
PC
SP
fd[64]
TCB
socket
CSS434 OS Support 5
ProcessesCreation 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
socket
IDParent’s PCB
PCSP
fd[64]TCB
text
heap
stack
data
0
2N
Address Space
Sharedmemory
IDChild’s PCB
PCSP
fd[64]TCB
text
heap
stack
data
0
2N
Address Space
Sharedmemory
copy
shared
shared
shared
CSS434 OS Support 6
ProcessesCreation 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.
text
text
data
data
stack
Sharedmemory
text
data
stack
Sharedmemory
stack
Sharedmemory
Physical address space Parent’s virtual address space
Child’s virtual address space
w
dataw
dataw
w
w
w
CSS434 OS Support 7
ProcessesCreation and Load Balancing
Transfer policy: Create a new process Locally Remotely → Location Policy
Static: transfer processes to predefined destinations Adaptive:transfer 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.
CSS434 OS Support 8
Threads 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
sharingdata
text
Stack A
Stack B
Stack C
heap
o
2N
IDProcess Control Block
fd[64]
TCB
IDThread A
PC
SP
IDThread B
PC
SP
IDThread C
PC
SP
CSS434 OS Support 9
Threads v.s. Multiple Processes
Server consisting of Threads Multiple Processes
Creation Light overhead Heavy overhead
Context switch Cheap Expensive
Remote object sharing
Easy Needs shared memory
Priority Easy to change Not quite dynamically changeable
Protection Vulnerable Safe
Maybe, we can implement a server of multiple processes?
CSS434 OS Support 10
Thread ImplementationLibrary and Class
LibraryFunctions
Java Pthread Solaris Thread
Create a new thread new Thread( ) pthread_create( ) thr_create( )
Terminate myself destroy( ) pthread_exit( ) thr_exit( )
Wait for a given thread to be terminated join( ) pthread_join( ) thr_join( )
Terminate a given thread stop( ) pthread_kill( ) thr_kill( )
Get my thread object currentThread( )
pthread_self( ) thr_self( )
Relinquish CPU and put myself in a ready queue
yield( ) thr_yield( )
Suspend a given thread suspend( ) thr_suspend( )
Resume a give thread resume( ) thr_continue( )
Get my current running priority getPriority( ) pthread_getschedparam( )
thr_getprio( )
Change my current running priority setPriority( ) pthread_setschedparam( )
thr_setprio( )
Wait for another thread to signal me wait( ) pthread_cond_wait( ) cond_wait( )
Signal another thread waiting for me signal( ) pthread_cond_signal( ) cond_signal( )
CSS434 OS Support 11
Thread ImplementationC++ Example
#include <iostream>#include <string>using namespace std;
#include <pthread.h>#include <unistd.h>
void* thread_func( void *param ) {
for ( int i = 0; i < 5; i++ ) { sleep( 2 ); cout << "I'm a slave: " << *( (string *)param ) << endl; } return NULL;}
void main( int argc, char *argv[] ){ pthread_t child; string arg;
cout << "enter message: "; cin >> arg; pthread_create( &child, NULL, thread_func, (void *)&arg ); for ( int i = 0; i < 10; i++ ) { sleep( 1 ); cout << "I'm a master: " << arg << endl; } pthread_join( child, NULL ); cout << "Master synched with slave" << endl;}
$ g++ thread.cpp -lpthread$ a.outenter 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$
Compilation and Execution Source Code
CSS434 OS Support 12
Thread ImplementationJava Example
public class MyThread { public static void main( String args[] ) {
String arg = args[0];ThreadFunc child = new ThreadFunc( arg );child.start( );for ( int i = 0; i < 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 < 5; i++ ) { try {
Thread.sleep( 2000 ); } catch ( InterruptedException e ) { }; System.out.println( "I'm a slave: " + param );}
} String param;}
$ lsMyThread.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$
Compilation and Execution
MyThread.java
ThreadFunc.java
CSS434 OS Support 13
Client 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.
Client Server
RPCRequestqueue
RPCthread
Computationthread
requests
CSS434 OS Support 14
Multithreaded 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
Child ThreadsMain thread
User request Server 1RPC or TCP
request Server 2RPC or TCP
request Server 3RPC or TCP
request Server 4RPC or TCP
response
Pick up
Enqueue
CSS434 OS Support 15
Multithreaded ClientsExample
Web browser Requests and reads the
main HTML file. For each <img
src=“…”>, <object>, <applet>, etc.,
Spawns a child thread Child threads:
Sets up an HTTP connection
Reads each web part.
Web browser
Child ThreadsMain thread
User Request the main HTML ServerHTTP
Spawn a thread ServerHTTP
Server
<html>……
Scan the file
img
Spawn a threadHTTP
applet
CSS434 OS Support 16
Multithreaded 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)
queueaccept
accept
acceptaccept
Remote object
Remote object
Remote object
process
response
request
request
request
Spawn for each request
Spawn for each session
Session
Pick up a request(a) (b)
(c) (d) (e)
CSS434 OS Support 17
Multithreaded 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
file A
stub
Per-objectthread
file B
stub
Per-objectthread
file C
stub
Per-objectthread
Object wrapper
req que
Request dispatcher
cgi 1
stub
Per-objectthread
cgi 2
stub
Per-objectthread
cgi 3
stub
Per-objectthread
Object wrapper
req que
Server
Client requests
Daemon thread mayServer for per-object threads
Thread Group
CSS434 OS Support 18
Thread/OS Interaction in RPC
Control transfer viatrap instruction
User Kernel
Thread
User 1 User 2
Control transfer viaprivileged instructions
Thread 1 Thread 2
Protection domainboundary
(a) System call
(b) RPC/RMI (within one computer)
Kernel
(c) RPC/RMI (between computers)
User 1 User 2
Thread 1 Network Thread 2
Kernel 2Kernel 1
CSS434 OS Support 19
OS and Network Interaction in RPC
1000 2000
RPC delay
Requested datasize (bytes)
Packetsize
0
Why does this gap occurs if a RPC arguments grow beyond a packet size?
CSS434 OS Support 20
Monolithic kernel and microkernel
Monolithic Kernel Microkernel
Server: Dynamically loaded server program:Kernel code and data:
.......
.......
Key:
S4
S1 .......
S1 S2 S3
S2 S3 S4
Unix, Sprite:non-modular way, intractable
WindowsNT:layering and OO design but stillmassive.
Mach, Chorus:modularity, portability, andextensibility
CSS434 OS Support 21
Role of the microkernelMiddleware
Languagesupport
subsystem
Languagesupport
subsystem
OS emulationsubsystem ....
Microkernel
Hardware
The microkernel supports middleware via subsystems
Microkernel facilitates only address spaces, threads and local inter-process communication.Middleware can use directly Microkernel for better performance or system processesfor convenience and flexible operations
CSS434 OS Support 22
Exercises (No turn-in)1. Textbook p333, Q7.7: Explain the advantage of copy-on-write region copying for Unix, where a call to for
k 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?
2. Why can threads perform their context switch faster than processes?3. What are the thread groups? What are the daemon threads? How can those contribute to the multithrea
ded server?4. If you comment out the following statement from the C++ code on Slide 11, what change will you obser
ver in the execution?1. pthread_join( child, NULL );
5. If you comment out the following statement from the Java code on Slide 12, what change will you observe in the execution?
6. child.join( );7. Textbook p333, Q7.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 server’s throughput capacity (average requests/sec) if it is:
1. Signle-threaded;2. Two-threaded, running on a single processor;3. Two-threaded, running on a two-processor computer.
8. Textbook p333 Q7.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.
CSS434 OS Support 23
Exercises (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_thread[0] and childe_thread[1], each associated with socket descriptor sd[0] and sd[1] 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 can’t 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.
CSS434 OS Support 24
Exercises (No turn-in)#include "Socket.h"#include "pthread.h"#define PORT 10000#define SIZE 100 // message sizeint sd[2]; // socket descriptorvoid* 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 ( sd[id] == 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( sd[id], &op, 1 ); // read an operation type from the socket if ( op[id] == 'r' ) { // a read operation read( fd, message, SIZE ); // read 100 bytes from my file write( sd[myId], message, SIZE ); // send them back to the client } else if ( op[id] == 'w' ) { // a write operation read( sd[myId], message, SIZE ); // receive 100 bytes from the client write( fd, message, SIZE ); // write them down to my file } close( fd ); // close my file close( sd[Id] ); // close this socket conneciton sd[Id] = NULL_FD; // inform the server that I'm ready } }
CSS434 OS Support 25
Exercises (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 child[2]; for ( int i = 0; i < 2; i++ ) { // create two child threads sd[i] = NULL_FD; pthread_create( &child[0], 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 ( sd[id] != NULL_FD )
; // wait for cihld_thread[id] to be ready sd[id] = sd; // pass this client socket to the thread } } if ( argc == 2 ) { // I'm a client if ( ( sd = Sock.getClientSocket( argv[1] ) ) == NULL_FD ) return -1; cout << "Operate on file0 or file1? (type 0 or 1): "; cin >> id; write( sd, id, 1 ); // send a file (thread) id to server cout << "Operation type? (type r or w): "; cin >> op; write( sd, &op, 1 ); // send a file operation type to server if ( op == 'w' ) { // if it's a write operation cout << "message: "; cin >> message; // read a user message write( sd, message, SIZE ); // send it to the server } if ( op == 'r' ) { // if it's a read operation read( sd, message, SIZE ); // receive a message from server cout << message << endl; // display it } }}