57
CS 43: Computer Networks 04: Sockets, Concurrency and Non-blocking I/O Sep 12, 2019

CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

  • Upload
    others

  • View
    1

  • Download
    1

Embed Size (px)

Citation preview

Page 1: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

CS 43: Computer Networks

04: Sockets, Concurrency and Non-blocking I/OSep 12, 2019

Page 2: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Reading Quiz

Slide 2

Page 3: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Last class

• HTTP – GET vs. POST – response messages

• Cookies • HTTP Performance – Persistence vs. Non-persistence

Slide 6

Page 4: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Today

• Under-the-hood look at system calls– Data buffering and blocking

• Inter-process communication• Processes, threads and blocking

Slide 7

Page 5: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Client-Server communication

• Client:– initiates communication– must know the address and port of the server– active socket

• Server:– passively waits for and responds to clients– passive socket

Slide 8

Page 6: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

What is a socket?

An abstraction through which an application may send and receive data, in the same way as a open-file handle allows an application to read and write data to stable storage.

Slide 9

Page 7: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

TCP Socket Procedures: Client Clientcreate a new communication endpoint

actively attempt to establish a connection

receive some data over a connection

send some data over a connection

release the connection

socket()

connect()

send()

recv()

close()Slide 10

Page 8: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

TCP Socket Procedures: Server socket()

bind()

listen()

accept()

recv()

send()

close()

Servercreate a new communication endpoint

attach a local address to a socket

announce willingness to accept connections

block caller until a connection request arrives

receive some data over a connection

send some data over a connection

release the connectionSlide 11

Page 9: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

TCP socket connectionsocket()

bind()

listen()

accept()

recv()

send()

close()

socket()

connect()

send()

recv()

close()

Server

Client

Slide 12

Page 10: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

If the client sends a GET request to the server using send() but forgets to send the last /r/n which of the following can happen?

socket()

bind()

listen()

accept()

recv()

send()

close()

socket()

connect()

send()

recv()

close()

Server Client

A. Server, Client both recv()B. Server send()s,

Client recv()sC. Server recv()s,

Client send()sD. Some other combination

Slide 13

Page 11: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

What happens when we call send and receive?

Slide 14

Page 12: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Recall: Inter-process communication

But first, let’s go over inter-process communication within one machine

Slide 15

Page 13: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Recall Inter-process Communication (IPC)

• Processes must communicate to cooperate

• Must have two mechanisms:

– Data transfer

– Synchronization

• On a single machine:

– Threads (shared memory)

– Message passing

Slide 16

Page 14: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Message Passing (local)

• Operating system mechanism for IPC– send (destination, message_buffer)

– receive (source, message_buffer)

• Data transfer: in to and out of kernel message buffers• Synchronization: ?

send (to, buf) receive (from, buf)Kernel

Process-1 Process-2

Process memory

Slide 17

Page 15: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Where is the synchronization in message passing Inter-process Communication?

A. The OS adds synchronization.

B. Synchronization is determined by the order of sends and receives.

C. The communicating processes exchange synchronization messages (lock/unlock).

D. There is no synchronization mechanism.

Slide 18

Page 16: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Interprocess Communication(non-local)

• Processes must communicate to cooperate

• Must have two mechanisms:– Data transfer– Synchronization

• Across a network:– Threads (shared memory) NOT AN OPTION!– Message passing

Slide 19

Page 17: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Message Passing (network)

• Same synchronization• Data transfer

– Copy to/from OS socket buffer– Extra step across network: hidden from applications

Slide 20

Page 18: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Descriptor Table

OS stores a table, per process, of descriptors

Kernel

For each Process

Slide 21

Page 19: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Descriptors

Slide 22

Page 20: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Descriptor Table

OS stores a table, per process, of descriptors

012…

stdin stdout stderr

For each Process

Kernel

http://www.learnlinux.org.za/courses/build/shell-scripting/ch01s04.html

Slide 23

Page 21: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

socket()

• socket() returns a socket descriptor

• Indexes into table

