signal-driven i/o concepts and steps for using signal-driven i/o udp echo server using signal-driven...

14
Signal-Driven I/O Concepts and steps for using signal- driven I/O UDP echo server using signal-driven I/O Readings UNP Section 6.2, Ch25 1

Upload: jerome-benson

Post on 29-Dec-2015

242 views

Category:

Documents


0 download

TRANSCRIPT

1

Signal-Driven I/O

• Concepts and steps for using signal-driven I/O• UDP echo server using signal-driven I/O

• Readings– UNP Section 6.2, Ch25

2

• Efficiently detecting an asynchronous event (arrival of a connection, arrival of a message, etc) is difficult.– Blocking IO: can block for a long time– Nonblocking IO: do not block for a long time, but must keep polling – tedious

– How do we deal with other types of asynchronous events? E.g. input from keyboard?• Using interrupt• Corresponding to this, we have signal driven IO

Motivation

3

• The kernel raises a signal (SIGIO) when something happens to a file descriptor

• Three steps to use signal-driven I/O with sockets– Establish a signal handler for SIGIO

• Functions signal/sigaction– Set the socket owner

• fcntl with command F_SETOWN– Enable signal-driven I/O for the socket

• fcntl with command O_SETFL (turn on O_ASYNC)• ioctl with request FIOASYNC

Signal-driven I/O

4

Set Socket Owner

• Function fcntl with command F_SETOWN– fcntl(int fd, int cmd, … /* int arg*/)– Set process ID or process group ID to receive SIGIO and

SIGURG signals– Arg > 0: process ID == arg– Arg < 0: process group ID == |arg|

fcntl(sockfd, F_SETOWN, getpid());

5

Enable Signal-Driven I/O for Socket

• Function fcntl with command F_SETFL– Turn on O_ASYNC

• Function ioctl with request FIOASYNC

int flag;flag = fcntl(sockfd, F_GETFL, 0);fcntl(sockfd, F_SETFL, flag | O_ASYNC);

int on = 1;ioctl(sockfd, FIOASYNC, &on);

6

Complication of Signal-Driven I/O

• Signals are not queued when they are blocked– When SIGIO is blocked, at most one SIGIO signal will be

pending even if two pieces of data arrive– No one to one mapping between number of signals and

arrived data

• In handler of SIGIO, we need to handle all arriving data – Read until there is no data (how?)

• Nonblocking I/O needs to be used with signal-driven I/O

7

• For UDP sockets– A datagram arrives– An error occurs

• For TCP sockets– A connection request has completed– A disconnect request has been initiated– A disconnect request has completed– Half of a connection has been shut down– Data has arrived – Data has been sent

• Too many SIGIOs for a TCP socket – rarely used– listening socket

When is SIGIO raised?

8

Echo Server with Signal-Driven I/O

• Examples udp_echo_server.cpp and udp_echo_client.cpp

• A particular use of signal-driven I/O is NTP (network time protocol)– Time sensitive. – A server needs to record the accurate arrival time of a

datagram and return to client.

9

Summary of I/O models

• Blocking• Nonblocking• Multiplexed• Signal driven• Asynchronous

10

Blocking IO

11

Nonblocking IO

12

I/O Multiplexing

13

Signal driven

14

Asynchronous IO