COMP1549 Week 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 48

COMP1549: Advanced Programming

Network and Concurrent Programming

Dr Taimoor Khan
https://mtaimoorkhan.github.io
https://www.isec.group

2nd February 2023 - Week 3

COMP1549: Advanced Programming 1


Announcements
◉ Coursework groups: Based on the requests received from many of you,
we have decided to
Ø Extend the deadline for submission for group members by Friday, i.e.,
February 3, 2023
Ø For those of you who fail to report us by the deadline if they are working
alone or in a group, we will randomly form groups for them

COMP1549: Advanced Programming 2


Network programming

COMP1549: Advanced Programming 3


Computer network
◉ A computer network is an interconnected
collection of autonomous computers

Computer is a network now


COMP1549: Advanced Programming 4
Internet
◉ How is it possible to send bits across
incompatible LANs, WANs and networks?
Ø Solution: protocol software running on each host
and router smooths out the differences between
the different networks
Ø Implements an internet protocol (i.e., set of
rules) that governs how hosts and routers should
cooperate when they transfer data from network
to network
Ø TCP/IP is the protocol for the global IP Internet

COMP1549: Advanced Programming 5


IP Internet
◉ Most famous example of an internet.
◉ Based on the TCP/IP protocol family
Ø Level 3 - IP (Internet protocol) :
v Provides basic naming scheme and unreliable delivery
capability of packets (datagrams) from host-to-host.
Ø Level 4 - UDP (User Datagram Protocol)
v Uses IP to provide unreliable datagram delivery from
process-to-process.
Ø Level 4 - TCP (Transmission Control Protocol)
v Uses IP to provide reliable byte streams from process-
to-process over connections.
◉ Accessed via a mix of Java file I/O and
functions from the sockets interface.
COMP1549: Advanced Programming 6
Network programming
◉ Every network application is based on the
client-server model:
Ø A server process and one or more client processes
Ø Server manages some resource.
Ø Server provides service by manipulating resource for clients.

1. Client sends request


Client Server
process process Resource
4. Client 3. Server sends response 2. Server
handles handles
response request

Note: clients and servers are processes (aka programs) running on


hosts/machines (can be the same or different hosts).

COMP1549: Advanced Programming 7


Client
◉ How does a client find the server?
Ø The IP address in the server socket address
identifies the host (more precisely, an adapter
on the host)
Ø The (well-known) port in the server socket
address identifies the service, and thus implicitly
identifies the server process that performs that
service.
Ø Examples of well-known ports
v Port 7: Echo server
v Port 23: Telnet server
v Port 25: Mail server
v Port 80: Web server

COMP1549: Advanced Programming 8


Server
◉ Servers are long-running processes (daemons).
Ø Created at boot-time (typically) by the init process
(process 1)
Ø Run continuously until the machine is turned off.
◉ Each server waits for requests to arrive on a
well-known port associated with a particular
service.
Ø Port 7: echo server
Ø Port 23: telnet server
Ø Port 25: mail server
Ø Port 80: HTTP server
◉ A machine that runs a server process is also
often referred to as a “server.”
COMP1549: Advanced Programming 9
Programmer’s view
◉ Hosts are mapped to a set of 32-bit IP addresses.
Ø 193.37.244.43
◉ The set of IP addresses is mapped to a set of
identifiers called Internet domain names. [A host
name].
Ø 193.37.244.43 is mapped to www.gre.ac.uk
Ø How do hostnames get matched to IP addresses?
v What is /etc/hosts?
v What is a DNS?
◉ A process on one Internet host can communicate with
a process on another Internet host over a connection.
Ø What is special about addresses 127.0.0.*?
v Each host has a locally defined domain name localhost which
always maps to the loopback address 127.0.0.1

COMP1549: Advanced Programming 10