0

1

2

7

stdin stdout stderr

int sock = socket(AF_INET, SOCK_STREAM, 0);

7

For each Process

KernelSlide 24

Page 22: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

socket()

OS stores details of the socket, connection, and pointers to buffers

0

1

2

7

Family: AF_INET, Type: SOCK_STREAMLocal address: NULL, Local port: NULLSend buffer , Receive buffer

stdin stdout stderr

int sock = socket(AF_INET, SOCK_STREAM, 0);

7

For each Process

KernelSlide 25

Page 23: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

socket()

0

1

2

7

stdin stdout stderr

int sock = socket(AF_INET, SOCK_STREAM, 0);

Family: AF_INET, Type: SOCK_STREAMLocal address: NULL, Local port: NULLSend buffer , Receive buffer

7

OS stores details of the socket, connection, and pointers to buffers

Buffer: Temporary data storage location

For each Process

KernelSlide 26

Page 24: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Socket Buffers

7

int sock = socket(AF_INET, SOCK_STREAM, 0);

Family: AF_INET, Type: SOCK_STREAMLocal address: NULL, Local port: NULLSend buffer , Receive buffer

7

Application buffer / storage space:

For each Process

KernelSlide 27

Page 25: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Socket Buffers

7

int sock = socket(AF_INET, SOCK_STREAM, 0);

Family: AF_INET, Type: SOCK_STREAMLocal address: NULL, Local port: NULLSend buffer , Receive buffer

7

Application buffer / storage space:

Internet

For each Process

KernelSlide 28

Page 26: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Socket Buffers

7

int sock = socket(AF_INET, SOCK_STREAM, 0);

Family: AF_INET, Type: SOCK_STREAMLocal address: NULL, Local port: NULLSend buffer , Receive buffer

7

Application buffer / storage space:

recv(): Move data from socket buffer to process

Internet

For each Process

KernelSlide 29

Page 27: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Socket Buffers

7

int sock = socket(AF_INET, SOCK_STREAM, 0);

Family: AF_INET, Type: SOCK_STREAMLocal address: NULL, Local port: NULLSend buffer , Receive buffer

7

Application buffer / storage space:

send(): Move data from process to socket buffer

Internet

For each Process

KernelSlide 30

Page 28: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Socket Buffers

7

int sock = socket(AF_INET, SOCK_STREAM, 0);

Family: AF_INET, Type: SOCK_STREAMLocal address: NULL, Local port: NULLSend buffer , Receive buffer

7

Application buffer / storage space:

Challenge: Your process does NOT know what is stored here!

Free space? Is data here?

For each Process

KernelSlide 31

Page 29: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

recv()

Kernel

0

1

2

7

int sock = socket(AF_INET, SOCK_STREAM, 0);

(assume we issued a connect() here…)

int recv_val = recv(sock, r_buf, 200, 0);

Family: AF_INET, Type: SOCK_STREAM

Local address: …, Local port: …

Send buffer , Receive buffer

Is data here?

r_buf (size 200)

For each Process

Slide 32

Page 30: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

What should we do if the receive socket buffer is empty? If it has 100 bytes?

int sock = socket(AF_INET, SOCK_STREAM, 0);(assume we connect()ed here…)

int recv_val = recv(sock, r_buf, 200, 0);

Socket buffer (receive) Empty

100 bytes

r_buf (size 200)

For each Process

Kernel

Two Scenarios:

Slide 33

Page 31: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

What should we do if the receive socket buffer is empty? If it has 100 bytes?

Empty 100 BytesA Block BlockB Block Copy 100 bytesC Copy 0 bytes BlockD Copy 0 bytes Copy 100 bytesE Something else

Socket buffer (receive) Empty

Two Scenarios:

100 bytes

r_buf (size 200)int sock = socket(AF_INET, SOCK_STREAM, 0);(assume we connect()ed here…)

int recv_val = recv(sock, r_buf, 200, 0);

For each Process

KernelSlide 34

Page 32: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

