1 / 22

Thread-specific Storage (TSS)

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. Imagine this Scenario…. Different threads generate different data store them in a result holder

kailey
Download Presentation

Thread-specific Storage (TSS)

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. 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. 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. Thread 1 Result holder Thread 2 … Thread 3 Thread 1’sresult Thread 2’sresult

  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 Thread 1 Result holder Thread 2 <--TSS … Thread 3 Thread 1’sresult Thread 2’sresult

  4. TSLog.java

  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 setResult()/printResult() Thread 1 Thread 1’sresult TSLog Thread 2 Thread name/id Integer … thread 1 <-- TSS thread 2 Thread 5 … … Thread 2’sresult

  6. An Architectural View of an OS User programs Libraries User level System Call Interface File Subsystem Memory Mgt Subsystem Kernel level ProcessControlSubsystem Inter-processcommunication Device Drivers Process Scheduling Hardware Control Hardware level Hardware

  7. 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 Host/machine

  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. 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. An Example: HTTP • GET /index.html HTTP/1.0 • HTTP/1.0 200 OKServer: Apache…..Date: Wed, 11 April 2007 HH:MM:SS GMTContent-Type: text/html:charset=ISO…Set-cookie: XXXXX=ZZZZZ<html><body><title>Welcome to my home page!</title>…

  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 octetsemail text included here • DELE 1 • +OK message 1 deleted • … • QUIT • +OK POP server signing off

  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. 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. Socket • 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 process Server process socket() socket() creates creates socket socket

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

  16. File Descriptor • 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. User-level process Kernel open(“foo.txt”) foo.txt creates finds andopens it File system fd=10 fd pointer 10 fd table

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

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

  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. 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. 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. 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

More Related