07--Operating System Support

Processes Threads --Pthread --Java Thread Multithreaded client and server design The role of OS/Network for RPC Microkernel
展开查看详情

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

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

3. System Layers Applications, services Middleware OS: kernel, OS1 OS2 libraries & Processes, threads, Processes, threads, servers communication, ... communication, ... Platform Computer & Computer & network hardware network hardware Node 1 Node 2 CSS434 OS Support 3

4. Processes Definition and Aspects  An environment to execute a program Address Space  A process consists of: 0  A CPU register set (including program text counter) Process Control Block  An dependent address space including heap ID PC  Text (code) SP  Data (global data) fd[64] TCB  Stack (local variables) stack  Heap (dynamic data)  Files socket data  Communication resources (sockets) 2N  Threads and their synchronization facility CSS434 OS Support 4

5. Processes Creation and Resource Sharing Parent’s PCB Child’s PCB  A parent process creates child ID ID processes through fork( ). PC copy PC SP SP  A child process overloads a new fd[64] fd[64] program on it through execve( ). TCB TCB  Execution Address Space Address Space  They run in concurrent. 0 0 text text  A parent may wait for the termination of a child through Shared memory shared Shared memory wait( ).  Resource sharing heap heap  Resource inherited by children: file descriptors, shared stack stack memory and system queues shared  Resource not inherited by socket data shared data children: address space 2N 2N CSS434 OS Support 5

6. Processes Creation and Copy-on-Write Physical address space  Parent’s virtual address space Upon creating a child process data  A virtual address space is data w allocated to a child. Shared  Corresponding physical w memory data memory is still mapped to its stack parent.  Upon a write operation, Shared text w memory physical memory is allocated Child’s virtual address space to a child and data is copied. data ww text Shared w memory stack stack text CSS434 OS Support 6

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

8.Threads o Process Control Block  Threads: ID text  The basic unit of CPU utilization fd[64]  Control of Program TCB  Process can have two or more heap Thread A parallel controls of program → ID multiple threads. PC  Belong to the same process. SP  No protection between Stack C threads. Thread B ID  Advantages: Stack B PC  Light creation SP  Light context switch Stack A  Suitable to parallel computing Thread C ID  Natural form of resource PC sharing data SP 2 N CSS434 OS Support 8

9. Threads v.s. Multiple Processes  Maybe, we can implement a server of multiple processes? Server consisting of Threads Multiple Processes Creation Light overhead Heavy overhead Context switch Cheap Expensive Remote object Easy Needs shared sharing memory Priority Easy to change Not quite dynamically changeable Protection Vulnerable Safe CSS434 OS Support 9

10. Thread Implementation Library and Class Library Java Pthread Solaris Functions 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 yield( ) thr_yield( ) queue Suspend a given thread suspend( ) thr_suspend( ) Resume a give thread resume( ) thr_continue( ) Get my current running priority getPriority( ) pthread_getschedpara thr_getprio( ) m( ) Change my current running priority setPriority( ) pthread_setschedpara thr_setprio( ) m( ) 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 10

11. Thread Implementation C++ Example #include <iostream> #include <string> Compilation and Execution using namespace std; Source Code $ g++ thread.cpp -lpthread #include <pthread.h> #include <unistd.h> $ a.out enter message: hello! void* thread_func( void *param ) { I'm a master: hello! I'm a slave: hello! for ( int i = 0; i < 5; i++ ) { I'm a master: hello! sleep( 2 ); I'm a master: hello! cout << "I'm a slave: " << *( (string *)param ) << endl; } I'm a slave: hello! return NULL; I'm a master: hello! } I'm a master: hello! I'm a slave: hello! void main( int argc, char *argv[] ) I'm a master: hello! { I'm a master: hello! pthread_t child; string arg; I'm a slave: hello! I'm a master: hello! cout << "enter message: "; I'm a master: hello! cin >> arg; I'm a slave: hello! pthread_create( &child, NULL, thread_func, (void *)&arg ); I'm a master: hello! for ( int i = 0; i < 10; i++ ) { Master synched with slave sleep( 1 ); cout << "I'm a master: " << arg << endl; $ } pthread_join( child, NULL ); cout << "Master synched with slave" << endl; } CSS434 OS Support 11