What should we do if the send socket buffer is full? If it has 100 bytes?

Socket buffer (send)

Full

s_buf (size 200)

100 bytes

int sock = socket(AF_INET, SOCK_STREAM, 0);(assume we connect()ed here…)

int recv_val = recv(sock, r_buf, 200, 0);

For each Process

Kernel

Two Scenarios:

Slide 35

Page 33: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

What should we do if the send socket buffer is full? If it has 100 bytes?

Full 100 BytesA Return 0 Copy 100 bytesB Block Copy 100 bytesC Return 0 BlockD Block BlockE Something else

Socket buffer (send)

Full

100 bytes

s_buf (size 200)int sock = socket(AF_INET, SOCK_STREAM, 0);(assume we connect()ed here…)

int recv_val = recv(sock, r_buf, 200, 0);

For each Process

Kernel

Two Scenarios:

Slide 36

Page 34: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Blocking Implications

send()• Do not assume that you will recv() all of the bytes

that you ask for.• Do not assume that you are done receiving.• Always receive in a loop!*

recv()• Do not assume that you will send() all of the data you

ask the kernel to copy.• Keep track of where you are in the data you want to

send.• Always send in a loop!*

* Unless you’re dealing with a single byte, which is rare.Slide 37

Page 35: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

ALWAYS check send() return value!

• When send() return value is less than the data size, you are responsible for sending the rest.

Data sent: 0Data to send: 130

send(sock, data, 130, 0);

Data:

Slide 38

Page 36: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

ALWAYS check send() return value!

• When send() return value is less than the data size, you are responsible for sending the rest.

60

Data:

Data:Data sent: 0Data to send: 130send(sock, data, 130, 0);

Data sent: 60Data to send: 130

Slide 39

Page 37: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

ALWAYS check send() return value!

• When send() return value is less than the data size,

you are responsible for sending the rest.

Data sent: 0

Data to send: 130

send(sock, data, 130, 0);60

Data:

Data:

Data sent: 60

Data to send: 130

// Copy the 70 bytes starting from offset 60.

send(sock, data + 60, 130 - 60, 0);

Slide 40

Page 38: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• When send() return value is less than the data size, you are responsible for sending the rest.

?

Repeat until all bytes are sent. (data_sent == data_to_send)…

60

ALWAYS check send() return value!

Data:

Data:Data sent: 0Data to send: 130send(sock, data, 130, 0);

Data sent: 60Data to send: 130// Copy the 70 bytes starting from offset 60.send(sock, data + 60, 130 - 60, 0);

Slide 41

Page 39: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

send()• Blocks when socket buffer

for sending is full

• Returns less than requested size when buffer cannot hold full size

recv()• Blocks when socket buffer

for receiving is empty

• Returns less than requested size when buffer has less than full size

Always check the return value!

Blocking Summary

Slide 42

Page 40: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

send()• Blocks when socket buffer

for sending is full

• Returns less than requested size when buffer cannot hold full size

recv()• Blocks when socket buffer

for receiving is empty

• Returns less than requested size when buffer has less than full size

Always check the return value!

Blocking Summary

Slide 43

Page 41: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• Think you’re the only one talking to that server?

Server

Concurrency

Slide 44

Page 42: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• Think you’re the only one talking to that server?

Web Serverrecv() request

Without Concurrency

Slide 45

Page 43: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• Think you’re the only one talking to that server?

Web Server

recv() request

Client taking its time…

Server Process Blocked!

Ready to send, but server still blocked on

first client.

If only we could handle these connections separately…

Without Concurrency

Slide 46

Page 44: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Web Server

Server fork()s

Child process recv()s

Web Server

Web Server

Services the new client request

Server fork()s

Multiple processes

Slide 47

Page 45: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Processes/Threads vs. Parent(More details in an OS class…)

Spawned Process• Inherits descriptor table• Does not share memory

– New memory address space

• Scheduled independently– Separate execution

context– Can block independently

Spawned Thread• Shares descriptor table• Shares memory

– Uses parent’s address space