Internet connections
◉ Clients and servers communicate by sending streams of
bytes over connections:
Ø Point-to-point, full-duplex (2-way communication), and
reliable.
◉ A socket is an endpoint of a connection
Ø Socket address is an IPaddress:port pair
◉ A port is a 16-bit integer that identifies a process:
Ø Ephemeral port: Assigned automatically on client when client
makes a connection request
Ø Well-known port: Associated with some service provided by a
server (e.g., port 80 is associated with Web servers)
◉ A connection is uniquely identified by the socket
addresses of its endpoints (socket pair)
Ø clientaddr:clientport
Ø serveraddress:serverport

COMP1549: Advanced Programming 11


Sockets
◉ Sockets as an abstraction provide a conduit through which
a process can send data out onto a network to another
process
Ø Sockets can be used with both the TCP and the UDP
transport layer protocols
Ø TCP and UDP sockets need IP addresses and port numbers
◉ What is a socket?
Ø To the kernel, a socket is an endpoint of communication.
Ø To an application, a socket is a file descriptor that lets the
application read/write from/to the network
v All I/O devices, including networks, are generally modeled as files
◉ Clients and servers communicate with each other by
reading from, and writing to, socket descriptors
◉ The main distinction between regular file I/O and socket
I/O is how the application “opens” the socket descriptors

COMP1549: Advanced Programming 12


The Java Socket class (1)
Sockets can
1. Connect to a remote machine

2. Send data

3. Receive data

4. Close a connection

5. Bind to a port

6. Listen for incoming connection

7. Accept connections from remote machines


on a bound port
COMP1549: Advanced Programming 13
The Java Socket class (2)
The Socket class supports the
1. Connect to a remote machine

Ø Socket socket = new Socket(…)


2. Send data [socket.write()]
3. Receive data [socket.read()]

4. Close a connection [socket.close()]

What is the kind of data that can be transmitted?


◉ Transmission as a byte stream

Typically a socket is encapsulated in a


InputStream class or a Reader class
COMP1549: Advanced Programming 14
The Java ServerSocket class
The ServerSocket class additionally supports
the [ServerSocket server_socket = new ServerSocket(…)]
5. Bind to a port [server_socket.bind()]

6. Listen for incoming connection


[server_socket.listen()]
7. Accept connections from remote machines

on a bound port [server_socket.accept()]

COMP1549: Advanced Programming 15


Network programming approach
◉ General approach
Ø Create and/or open a socket
Ø Convert a socket to a standard Java I/O class
v Input stream
v Output stream
Ø Use standard Java I/O for all operations
◉ Works for "normal" TCP connection

COMP1549: Advanced Programming 16


Server – example
public class Server {

public static void main(String[] args) throws IOException {


try (ServerSocket listener = new ServerSocket(7000)) {
System.out.println("The date server is running...");
while (true) {
try (Socket socket = listener.accept()) {
PrintWriter out =
new PrintWriter(socket.getOutputStream(), true);
out.println(new Date().toString());
}
}
}
}
}
COMP1549: Advanced Programming 17
Client - example
public class Client {

public static void main(String[] args) throws IOException {


if (args.length != 1) {
System.err.println("Pass the server IP …");
return;
}
Socket socket = new Socket(args[0], 7000);
Scanner in = new Scanner(socket.getInputStream());
System.out.println("Server response: " + in.nextLine());
}
}

COMP1549: Advanced Programming 18


Client server models
Currently we have learned
◉ 1 client and 1 server
Ø Server accepting requests from one client
Next
◉ Many clients and 1 server
Ø Server accepting requests from many clients
◉ Many clients and many servers (peer to peer)
Ø Everyone accepting requests from others

Concurrently listening to many requests through


Threads in Java

COMP1549: Advanced Programming 19


Concurrent programming

COMP1549: Advanced Programming 20


Threads
◉ To develop concurrent processes
Ø Keep things moving when something blocks
Ø Make use of multiple cores

COMP1549: Advanced Programming 21


Threads
Normally we write programs that A multi-threaded program may execute
execute with a single thread of with multiple threads of control - more
control - one thing happens after than one thing can happen at once
another (aka concurrently)

