← Documents Documentation/userspace-api/media/v4l/func-select.rst GitHub 원문 ↗

Linux 6.18.37 · Userspace API / Media / V4L

V4L2 select()

V4L2 descriptor의 read·write·event 준비 상태와 select 오류 계약을 설명합니다.

Source pathDocumentation/userspace-api/media/v4l/func-select.rst
Source versionLinux v6.18.37
TranslationDUJINLABS 전문 번역 + 해설

요약·해설과 원문, 전문 번역을 서로 분리했습니다. API 이름, symbol, source path는 원문 표기를 사용합니다.

1. 요약·해설

원문의 핵심 논리와 kernel programming 관점의 보충 설명입니다. 아래의 전문 번역과는 별도로 작성했습니다.

요약·해설

func-select.rst:1-116

`select()`은 readfds, writefds와 exceptfds로 V4L2 준비 상태를 나누지만 POLLERR를 표현할 수 없어 잘못된 streaming 상태의 오류가 후속 I/O에서 나타날 수 있습니다.

2. 영어 원문 전체

번역 기준이 된 Linux v6.18.37 원문입니다. 줄 번호는 이 버전의 파일 좌표입니다.

원문 전체 펼치기
1 .. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
2 .. c:namespace:: V4L
3
4 .. _func-select:
5
6 *************
7 V4L2 select()
8 *************
9
10 Name
11 ====
12
13 v4l2-select - Synchronous I/O multiplexing
14
15 Synopsis
16 ========
17
18 .. code-block:: c
19
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23
24 .. c:function:: int select( int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout )
25
26 Arguments
27 =========
28
29 ``nfds``
30 The highest-numbered file descriptor in any of the three sets, plus 1.
31
32 ``readfds``
33 File descriptions to be watched if a read() call won't block.
34
35 ``writefds``
36 File descriptions to be watched if a write() won't block.
37
38 ``exceptfds``
39 File descriptions to be watched for V4L2 events.
40
41 ``timeout``
42 Maximum time to wait.
43
44 Description
45 ===========
46
47 With the :c:func:`select()` function applications can suspend
48 execution until the driver has captured data or is ready to accept data
49 for output.
50
51 When streaming I/O has been negotiated this function waits until a
52 buffer has been filled or displayed and can be dequeued with the
53 :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. When buffers are already in
54 the outgoing queue of the driver the function returns immediately.
55
56 On success :c:func:`select()` returns the total number of bits set in
57 ``fd_set``. When the function timed out it returns
58 a value of zero. On failure it returns -1 and the ``errno`` variable is
59 set appropriately. When the application did not call
60 :ref:`VIDIOC_QBUF` or
61 :ref:`VIDIOC_STREAMON` yet the :c:func:`select()`
62 function succeeds, setting the bit of the file descriptor in ``readfds``
63 or ``writefds``, but subsequent :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`
64 calls will fail. [#f1]_
65
66 When use of the :c:func:`read()` function has been negotiated and the
67 driver does not capture yet, the :c:func:`select()` function starts
68 capturing. When that fails, :c:func:`select()` returns successful and
69 a subsequent :c:func:`read()` call, which also attempts to start
70 capturing, will return an appropriate error code. When the driver
71 captures continuously (as opposed to, for example, still images) and
72 data is already available the :c:func:`select()` function returns
73 immediately.
74
75 When use of the :c:func:`write()` function has been negotiated the
76 :c:func:`select()` function just waits until the driver is ready for a
77 non-blocking :c:func:`write()` call.
78
79 All drivers implementing the :c:func:`read()` or :c:func:`write()`
80 function or streaming I/O must also support the :c:func:`select()`
81 function.
82
83 For more details see the :c:func:`select()` manual page.
84
85 Return Value
86 ============
87
88 On success, :c:func:`select()` returns the number of descriptors
89 contained in the three returned descriptor sets, which will be zero if
90 the timeout expired. On error -1 is returned, and the ``errno`` variable
91 is set appropriately; the sets and ``timeout`` are undefined. Possible
92 error codes are:
93
94 EBADF
95 One or more of the file descriptor sets specified a file descriptor
96 that is not open.
97
98 EBUSY
99 The driver does not support multiple read or write streams and the
100 device is already in use.
101
102 EFAULT
103 The ``readfds``, ``writefds``, ``exceptfds`` or ``timeout`` pointer
104 references an inaccessible memory area.
105
106 EINTR
107 The call was interrupted by a signal.
108
109 EINVAL
110 The ``nfds`` argument is less than zero or greater than
111 ``FD_SETSIZE``.
112
113 .. [#f1]
114 The Linux kernel implements :c:func:`select()` like the
115 :c:func:`poll()` function, but :c:func:`select()` cannot
116 return a ``POLLERR``.
117

3. 한국어 전문 번역

영어 원문의 문단 순서와 의미를 유지한 전체 번역입니다. 코드, 함수명, symbol과 URL은 원문 표기를 유지합니다.

동기 I/O multiplexing 선언

1-43

`v4l2-select`는 여러 descriptor의 동기 I/O 준비 상태를 기다립니다. `<sys/time.h>`, `<sys/types.h>`, `<unistd.h>`를 포함하고 `int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)`을 호출합니다.

`select()` 인자
항목설명
`nfds`세 set에 든 descriptor 중 가장 큰 번호에 1을 더한 값입니다.
`readfds``read()`가 block하지 않을 descriptor를 감시합니다.
`writefds``write()`가 block하지 않을 descriptor를 감시합니다.
`exceptfds`V4L2 event가 발생한 descriptor를 감시합니다.
`timeout`최대 대기 시간입니다.

Read, write와 V4L2 event 준비 상태를 별도 set으로 감시합니다.

.. SPDX-License-Identifier: GFDL-1.1-no-invariants-or-later
.. c:namespace:: V4L

.. _func-select:

*************
V4L2 select()
*************

Name
====

v4l2-select - Synchronous I/O multiplexing

Synopsis
========

.. code-block:: c

    #include <sys/time.h>
    #include <sys/types.h>
    #include <unistd.h>

.. c:function:: int select( int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout )

Arguments
=========

``nfds``
  The highest-numbered file descriptor in any of the three sets, plus 1.

``readfds``
  File descriptions to be watched if a read() call won't block.

``writefds``
  File descriptions to be watched if a write() won't block.

``exceptfds``
  File descriptions to be watched for V4L2 events.

``timeout``
  Maximum time to wait.

Streaming과 read/write 준비 상태

44-84

Application은 `select()`로 driver가 capture data를 준비하거나 output data를 받을 수 있을 때까지 실행을 멈출 수 있습니다. Streaming I/O에서는 buffer가 채워지거나 display되어 `VIDIOC_DQBUF`로 꺼낼 수 있을 때까지 기다립니다. Driver outgoing queue에 buffer가 이미 있으면 즉시 반환합니다.

성공하면 반환된 `fd_set`에 설정된 bit의 총수, timeout이면 0, 실패하면 -1과 `errno`를 반환합니다. 아직 `VIDIOC_QBUF` 또는 `VIDIOC_STREAMON`을 호출하지 않았어도 `select()`는 성공하고 readfds 또는 writefds에 descriptor bit를 설정하지만 뒤이은 `VIDIOC_DQBUF`는 실패합니다.

Read I/O가 협상됐고 아직 capture하지 않으면 `select()`가 capture를 시작합니다. 시작 실패 자체는 select 성공으로 나타나며, 다시 capture 시작을 시도하는 다음 `read()`가 적절한 오류를 반환합니다. Continuous capture에서 data가 이미 있으면 즉시 반환합니다. Write I/O에서는 non-blocking `write()`가 가능할 때까지 기다립니다.

Read, write 또는 streaming I/O를 구현하는 모든 driver는 `select()`도 지원해야 합니다.

`select()` 준비 상태
Read/write/event descriptor set 구성가장 큰 fd + 1을 `nfds`로 설정`select()`에서 준비 또는 timeout 대기반환된 set의 bit 확인Read·write·DQEVENT 또는 DQBUF 수행

Set 종류에 따라 다음에 안전하게 호출할 I/O를 결정합니다.

Description
===========

With the :c:func:`select()` function applications can suspend
execution until the driver has captured data or is ready to accept data
for output.

When streaming I/O has been negotiated this function waits until a
buffer has been filled or displayed and can be dequeued with the
:ref:`VIDIOC_DQBUF <VIDIOC_QBUF>` ioctl. When buffers are already in
the outgoing queue of the driver the function returns immediately.

On success :c:func:`select()` returns the total number of bits set in
``fd_set``. When the function timed out it returns
a value of zero. On failure it returns -1 and the ``errno`` variable is
set appropriately. When the application did not call
:ref:`VIDIOC_QBUF` or
:ref:`VIDIOC_STREAMON` yet the :c:func:`select()`
function succeeds, setting the bit of the file descriptor in ``readfds``
or ``writefds``, but subsequent :ref:`VIDIOC_DQBUF <VIDIOC_QBUF>`
calls will fail. [#f1]_

When use of the :c:func:`read()` function has been negotiated and the
driver does not capture yet, the :c:func:`select()` function starts
capturing. When that fails, :c:func:`select()` returns successful and
a subsequent :c:func:`read()` call, which also attempts to start
capturing, will return an appropriate error code. When the driver
captures continuously (as opposed to, for example, still images) and
data is already available the :c:func:`select()` function returns
immediately.

When use of the :c:func:`write()` function has been negotiated the
:c:func:`select()` function just waits until the driver is ready for a
non-blocking :c:func:`write()` call.

All drivers implementing the :c:func:`read()` or :c:func:`write()`
function or streaming I/O must also support the :c:func:`select()`
function.

For more details see the :c:func:`select()` manual page.

반환 후 set 상태와 오류

85-116

성공하면 세 반환 set에 포함된 descriptor 수를 반환하고 timeout이면 0입니다. 오류면 -1과 `errno`를 반환하며 이때 descriptor set들과 `timeout`의 내용은 undefined입니다.

`select()` 오류
항목설명
`EBADF`Set 하나 이상에 열려 있지 않은 descriptor가 있습니다.
`EBUSY`Driver가 multiple read/write stream을 지원하지 않고 장치가 이미 사용 중입니다.
`EFAULT``readfds`, `writefds`, `exceptfds` 또는 `timeout` pointer가 접근 불가능한 memory를 참조합니다.
`EINTR`Signal이 호출을 중단했습니다.
`EINVAL``nfds`가 0보다 작거나 `FD_SETSIZE`보다 큽니다.

Descriptor set, memory, signal과 nfds 범위를 검사합니다.

Linux kernel의 `select()` 구현은 `poll()`과 유사하지만 `POLLERR`를 직접 반환할 수 없습니다. 따라서 잘못된 streaming 상태가 descriptor-ready처럼 보이고 실제 후속 I/O에서 오류가 드러날 수 있습니다.

Return Value
============

On success, :c:func:`select()` returns the number of descriptors
contained in the three returned descriptor sets, which will be zero if
the timeout expired. On error -1 is returned, and the ``errno`` variable
is set appropriately; the sets and ``timeout`` are undefined. Possible
error codes are:

EBADF
    One or more of the file descriptor sets specified a file descriptor
    that is not open.

EBUSY
    The driver does not support multiple read or write streams and the
    device is already in use.

EFAULT
    The ``readfds``, ``writefds``, ``exceptfds`` or ``timeout`` pointer
    references an inaccessible memory area.

EINTR
    The call was interrupted by a signal.

EINVAL
    The ``nfds`` argument is less than zero or greater than
    ``FD_SETSIZE``.

.. [#f1]
   The Linux kernel implements :c:func:`select()` like the
   :c:func:`poll()` function, but :c:func:`select()` cannot
   return a ``POLLERR``.