Transcript

1

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

2

Imagine this Scenario…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.

Resultholder

Thread 1

Thread 1’sresult

Thread 2’sresult

Thread 2

Thread 3

3

When does a TSS Work?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

Resultholder

Thread 1

Thread 1’sresult

Thread 2’sresult

Thread 2

Thread 3

<--TSS

4

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

TSLog

Thread 1

Thread 2

Thread 5

<-- TSS…setResult()/printResult()

IntegerThread name/idthread 1thread 2

… …

Thread 1’sresult

Thread 2’sresult

6

An Architectural View of an OS An Architectural View of an OS

Inter-processcommunication

Process Scheduling

ProcessControl

Subsystem

File Subsystem

Device Drivers

Hardware Control

System Call Interface

Hardware levelHardware

Kernel level

User programs Libraries User level

Memory Mgt Subsystem

7

Host/machine

Inter-Process CommunicationInter-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

Protocol StackProtocol 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 ProtocolsNetwork 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: HTTPAn 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: POPAnother 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

Network-related System CallsNetwork-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

sock

et()

conn

ect(

)

writ

e()

sock

et()

liste

n()

read

()

Network connection

13

Java Networking APIJava 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

SocketSocket• 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()

creates

socket()

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 DescriptorFile 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.txtfinds andopens it

Kernel

fd

File system

pointer10

fd table

17

fd=10

foo.txt

Kernel

fd File systempointer11

fd table

Client process

socket()creates

Socketfd=11

TC

P

Protocolstack

NICDevicedriver

Server process

socket()

creates

Socketfd=30

TC

P

Protocol stack

NICDevicedriver

Kernel

10

fd pointer30

fd table

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

IP and Port NumberIP 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 SocketJava 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 CodeSample 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


Top Related