Download Lecture Note 17

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
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
1
Imagine this Scenario…
Thread 1
Result
holder
Thread 2
…
Thread 3
Thread 1’s Thread 2’s
result
result
• 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.
2
When does a TSS Work?
Thread 1
Thread 2
Result
holder
…
Thread 3
<--TSS
Thread 1’s Thread 2’s
result
result
• If each element is paired with a thread and
accessed only by the thread…
– TSS works well.
• Easier-to-read code
• Safer code
3
TSLog.java
4
…
Thread 2
Thread 1’s
result
TSLog
Thread name/id Integer
thread 1
thread 2
…
Thread 5
setResult()/printResult()
…
Thread 1
<-- TSS
Thread 2’s
result
• 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
5
An Architectural View of an OS
User programs
Libraries
User level
System Call Interface
Memory Mgt Subsystem
File Subsystem
Device Drivers
Inter-process
Process
communication
Control
Subsystem Process Scheduling
Kernel level
Hardware Control
Hardware
Hardware level
6
Inter-Process 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)
Host/machine
Process
(e.g. JVM,
web server,
DB)
Host/machine
• Same system calls/APIs program for both types
of communication
7
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
8
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
9
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
<html>
<body>
<title>Welcome to my home page!</title>
…
10
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
11
Network-related System Calls
Socket interface
programmer
– A part of OS system call interface
– A set of functions specific to networking
– implements the transport layer
program
Socket interface
– socket()
• Names a created socket
– connect()
• Sends out a connection request
write()
– bind()
connect()
• Creates a socket
socket()
…
– listen()
• Accepts a connection request
– select(), read(), write(), close()
read()
– accept()
listen()
• Waits for connection requests
socket()
•
…
Network connection
12
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
13
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
14
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.)
15
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”)
creates
foo.txt
finds and
opens it
File system
fd=10
fd
pointer
10
fd table
16
Client process
Socket
fd=11
Kernel
Device
driver
TCP
socket()
creates
Protocol
stack
fd=10
fd table
fd
11
10
creates
File system
Kernel
Device
driver
TCP
socket()
foo.txt
pointer
Server process
NIC
NIC
Protocol stack
Socket
fd=30
fd table
fd
30
pointer
17
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
> Hi Java
stdout: fd=1
Hi shell
I have a problem.
stderr: fd=2
InputStream in =
new InputStreamReader(System.in);
in.read()
System.out.println( “Hi shell” );
System.err.println(“I have a problem”);
18
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.
19
• 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
20
Java Socket
• Server
• Client
•
•
ServerSocket serverSocket =
new ServerSocket( 9000 );
Socket socket =
new Socket( “localhost”, 9000);
Socket socket =
serverSocket.accept();
Scanner scanner = new
Scanner( socket.getInputStream);
Scanner scanner = new
Scanner( socket.getInputStream);
PrintWriter writer = new
PrintWriter( socket.getOutputStream);
PrintWriter writer = new
PrintWriter( socket.getOutputStream);
21
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
22
Related documents