Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Thread-specific Storage (TSS) Storage/space (a variable) per thread. –A variable is associated with a thread. –The per-thread variable is never touched.

Similar presentations


Presentation on theme: "1 Thread-specific Storage (TSS) Storage/space (a variable) per thread. –A variable is associated with a thread. –The per-thread variable is never touched."— Presentation transcript:

1 1 Thread-specific Storage (TSS) Storage/space (a variable) per thread. –A variable is associated with a thread. –The per-thread variable is never touched by other threads java.lang.ThreadLocal

2 2 Imagine this Scenario… Different threads –generate different data –store them in a result holder –read them from the result holder. Need to protect the result holder from threads. –A read-write lock to be implemented in the holder. Result holder Thread 1 … Thread 1’s result Thread 2’s result Thread 2 Thread 3

3 3 When does a TSS Work? If each element is paired with a thread and accessed only by the thread… –TSS works well. Easier-to-read code Safer code Result holder Thread 1 … Thread 1’s result Thread 2’s result Thread 2 Thread 3 <--TSS

4 4TSLog.java

5 5 Locking is encapsulated in ThreadLocal No ways to access other threads’ TSS. No code to acquire and release a lock in TSLog –Shorter (easier-to-understand) code –No worry on race conditions and deadlock TSLog Thread 1 Thread 2 Thread 5 <-- TSS … setResult()/printResult() IntegerThread name/id thread 1 thread 2 … … Thread 1’s result Thread 2’s result

6 6 An Architectural View of an OS Inter-process communication Process Scheduling Process Control Subsystem File Subsystem Device Drivers Hardware Control System Call Interface Hardware level Hardware Kernel level User programsLibraries User level Memory Mgt Subsystem

7 7 Host/machine Inter-Process Communication Same system calls/APIs program for both types of communication Process (e.g. JVM, web browser, DB client) Process (e.g. JVM, web server, DB) Host/machine Process (e.g. JVM, web browser, DB client) Process (e.g. JVM, web server, DB) Host/machine

8 8 Protocol Stack Application layer –e.g., HTTP, POP, SMTP, SSH Session layer –e.g., SSL Transport layer –e.g., TCP, UDP Network layer –e.g., IP MAC (data link) and physical layer –e.g., Ethernet, FDDI, ATM, PPP

9 9 Network Protocols A protocol allows multiple processes to talk with each other in an unambiguous way. Each protocol defines… –Communication primitives/commands –Pairs of request and response messages –Message format

10 10 An Example: HTTP GET /index.html HTTP/1.0 HTTP/1.0 200 OK Server: Apache….. Date: Wed, 11 April 2007 HH:MM:SS GMT Content-Type: text/html:charset=ISO… Set-cookie: XXXXX=ZZZZZ Welcome to my home page! …

11 11 Another Example: POP USER jxs<-- client +OK Password required for jxs <-- server PASS mypasswd +OK jxs has 2 messages (300 octets) STAT +OK 2 300 RETR 1 +OK 200 octets email text included here DELE 1 +OK message 1 deleted … QUIT +OK POP server signing off

12 12 Network-related System Calls Socket interface –A part of OS system call interface –A set of functions specific to networking –implements the transport layer –socket() Creates a socket –bind() Names a created socket –connect() Sends out a connection request –listen() Waits for connection requests –accept() Accepts a connection request –select(), read(), write(), close() programmer program Socket interface socket()connect()write() … socket()listen()read() … Network connection

13 13 Java Networking API A set of classes/methods in the java.net package –follows the the socket interface’s design. –implements TCP and UDP. –glues Java programs to the socket interface –makes it easier to implement network systems than using socket system calls directly

14 14Socket Socket –A communication channel to transmit TCP/UDP packets between processes on the same machine or on different machines –Google Desktop »inter-process comm on the same machine –Remote server access (e.g., HTTP and POP) »Inter-process comm on different machines. Client processServer process socket() creates socket() creates socket

15 15 Client processServer process connect() accept() Connects Client processServer process write() read() write() Talk with each other (A TCP connection is full duplex.)

16 16 File Descriptor User-level process open(“foo.txt”) creates How does a process reference and access its sockets? –Using a file descriptor File descriptors –Used to reference various data structures in the kernel e.g., files, directories, character devices, sockets, pipes, etc. fd=10 foo.txt finds and opens it Kernel fd File system pointer 10 fd table

17 17 fd=10 foo.txt Kernel fd File system pointer 11 fd table Client process socket() creates Socket fd=11 TCP Protocol stack NIC Device driver Server process socket() creates Socket fd=30 TCP Protocol stack NIC Device driver Kernel 10 fdpointer 30 fd table

18 18 Process fd=0: standard input (file) Default file descriptors (special files) for every process fd=1: standard output (file) fd=2: standard error output (file) Shell Java program InputStream in = new InputStreamReader(System.in); in.read() stdin: fd=0 > Hi Java System.out.println( “Hi shell” ); System.err.println(“I have a problem”); stdout: fd=1 stderr: fd=2 Hi shell I have a problem.

19 19 IP and Port Number How does a process identify and access a remote process? –10 to 100+ processes on a machine –A huge number of machines on the network (the Internet) How about using process IDs? –Not good The same program uses different pids when running at different times. –If a program is rebooted, it uses a different pid than the one it was using before the reboot.

20 20 A combination of an IP address and port number –An IP address uniquely identifies a particular machine in the network 158.121.105.85 (www.cs.umb.edu) –A port number uniquely identifies a particular process on a machine. A program can use the same port number at different times. –e.g., before and after a reboot. –http://www.cs.umb.edu:80

21 21 Java Socket Server ServerSocket serverSocket = new ServerSocket( 9000 ); Socket socket = serverSocket.accept(); Scanner scanner = new Scanner( socket.getInputStream); PrintWriter writer = new PrintWriter( socket.getOutputStream); Client Socket socket = new Socket( “localhost”, 9000); Scanner scanner = new Scanner( socket.getInputStream); PrintWriter writer = new PrintWriter( socket.getOutputStream);

22 22 Sample Code Networked bank account –A bank account at the server side –A client accesses the bank account through via TCP socket Simple Banking Protocol (SBP) –Commands from a client BALANCE –Get the current balance. The current balance is returned. DEPOSIT X –Deposit amount X. The current (updated) balance is returned. WITHDRAW X –Withdraw amount X. The current (updated) balance is returned. QUIT –Close a TCP connection


Download ppt "1 Thread-specific Storage (TSS) Storage/space (a variable) per thread. –A variable is associated with a thread. –The per-thread variable is never touched."

Similar presentations


Ads by Google