일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- Javascript
- 수학
- grid
- c
- 소수
- REM
- Prefix Sums
- 미디어 쿼리
- 백준
- 상태
- 반응형 웹
- spring
- Gap
- pandas
- 강화학습
- CSS
- float
- stl
- Photoshop
- box-sizing
- SK바이오사이언스
- 확률
- 알고리즘
- react
- transform
- 포토샵
- Codility
- skt membership
- 통신사할인
- c++
Archives
- Today
- Total
sliver__
[UNIX Input and Output] - readv / writev 함수 본문
728x90
- read / write 함수가 불편한 경우가 있다.
- socket programming 시 다른 버퍼에 존재하는 데이터를 읽어야 하는 경우이다.
- readv / writev 함수는 다른 I/O 버퍼에서 읽어들이는 상황에서 사용한다.
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
ssize_t readv(int fd, const struct iovec *iov, int iovcnt);
ssize_t writev(int fd, const struct iovec *iov, int iovcnt);
struct iovec {
char *iov_base; /* Base address. */
size_t iov_len; /* Length. */
} ;
- 인수 iov는 실제로 struct iovec 유형의 배열이다.
- 각 배열 항목은 특정 크기(iov_len 크기)의 하나의 버퍼(iov_base 기준)를 가리킨다.
- count iovcnt는 함수 호출이 사용해야 하는 배열 요소의 수를 나타낸다.
- 읽거나 쓴 바이트 수가 반환됩니다. 오류가 발생하면 -1이 반환됩니다(errno 포함).
- 예시는 아래와 같다.
1: /* writev.c */
2:
3: #include <stdio.h>
4: #include <fcntl.h>
5: #include <unistd.h>
6: #include <errno.h>
7: #include <string.h>
8: #include <sys/types.h>
9: #include <sys/uio.h>
10:
11: int
12: main(int argc,char **argv) {
13: int z; /* Return status code */
14: static char buf1[] = "by writev(2)";/* Middle buffer */
15: static char buf2[] =">>>"; /* Last buffer */
16: static char buf3[] = "<<<"; /* First buffer */
17: static char buf4[] = "\n"; /* Newline at end */
18: struct iovec iov[4]; /* Handles 4 buffers */
19:
20: iov[0].iov_base = buf3;
21: iov[0].iov_len = strlen(buf3);
22: iov[1].iov_base = buf1;
23: iov[1].iov_len = strlen(buf1);
24: iov[2].iov_base = buf2;
25: iov[2].iov_len = strlen(buf2);
26: iov[3].iov_base = buf4;
27: iov[3].iov_len = strlen(buf4);
28:
29: z = writev(1,&iov[0],4); /* scatter write 4 buffers */
30: if ( z == -1 )
31: abort(); /* Failed */
32:
33: return 0;
34: }
$ make writev
cc -c -D_POSIX_C_SOURCE=199309L -Wall writev.c
cc writev.o -o writev
$ ./writev
<<<by writev(2)>>>
$
728x90
'CS > UNIX' 카테고리의 다른 글
[File Locking] - Lock Types (0) | 2022.10.17 |
---|---|
[UNIX Input and Output] - ttyname / isatty 함수 (0) | 2022.10.11 |
[UNIX Input and Output] - sync 함수 (0) | 2022.10.11 |
[UNIX Input and Output] - Sparse Files (0) | 2022.10.11 |
[UNIX Input and Output] - truncate / ftruncate 함수 (0) | 2022.10.11 |
Comments