12. Thread Implementation Java Example MyThread.java public class MyThread { public static void main( String args[] ) { String arg = args[0]; Compilation and Execution ThreadFunc child = new ThreadFunc( arg ); child.start( ); $ ls for ( int i = 0; i < 10; i++ ) { MyThread.java ThreadFunc.java try { Thread.sleep( 1000 ); $ javac MyThread.java } catch ( InterruptedException e ) { }; $ java MyThread hello! System.out.println( "I'm a master: " + arg ); I'm a master: hello! } I'm a slave: hello! try { I'm a master: hello! child.join( ); I'm a master: hello! } catch ( InterruptedException e ) { }; System.out.println( "Master synched with slave" ); I'm a slave: hello! } I'm a master: hello! I'm a master: hello! } I'm a slave: hello! public class ThreadFunc extends Thread { I'm a master: hello! public ThreadFunc( String param ) { ThreadFunc.java I'm a master: hello! this.param = param; } I'm a slave: hello! public void run( ) { I'm a master: hello! for ( int i = 0; i < 5; i++ ) { I'm a master: hello! try { I'm a slave: hello! Thread.sleep( 2000 ); I'm a master: hello! } catch ( InterruptedException e ) { }; Master synched with slave System.out.println( "I'm a slave: " + param ); } $ } String param; } CSS434 OS Support 12

13. Client and Server with Threads  Client:  Can avoid a block on RPC by having two threads.  Server: Client Server  Assume: A request consists of 2ms processing and 4ms disk I/O. Computation RPC thread thread Request  Single-threaded Server queue RPC  Needs 6ms for a request requests  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 13

14. Multithreaded Clients Architecture  Why Multithreaded Clients  Hide network latency Client  Main Thread:  Interacts with a client user Main thread Child Threads  Work on computation RPC or TCP User request Server 1  Dispatch a request to a Pick up request RPC or TCP Server 2 child thread response RPC or TCP  Child threads request Server 3  Sends a request to a RPC or TCP given server through TCP request Server 4 or RPC. Enqueue  Waits for a response  Forwards a response to the main. CSS434 OS Support 14

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

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

17. Multithreaded Servers Example Thread Group  Object Server  Instantiates remote file A file B file C cgi 2 cgi 3 objects cgi 1  Associates each with stub stub an independent thread stub stub stub stub  Let a thread maintain and protect its object  Requests Per-objectPer-object Per-object Per-object Per-object Per-object thread thread thread thread thread  Accepted at request thread dispatcher  Forwarded to an object req que req que wrapper including Object wrapper Object wrapper objects residing under the same policy Daemon thread may  Server for per-object threads Picked up by the destination thread Request dispatcher Server Client requests CSS434 OS Support 17

18.Thread/OS Interaction in RPC (a) System call Thread Control transfer via trap instruction Control transfer via privileged instructions User Kernel Protection domain (b) RPC/RMI (within one computer) boundary Thread 1 Thread 2 User 1 Kernel User 2 (c) RPC/RMI (between computers) Thread 1 Network Thread 2 User 1 User 2 Kernel 1 Kernel 2 CSS434 OS Support 18

19.OS and Network Interaction in RPC RPC delay Why does this gap occurs if a RPC arguments grow beyond a packet size? Requested data size (bytes) 0 1000 2000 Packet size CSS434 OS Support 19

20. Monolithic kernel and microkernel S4 ....... S1 S2 S3 S4 ....... ....... S1 S2 S3 Monolithic Kernel Microkernel Key: Server: Kernel code and data: Dynamically loaded server program: Unix, Sprite: Mach, Chorus: non-modular way, intractable modularity, portability, and WindowsNT: extensibility layering and OO design but still massive. CSS434 OS Support 20

21. Role of the microkernel Middleware Language Language OS emulation support support subsystem .... subsystem subsystem 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 processes or convenience and flexible operations CSS434 OS Support 21

22. Exercises (No turn-in) 1. Textbook p333, Q7.7: Explain the advantage of copy-on-write region copying for Unix, where a c all to fork is typically followed by a call to exec. What should happen if a region that has been co pied 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 mu ltithreaded server? 4. If you comment out the following statement from the C++ code on Slide 11, what change will yo u 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 yo u 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 i n the server costs 5ms of CPU time when the server finds the requested block in the cache, and t ake an additional 15ms of disk I/O time otherwise. Explaining any assumptions you made, estima te 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 100M bps. CSS434 OS Support 22

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 re ceives 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 wit h socket descriptor sd[0] and sd[1] respectively. Every time the server accepts a new soc ket request from a client, it receives a file id from the client, and passes this socket reque st to the corresponding thread. The thread opens its file, checks a file operation type, an d 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 mo dels 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 23

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 24

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 25