class Joe { class Fred extends Thread {


public void run() { public void run() {
for (int i = 0;i < num; i++) { for (int i = 0;i < num; i++) {
// do something // do something
} }
} }
} }
..... .....
Joe j1 = new Joe(); Fred f1 = new Fred();
j1.run(); f1.start();
for (z = 20; z > 0; z --) { for (z = 20; z > 0; z --) {
// do something // do something
} }

COMP1549: Advanced Programming 22


Threads vs distributed system
◉ Java Threads
Ø operate within a single Java Virtual Machine.
Ø can share memory (i.e., can have references to the same
objects)

◉ Distributed Java Systems


Ø spread over more than one JVM and probably physical
machine
Ø communicate via sockets (perhaps hidden by
technologies such as web services)

COMP1549: Advanced Programming 23


Threads vs process in Java
Max Address
Registers
◉ A process is a JVM instance SP
Stack
Ø Unit of allocation (resources, privileges, etc)
Ø The Process contains the heap
Ø The heap holds all static memory
◉ A thread is a runtime (JVM) state
Ø unit of execution (PC, SP, registers)
Ø "Java Stack" (runtime stack)

Ø Stored registers Heap


(Dynamic Data)
Ø Local variables
Static Data
Ø Instruction pointer
Constants
◉ Each process has one or more threads PC
Code
◉ Each thread belong to one process Low Address
Process address space

COMP1549: Advanced Programming


Threads in Java’s memory model
TCB for
◉ Processes define an address Thread1
space, which is shared by PC Registers (T2)
threads SP
State Stack (T2)
Registers
◉ Process Control Block (PCB) …
contains process-specific
information Registers (T1)
• Owner, PID, heap pointer,
Stack (T1)
priority, active thread, and TCB for
pointers to thread information Thread2
PC Heap
◉ Thread Control Block (TCB) SP (Dynamic Data)
contains thread-specific State
Registers
information …
Static Data

• Stack pointer, PC, thread state Constants


(running, …), register values, Code
a pointer to PCB, …
Process address space

COMP1549: Advanced Programming 25


Threads in Java
◉ Every thread is created as an instance of class
java.lang.Thread
◉ You either extend Thread class
public class MyThread1 extends Thread {}
◉ or implement Runnable interface
public class MyThread2 implements Runnable {}
◉ In either case you must provide a method called
run() which is executed when the thread runs
e.g.
public void run() {
for (int i=0; i < 5; i++)
System.out.println("I'm a thread " + i);
}

COMP1549: Advanced Programming 26


Thread states

Active thread

scheduled
runnable running

