Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS434 OS Support1 CSS434 Operating System Support Textbook Ch7 Professor: Munehiro Fukuda.

Similar presentations


Presentation on theme: "CSS434 OS Support1 CSS434 Operating System Support Textbook Ch7 Professor: Munehiro Fukuda."— Presentation transcript:

1 CSS434 OS Support1 CSS434 Operating System Support Textbook Ch7 Professor: Munehiro Fukuda

2 CSS434 OS Support2 Outline Processes Threads Pthread Java Thread Multithreaded client and server design The role of OS/Network for RPC Microkernel

3 CSS434 OS Support3 System Layers

4 CSS434 OS Support4 Processes Definition 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 2N2N Address Space ID Process Control Block PC SP fd[64] TCB socket

5 CSS434 OS Support5 Processes Creation 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 ID Parent ’ s PCB PC SP fd[64] TCB text heap stack data 0 2N2N Address Space Shared memory ID Child ’ s PCB PC SP fd[64] TCB text heap stack data 0 2N2N Address Space Shared memory copy shared

6 CSS434 OS Support6 Processes Creation 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 data stack Shared memory text data stack Shared memory stack Shared memory Physical address space Parent ’ s virtual address space Child ’ s virtual address space w data w w w w w

7 CSS434 OS Support7 Processes Creation 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.

8 CSS434 OS Support8 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 sharing data text Stack A Stack B Stack C heap o 2N2N ID Process Control Block fd[64] TCB ID Thread A PC SP ID Thread B PC SP ID Thread C PC SP

9 CSS434 OS Support9 Threads v.s. Multiple Processes Server consisting ofThreadsMultiple Processes CreationLight overheadHeavy overhead Context switchCheapExpensive Remote object sharingEasyNeeds shared memory PriorityEasy to changeNot quite dynamically changeable ProtectionVulnerableSafe Maybe, we can implement a server of multiple processes?

10 CSS434 OS Support10 Thread Implementation Library and Class Library Functions JavaPthreadSolaris Thread Create a new threadnew Thread( )pthread_create( )thr_create( ) Terminate myselfdestroy( )pthread_exit( )thr_exit( ) Wait for a given thread to be terminatedjoin( )pthread_join( )thr_join( ) Terminate a given threadstop( )pthread_kill( )thr_kill( ) Get my thread objectcurrentThread( )pthread_self( )thr_self( ) Relinquish CPU and put myself in a ready queueyield( )thr_yield( ) Suspend a given threadsuspend( )thr_suspend( ) Resume a give threadresume( )thr_continue( ) Get my current running prioritygetPriority( )pthread_getschedparam( )thr_getprio( ) Change my current running prioritysetPriority( )pthread_setschedparam( )thr_setprio( ) Wait for another thread to signal mewait( )pthread_cond_wait( )cond_wait( ) Signal another thread waiting for mesignal( )pthread_cond_signal( )cond_signal( )

11 CSS434 OS Support11 Thread Implementation C++ Example #include using namespace std; #include 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.out enter message: hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! I'm a slave: 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

12 CSS434 OS Support12 Thread Implementation Java 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; } $ 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 slave: hello! I'm a master: hello! I'm a slave: hello! I'm a master: hello! I'm a slave: 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

13 CSS434 OS Support13 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 RPC Request queue RPC thread Computation thread requests

14 CSS434 OS Support14 Multithreaded Clients Architecture 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 Threads Main thread User request Server 1 RPC or TCP request Server 2 RPC or TCP request Server 3 RPC or TCP request Server 4 RPC or TCP response Pick up Enqueue

15 CSS434 OS Support15 Multithreaded Clients Example Web browser Requests and reads the main HTML file. For each,,, etc., Spawns a child thread Child threads: Sets up an HTTP connection Reads each web part. Web browser Child Threads Main thread User Request the main HTML Server HTTP Spawn a thread Server HTTP Server … Scan the file img Spawn a thread HTTP applet

16 CSS434 OS Support16 Multithreaded Servers Architecture Dispatcher-worker model The worker pool (a) Per-request threads (b) Per-connection threads (c) Team model Per-object threads (d) Pipeline model (e) queue accept Remote object process response request Spawn for each request Spawn for each session Session Pick up a request (a) (b) (c) (d) (e)

17 CSS434 OS Support17 Multithreaded Servers Example 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-object thread file B stub Per-object thread file C stub Per-object thread Object wrapper req que Request dispatcher cgi 1 stub Per-object thread cgi 2 stub Per-object thread cgi 3 stub Per-object thread Object wrapper req que Server Client requests Daemon thread may Server for per-object threads Thread Group

18 CSS434 OS Support18 Thread/OS Interaction in RPC

19 CSS434 OS Support19 OS and Network Interaction in RPC Why does this gap occurs if a RPC arguments grow beyond a packet size?

20 CSS434 OS Support20 Monolithic kernel and microkernel Unix, Sprite: non-modular way, intractable WindowsNT: layering and OO design but still massive. Mach, Chorus: modularity, portability, and extensibility

21 CSS434 OS Support21 Role 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

22 CSS434 OS Support22 Exercises (No turn-in) 1. Textbook p333, Q7.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? 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 multithreaded server? 4. If you comment out the following statement from the C++ code on Slide 11, what change will you observer 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.

23 CSS434 OS Support23 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.

24 CSS434 OS Support24 Exercises (No turn-in) #include "Socket.h" #include "pthread.h" #define PORT 10000 #define SIZE 100 // message size int sd[2]; // 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 ( 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 }

25 CSS434 OS Support25 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 > id; write( sd, id, 1 ); // send a file (thread) id to server cout > op; write( sd, &op, 1 ); // send a file operation type to server if ( op == 'w' ) { // if it's a write operation cout > 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 }


Download ppt "CSS434 OS Support1 CSS434 Operating System Support Textbook Ch7 Professor: Munehiro Fukuda."

Similar presentations


Ads by Google