38
Advanced I/O ‘15H2 Inshik Song

Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advanced I/O

‘15H2

Inshik Song

Page 2: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Nonblocking I/O

Advanced I/O 2

• Certain system calls can block forever:– read(2) from a particular file, if data isn’t present (pipes,

terminals, network devices)– write(2) to the same kind of file– open(2) of a particular file until a specific condition occurs– read(2) and write(2) of files that have mandatory locking

enabled– certain ioctls(2)– some IPC functions (such as sendto(2) or recv(2))

• Nonblocking I/O lets us issue an I/O operation and not have it block forever. If the operation cannot be completed, return is made immediately with an error noting that the operating would have blocked (EWOULDBLOCK or EAGAIN).

Page 3: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Nonblocking I/O

Advanced I/O 3

• Ways to specify nonblocking mode:– pass O_NONBLOCK to open(2):

– set O_NONBLOCK via fcntl(2):

Page 4: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advanced I/O 4

Page 5: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advanced I/O 5

Case 1: standard output is a regular file

Page 6: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advanced I/O 6

Case 2: standard output is a terminal - polling, is a waste of CPU time

Page 7: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Resource Locking

Advanced I/O 7

• Ways we have learned so far to ensure only one process has exclusive access to a resource:– open file using O_CREAT|O_EXCL, then immediately unlink(2) it

– create a “lockfile” – if file exists, somebody else is using the resource

– use of a semaphore

• What are some problems with each of these?

Page 8: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advisory Locking

Advanced I/O 8

• Applies or removes an advisory lock on the file associated with the file descriptor fd

• Operation can be LOCK_NB and any one of:– LOCK_SH

– LOCK_EX

– LOCK_UN

• Locks entire file

Page 9: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advisory “Record” Locking

Advanced I/O 9

• Record (byte-range) locking is done using fcntl(2), using one of cmd (F_GETLK, F_SETLK or F_SETLKW).

• flock data structure

Page 10: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advisory “Record” Locking

Advanced I/O 10

• Lock types are:– F_RDLCK – Non-exclusive (read) lock; fails if write

lock exists.

– F_WRLCK – Exclusive (write) lock; fails if any lock exists.

– F_UNLCK – Releases our lock on specified range.

Page 11: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advisory “Record” Locking

Advanced I/O 11

• Lock commands:– F_GETLK – Determine whether the lock described by flockptr is

blocked by some other lock. If a lock exists that would prevent ours from being created, the information on that existing lock overwrites the information pointed to by flockptr. If no lock exists that would prevent ours from being created, the structure pointed to by flockptr is left unchanged except for the l_type member, which is set to F_UNLCK.

– F_SETLK – Set the lock described by flockptr.

– F_SETLKW – Blocking version of F_SETLK.

Page 12: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advisory “Record” locking

Advanced I/O 12

Page 13: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Example : Requesting/Releasing a Lock

Advanced I/O 13

Page 14: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advanced I/O 14

Example : Testing a Lock

Page 15: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advanced I/O 15

Example : Deadlock

Page 16: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advanced I/O 16

Example : Deadlock

Page 17: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advisory “Record” Locking

Advanced I/O 17

• Locks are:– released if a process terminates

– released if a file descriptor is closed (!)

– not inherited across fork(2)

– inherited across exec(2)

– released upon exec(2) if close-on-exec is set

Page 18: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advisory “Record” Locking

Advanced I/O 18

• Locks are associated with a file and process pair, not with a file descriptor!

Page 19: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Advanced I/O 19

Page 20: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 20

• Standard I/O loop:

• Suppose you want to read from multiple file descriptors - now what?

Page 21: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 1

• When handling I/O on multiple file descriptors, we have the following options:– blocking mode: open one fd, block, wait (possibly forever),

then test the next fd

– fork and use one process for each, communicate using signals or other IPC

– non-blocking mode: open one fd, immediately get results, open next fd, immediately get results, sleep for some time

Page 22: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 2

• Instead of blocking forever (undesirable), using non-blocking mode (busy-polling is inefficient) or using asynchronous I/O (somewhat limited and significantly more complex), we can:– build a set of file descriptors we’re interested in

– call a function that will return if any of the file descriptors are ready for I/O (or a timeout has elapsed)

Page 23: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 3

• Arguments passed:– which descriptors we’re interested in– what conditions we’re interested in– how long we want to wait

• tvptr == NULL means wait forever• tvptr->tv_sec == tvptr->tv_usec == 0 means don’t wait at all• wait for specified amount of time

• select(2) tells us both the total count of descriptors that are ready as well as which ones are ready.

Page 24: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 4

• filedescriptor sets are manipulated using the FD_* functions/macros

• read/write sets indicate readiness for read/write; except indicates an exception condition (for example OOB data, certain terminal events)

• EOF means ready for read - read(2) will just return 0 (as usual)

• Return value:– −1 : an error occurred

– 0 : no descriptors are ready

– positive value : the number of descriptors that are ready

Page 25: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 5

Page 26: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 6

Page 27: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 7

• Return value:– −1 : an error occurred

– 0 : no descriptors are ready

– positive value : the number of descriptors that are ready

Page 28: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

I/O Multiplexing

Advanced I/O 8

• pselect(2) provides finer-grained timeout control; allows you to specify a signal mask (original signal mask is restored upon return)

• poll(2) provides a conceptually similar interface

Page 29: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Asynchronous I/O

Advanced I/O 9

• System V derived async I/O– limited to STREAMS– enabled via ioctl(2)– uses SIGPOLL

• BSD derived async I/O– limited to terminals and networks– enabled via fcntl(2) (O ASYNC, F SETOWN)– uses SIGIO and SIGURG

• POSIX async I/O– see aio(7) for an example– added complexity

• Mentioned here for completeness’s sake only.

Page 30: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Scatter-Gather I/O

Advanced I/O 10

• Read into and write from multiple noncontiguous buffers in a single function call

Page 31: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Scatter-Gather I/O

Advanced I/O 11

Page 32: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Scatter-Gather I/O

Advanced I/O 12

Generating a 300-megabyte file, each record contains a 100-byte header followed by 200 bytes of data.

Page 33: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Memory Mapped I/O

Advanced I/O 13

• Protection specified for a region:– PROT_READ – region can be read

– PROT_WRITE – region can be written

– PROT_EXEC – region can be executed

– PROT_NONE – region can not be accessed

• flag needs to be one of– MAP_FIXED

– MAP_SHARED

– MAP_PRIVATE

which may be OR’d with other flags (see mmap(2) for details).

Page 34: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Memory Mapped I/O

Advanced I/O 14

Page 35: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Memory Mapped I/O Example

Advanced I/O 15

Page 36: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Memory Mapped I/O Example

Advanced I/O 16

Page 37: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Memory Mapped I/O

Advanced I/O 17

Read/write copy used 8KB buffer without sync for a 300MB file

• Timing results comparing read/write versus mmap/memcpy

Page 38: Advanced I/Ocontents.kocw.net/KOCW/document/2015/chungang/songinshik/... · 2016-09-09 · Resource Locking Advanced I/O 7 • Ways we have learned so far to ensure only one process

Questions?

Advanced I/O 18