Ex s
)
t(

or
it to
a r

s p(
t

ted
s

ru )
I/

n(
O

ues

)
notify()

av
notifyAll() wait()

req
ai
new time is up sleep() dead
la
new

I/O
resume() suspend()
t = new Thread(); bl
e

blocked

Blocked for I/O


waiting/sleeping

COMP1549: Advanced Programming 27


Creating threads
class MyThread1 extends Thread { what will
public void run() { this output?
for (int i=0; i < 5; i++)
System.out.println("I'm a thread " + i);
}
}
public class CreateThread1 {
what would happen
public static void main (String[] args) {
if t.start() was
Thread t = new MyThread1();
replaced by t.run()
t.start();
?
for (int i=0; i < 5; i++)
System.out.println("I'm main " + i);
}
}
COMP1549: Advanced Programming 28
Joining threads
As well as diverging Threads can be joined using join()
class MyThread3 extends Thread {
public void run() {
for (int i=0; i < 5; i++) what can you
System.out.println("I'm a thread " + i);
predict about
}}
public class JoiningThreads { what this will
public static void main (String[] args) { output?
Thread t = new MyThread3();
System.out.println("Threads diverging");
t.start();
for (int i=0; i < 5; i++)
System.out.println("I'm main " + i);
try { wait for thread
t to finish
t.join();
} catch (InterruptedException ie) {ie.printStackTrace(); }
System.out.println("Threads re-joined");
}}

COMP1549: Advanced Programming 29


Threads sharing objects
◉ New threads always start as instances of
separate objects
◉ Once started different threads of control can
pass through shared objects
Thread instance 2
Thread instance 1 Shared Object run() {
run() {

}
◉ Shared Object’s code is executed by two
different Threads
COMP1549: Advanced Programming 30
Shared objects Java memory model

Thread Stack Thread Stack


methodOne() methodOne()
Local Variable 1 Local Variable 1

Local Variable 2 Local Variable 2

methodTwo() methodTwo()
Local Variable 1 Local Variable 1
www.javaworlld.com

Object 1 Object 2 Object 3 Object 4 Object 5

Heap

JVM

COMP1549: Advanced Programming 31


Example – shared objects (1)
class Shared2 {

private int counter = 0;


sleep for a random
public void quickNap() { amount of time - will
try {Thread.sleep((int)(Math.random() * 2)); } make the threading
catch(InterruptedException ie) {}
}
more obvious
public int increment() {
counter = 0;
for (int i = 0; i < 1000; i++) {
counter++; taking this code on
quickNap(); }
return counter; face value what do
} you think
public int decrement() { increment() and
counter = 0;
for (int i = 0; i < 1000; i++) { decrement() will
counter--; return?
quickNap(); }
return counter;
}}

COMP1549: Advanced Programming 32


Example – shared objects (2)
class CountDown2 extends Thread {
private Shared2 myShared;
a reference to the shared
private JTextArea display; object is passed to the
constructor and stored
public CountDown2(Shared2 s, JTextArea display) {
myShared = s;
instances of CountDown2
this.display = display;
call the decrement()
} method of the shared
public void run() { object
int tempVal = myShared.decrement();
display.append("Down reports:" + tempVal + "\n");
}
}
COMP1549: Advanced Programming 33
Example – shared objects (3)
class CountUp2 extends Thread {
private Shared2 myShared;
private JTextArea display;
public CountUp2(Shared2 s, JTextArea display) {
myShared = s;
this.display = display; CountUp2 is very similar to
} the CountDown2 except it
calls the increment() method
on the shared object
public void run() {
int tempVal = myShared.increment();
display.append("Up reports:" + tempVal + "\n");
}
}

COMP1549: Advanced Programming 34


Example – shared objects (4)
public class ThreadApp2 extends JFrame implements ActionListener {
private JButton one = new JButton("Go");
private JTextArea display = new JTextArea(10,20);
private Shared2 s = new Shared2();
private CountDown2 down;
private CountUp2 up;
public ThreadApp2() { ThreadApp2
setLayout(new FlowLayout());
add(one); • provides a GUI
one.addActionListener(this); • creates an instance
add(new JScrollPane(display)); of the shared objects
……..
} • creates and starts the
public void actionPerformed(ActionEvent event) { Threads
down = new CountDown2(s, display);
up = new CountUp2(s, display);
down.start();
up.start();
}
public static void main(String[] args) {
ThreadApp2 me = new ThreadApp2();
me.setVisible(true);
}}

COMP1549: Advanced Programming 35


Example run (1)

◉ Why aren’t the results always 1000?


◉ How come the threads can be run again after
they finish?
COMP1549: Advanced Programming 36
Example run (2)
Why aren’t the results always 1000?

• Because both threads share the same


object (Shared2)
• Counter is therefore a shared variable
and continuously changing

How come the threads can be run again after they finish?
• There’re not!
• New threads are created each time Go
is pressed
• But all are sharing the same Shared2
object

COMP1549: Advanced Programming 37


Visualizing shared objects
ThreadApp2
actionPerformed() {

CountDown2
CountUp2
run() {
Shared2 run() {
int counter
increment() {

}
}
}
decrement() {

COMP1549: Advanced Programming 38


Race condition and thread safety
◉ The previous program shows an example of a
race condition where the interleaving of actions
from different threads cause corruption or
unpredictable results
◉ The problem is caused because Shared2 isn’t a
Thread-safe class
Ø Being Thread-safe means that even if an instance of
the class is shared by multiple threads no race
condition can occur
Ø If you are writing code for classes that may have
their instances shared amongst threads then you
must consider the issue of thread-safety

COMP1549: Advanced Programming 39


Thread safety
◉ Java allows objects to be locked to prevent problems
when they are accessed by multiple threads

◉ Object locking is controlled by use of the keyword


synchronized

◉ The most common use of synchronized is to apply it


to a method e.g.

public synchronized int increment() {


counter = 0;
…..
}

COMP1549: Advanced Programming 40


Semantics of synchronized
◉ For a thread to execute a synchronized method it
must obtain a lock on the object (the actual instance)
to which the method belongs
while Thread1 holds
◉ If a thread has the object lock then the lock on the
any other threads wanting to execute instance of Shared2
synchronized methods will enter a any other threads
“seeking a lock state” wanting to execute its
I’ve got :Shared2 synchronized
the lock int counter methods must wait
synchronized increment() {
T
h
r hurry up
e }
a synchronized decrement() {
d
1
}

COMP1549: Advanced Programming 41


Threads and locks
◉ The lock is on the object on which the synchronized
method is called.
◉ If there are multiple instances of a class containing
synchronized methods, then different threads can
be executing synchronized methods on different
instances
◉ If one thread is executing a synchronized method
there’s nothing to stop other threads executing non-
synchronized method of the same object
◉ A thread releases the lock on an object when it exits
from the synchronized code
◉ It keeps the lock while it is sleeping

COMP1549: Advanced Programming 42


When to use synchronized?
◉ Should all methods be synchronized?
Ø It would certainly ensure thread-safety but at a
cost of performance.
Ø Synchronized can be applied to a block of code
as well as a whole method
Ø Keep the synchronized code a minimum
Ø Document whether or not your code is thread-
safe
Ø Providing both a thread-safe and non-thread-
safe version of a class may be sensible

COMP1549: Advanced Programming 43


A new alternative
◉ Java >= 5 introduced changes to the Java
API to enhance support for threading
◉ These have been enhanced since and are
becoming more widely used
◉ New classes are in package

java.util.concurrent
◉ Class ReentrantLock supports explicit locking

as an alternative to using synchronized


blocks

COMP1549: Advanced Programming 44


Example - reentrant new way
class Shared3 {
private int counter = 0;
private final Lock theLock =
older way - using synchronized new ReentrantLock();

class Shared3 { public int increment() {


private int counter = 0; int saveCounter = 0;
theLock.lock();
public synchronized int increment() {
counter = 0; try {
for (int i = 0; i < 1000; i++) { counter = 0;
for (int i = 0; i < 1000; i++) {
counter++;
counter++;
quickNap(); quickNap();
} }
return counter; saveCounter = counter;
} } finally {
theLock.unlock();
// decrement return saveCounter;
}
}
}
// similar changes to decrement
}
COMP1549: Advanced Programming 45
Deadlock
◉ Any systems where concurrent threads or
processes can lock shared resources are prone to
deadlock
◉ An obvious example is deadly embrace between
two threads
I’ve got object
I’ve got 2 and now I
object 1 and
want object 1
now I want
T object 2
h object 1 T
r h
e r
a e
d a
1 object 2 d
2
Error prone as developer may forget to lock/unlock
COMP1549: Advanced Programming 46
Avoid deadlock
◉ Keep locking to a minimum
◉ Don’t hold locks for longer than necessary
◉ Try to make sure resources are always
locked in the same order
◉ Use tryLock() with a timeout and if necessary
release your locks and try again
◉ Test very thoroughly!!

COMP1549: Advanced Programming 47


End of week 3!

COMP1549: Advanced Programming 48

You might also like