1 / 25

CSS434 Operating System Support Textbook Ch7

CSS434 Operating System Support Textbook Ch7. Professor: Munehiro Fukuda. Outline. Processes Threads Pthread Java Thread Multithreaded client and server design The role of OS/Network for RPC Microkernel. System Layers. Address Space. 0. text. heap. Process Control Block. ID. PC.

Gabriel
Télécharger la présentation

CSS434 Operating System Support Textbook Ch7

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSS434 Operating System Support Textbook 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 CSS434 OS Support

  4. Address Space 0 text heap Process Control Block ID PC SP stack fd[64] TCB data 2N 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 socket CSS434 OS Support

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

  6. data w data w 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. Physical address space Parent’s virtual address space data Shared memory w data stack text Shared memory w Child’s virtual address space data w text Shared memory w stack stack text 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. o text Process Control Block ID heap fd[64] TCB Stack C Thread B Thread C Thread A ID ID ID PC PC PC Stack B SP SP SP Stack A data 2N 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 CSS434 OS Support

  9. Threads v.s. Multiple Processes • Maybe, we can implement a server of multiple processes? CSS434 OS Support

  10. Thread ImplementationLibrary and Class 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; } 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 $ CSS434 OS Support

  12. Thread ImplementationJava Example MyThread.java 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; } 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 CSS434 OS Support

  13. Client Server Computation thread RPC thread Request queue RPC requests 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. CSS434 OS Support

  14. RPC or TCP RPC or TCP RPC or TCP Server 3 Server 2 Server 1 request request request 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 Main thread Child Threads User Pick up response RPC or TCP Server 4 request 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 Main thread Child Threads HTTP User Request the main HTML Server <html> … … Scan the file HTTP Server Spawn a thread img HTTP Spawn a thread Server applet CSS434 OS Support

  16. Remote object Remote object Remote object 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) (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 CSS434 OS Support

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

  18. Thread/OS Interaction in RPC CSS434 OS Support

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

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

  21. 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 CSS434 OS Support

  22. Exercises (No turn-in) • 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? • 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 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: • Signle-threaded; • Two-threaded, running on a single processor; • Two-threaded, running on a two-processor computer. • 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 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 } } 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 } } } CSS434 OS Support

More Related