Presentation is loading. Please wait.

Presentation is loading. Please wait.

ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 6.

Similar presentations


Presentation on theme: "ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 6."— Presentation transcript:

1 ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 6

2 ICOM 6115©Manuel Rodriguez-Martinez Lecture Objectives Understand Network Programming –Operating Systems support for Networking threads –Implementation options for servers Iterative servers Concurrent servers –Buffering of I/O streams –Finite state machines –Protocols Design

3 ICOM 6115©Manuel Rodriguez-Martinez Threads and Concurrent Servers One thread is running the master server –Called the main thread Many other threads are used for slave server –Created on the fly MyServer2.java –Obtained from pool of pre-allocated threads Will see later

4 ICOM 6115©Manuel Rodriguez-Martinez Processes and Threads Process –Program in execution –Has: program counter, text section, process stack, and data section –By default has one thread of execution Thread – lightweight process –Unit of CPU time allocation –Has: thread id, program counter, register set and stack –Allows for multiple actions to run within a process

5 ICOM 6115©Manuel Rodriguez-Martinez Multi-threaded Processes Processes Each thread is executing a part of the program

6 ICOM 6115©Manuel Rodriguez-Martinez Why threads? Responsiveness –GUI can have one thread running HTTP client code and other thread drawing JPEG file Resource sharing –Thread shared variables in a process Sockets, and files can be shared by many threads –Parallel I/O Simplicity –IPC via shared memory is a pain … Support for parallelism on parallel machines for free (“almost”)

7 ICOM 6115©Manuel Rodriguez-Martinez Example Program MyThread1.java –Creates many threads that type the word “Hello World” 10 times Java run time system calls the main() method of the thread object Main() creates new threads and calls the run() method in each one.

8 ICOM 6115©Manuel Rodriguez-Martinez Thread Types User threads (the ones you use) –thread management done by User-Level Threads Library –Ex: Posix Pthreads, Solaris threads, Java Kernel threads –thread management done by kernel of OS –Ex: Windows 95/98/NT/XP, Solaris 2 Need both to make multi-threaded programs

9 ICOM 6115©Manuel Rodriguez-Martinez Thread Models Many-to-One Model One-to-One Model Many-to-Many Model

10 ICOM 6115©Manuel Rodriguez-Martinez Many-to-One Model

11 ICOM 6115©Manuel Rodriguez-Martinez Many-to-One Model Many user-level threads are mapped to a single kernel thread Used on systems that do not support kernel threads –Sun OS 4 Process will block on I/O request Cannot support parallelism on multi- processors

12 ICOM 6115©Manuel Rodriguez-Martinez One-to-One Model

13 ICOM 6115©Manuel Rodriguez-Martinez One-to-One Model Each user thread is mapped to a kernel thread Provides more concurrency and paralellism. Process will not block for I/O –Only the thread running the I/O request blocks Expensive to create kernel threads –System bogs down when multiple applications are opened Example OS: –You guessed it! Windows 95/98/NT/XP

14 ICOM 6115©Manuel Rodriguez-Martinez Many-to-Many Model thread dispatcher

15 ICOM 6115©Manuel Rodriguez-Martinez Many-to-Many Model User threads are multiplexed to multiple kernel threads Provides more concurrency and paralellism. Process will not block for I/O –Only the thread running the I/O request blocks Kernel threads can be pre-allocated –Pool of threads –Very inexpensive and fast Example OS –Sun Solaris 2

16 ICOM 6115©Manuel Rodriguez-Martinez Problem: Shared Data Since threads can share many variables we need a way to arbitrate access Lost update problem –If many threads change a variable, which change survives –If many threads change a variable, how can we serialize the changes to make them consistent Withdraw from a ATM (aka ATH)

17 ICOM 6115©Manuel Rodriguez-Martinez Thread Contention Balance = $400 Balance - = 10;Balance - = 20;Balance - = 5; Threads Final Balance Should be $365

18 ICOM 6115©Manuel Rodriguez-Martinez Solution: Synchronization Need a mechanism that guarantees only one thread can access the variable at a time Java provides synchronized methods –Guaranteed to be executed by only thread at a time –Put code that changes shared variables on a synchronized method Critical section

19 ICOM 6115©Manuel Rodriguez-Martinez Examples of Contention Example 1: Contention1.java –Changes a shared variable unsafely –Lost update problem Example 2: Contention2.java –Changes a shared variable safely Using a synchronized method


Download ppt "ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 6."

Similar presentations


Ads by Google