• Scheduled independently– Separate execution

context– Can block independently

Slide 48

Page 46: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Processes/Threads vs. Parent(More details in an OS class…)

Spawned Process• Inherits descriptor table• Does not share memory

– New memory address space

• Scheduled independently– Separate execution

context– Can block independently

Spawned Thread• Shares descriptor table• Shares memory

– Uses parent’s address space

• Scheduled independently– Separate execution

context– Can block independently

Often, we don’t need the extra isolation of a separate address space. Faster to skip creating it and share with parent –

threading. Slide 49

Page 47: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• Global variables and static objects are shared– Stored in the static data segment, accessible by any

thread

• Dynamic objects and other heap objects are shared– Allocated from heap with malloc/free or new/delete

• Local variables are not shared – Refer to data on the stack– Each thread has its own stack– Never pass/share/store a pointer to a local variable on

another thread’s stack

Threads & Sharing

Slide 50

Page 48: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• Several benefits– Modularizes code: one piece accepts connections,

another services them– Each can be scheduled on a separate CPU– Blocking I/O can be overlapped

Both processes and threads:

Slide 51

Page 49: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

A. Modular code/separation of concerns.

B. Multiple CPU/core parallelism.

C. I/O overlapping.

D. Some other benefit.

Which benefit is most critical?

Slide 52

Page 50: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• Several benefits– Modularizes code: one piece accepts connections,

another services them

– Each can be scheduled on a separate CPU

– Blocking I/O can be overlapped

• Still not maximum efficiency…– Creating/destroying threads still takes time– Requires memory to store thread execution state

– Lots of context switching overhead

Both processes and threads

Slide 53

Page 51: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• One operation: add a flag to send/recv• Permanently, for socket: fcntl() – “file control”– Allows setting options on file/socket descriptors

int sock, result, flags = 0;sock = socket(AF_INET, SOCK_STREAM, 0);result = fcntl(sock, F_SETFL, flags|O_NONBLOCK)

check result – 0 on success

Non-blocking I/O

Slide 54

Page 52: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• With O_NONBLOCK set on a socket– No operations will block!

• On recv(), if socket buffer is empty:– returns -1, errno is EAGAIN or EWOULDBLOCK

• On send(), if socket buffer is full:– returns -1, errno is EAGAIN or EWOULDBLOCK

Non-blocking I/O

Slide 55

Page 53: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Will this work?

server_socket = socket(), bind(), listen() //non-blocking

connections = []

while (1)

new_connection = accept(server_socket)

if new_connection != -1,

add it to connections

for connection in connections:

recv(connection, …) // Try to receive

send(connection, …) // Try to send, if needed

Slide 56

Page 54: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

A. Yes, this will work.B. No, this will execute too slowly.C. No, this will use too many

resources.D. No, this will still block.

Will this work?

server_socket = socket(), bind(), listen() //non-blocking

connections = []

while (1)

new_connection = accept(server_socket)

if new_connection != -1,

add it to connections

for connection in connections:

recv(connection, …) // Try to receive

send(connection, …) // Try to send, if needed

Slide 57

Page 55: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• Rather than checking over and over, let the OS tell us when data can be read/written

• Create set of file/socket descriptors we want to read and write

• Tell system to block until at least one of those is ready for us to use. The OS worries about selecting which one(s).

Event-based concurrency: select()

Slide 58

Page 56: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

• Rather than checking over and over, let the OS tell us when data can be read/written

• Tell system to block until at least one of those is ready for us to use. The OS worries about selecting which one(s).

• Only one process/thread (or one per core)– No time wasted on context switching

– No memory overhead for many processes/threads

Event-based concurrency

Slide 59

Page 57: CS 43: Computer Networkschaganti/cs43/f19/lecs/04... · 2019-09-12 · What should we do if the send socket buffer is full? If it has 100 bytes? Full 100 Bytes A Return 0 Copy100

Next class

• Network and Distributed Systems• Client-Server Architecture• Peer-to-Peer Architecture

Slide 60