Do You Coding?
[function] dup ํจ์ ์์๋ณด๊ธฐ ๋ณธ๋ฌธ

๐ ๋งค๋ด์ผ (Linux)
NAME
dup - duplicate a file descriptor
: ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ฅผ ๋ณต์
SYNOPSIS
#include <unistd.h>
int dup(int oldfd);
DESCRIPTION
The dup() system call creates a copy of the file descriptor oldfd, using the lowest-numbered
unused file descriptor for the new descriptor.
After a successful return, the old and new file descriptors may be used interchangeably. They
refer to the same open file description (see open(2)) and thus share file offset and file staโ
tus flags; for example, if the file offset is modified by using lseek(2) on one of the file
descriptors, the offset is also changed for the other.
The two file descriptors do not share file descriptor flags (the close-on-exec flag). The
close-on-exec flag (FD_CLOEXEC; see fcntl(2)) for the duplicate descriptor is off.
: dup() ์์คํ ํธ์ถ์ ์ ๋์คํฌ๋ฆฝํฐ์ ๋ํด ๊ฐ์ฅ ๋ฎ์ ๋ฒํธ์ ๋ฏธ์ฌ์ฉ ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ฅผ ์ฌ์ฉํ์ฌ
ํ์ผ ๋์คํฌ๋ฆฝํฐ oldfd์ ๋ณต์ฌ๋ณธ์ ๋ง๋ญ๋๋ค.
์ฑ๊ณต์ ์ผ๋ก ๋ฐํ๋ ํ์๋ ์ด์ ํ์ผ ๋์คํฌ๋ฆฝํฐ์ ์ ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ฅผ ์ํธ ๊ตํํ์ฌ ์ฌ์ฉํ ์ ์์ต๋๋ค.
ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ ๋์ผํ ์ด๋ฆฐ ํ์ผ ๋์คํฌ๋ฆฝํฐ(open(2))๋ฅผ ์ฐธ์กฐํ๋ฏ๋ก ํ์ผ ์คํ์ ๊ณผ ํ์ผ ์ํ ํ๋๊ทธ๋ฅผ
๊ณต์ ํฉ๋๋ค. ์๋ฅผ ๋ค์ด, ํ์ผ ๋์คํฌ๋ฆฝํฐ ์ค ํ๋์์ lseek(2)๋ฅผ ์ฌ์ฉํ์ฌ ํ์ผ ์คํ์ ์ ์์ ํ๋ฉด
๋ค๋ฅธ ํ์ผ ๋์คํฌ๋ฆฝํฐ์์๋ ์คํ์
์ด ๋ณ๊ฒฝ๋ฉ๋๋ค.
๋ ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ ํ์ผ ๋์คํฌ๋ฆฝํฐ ํ๋๊ทธ(close-on-exec ํ๋๊ทธ)๋ฅผ ๊ณต์ ํ์ง ์์ต๋๋ค.
์ค๋ณต ๋์คํฌ๋ฆฝํฐ์ ๋ํ close-on-exec ํ๋๊ทธ(FD_CLOEXEC; fcntl(2) ์ฐธ์กฐ)๋ off์ ๋๋ค.
RETURN VALUE
On success, these system calls return the new file descriptor. On error, -1 is returned, and
errno is set appropriately.
: ์ฑ๊ณต ์ ์ด๋ฌํ ์์คํ ํธ์ถ์ ์ ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ฅผ ๋ฐํํฉ๋๋ค.
์ค๋ฅ ์ -1์ด ๋ฐํ๋๊ณ errno๊ฐ ์ ์ ํ๊ฒ ์ค์ ๋ฉ๋๋ค.
๐ ํจ์ ์ค๋ช
dup ํจ์๋ fd๋ก ์ ๋ฌ๋ฐ์ ํ์ผ ์์ ์๋ฅผ ๋ณต์ ํ์ฌ ๋ฐํํ๋ค. ํ์ฌ ์ฌ์ฉํ๊ณ ์์ง ์์ fd ์ค ๊ฐ์ฅ ๋ฎ์ fd๋ฅผ ๋ฐํํ๋ฉฐ,
ํด๋น fd๋ ์ ๋ฌ๋ฐ์ ํ์ผ ์์ ์๊ฐ ๊ฐ๋ฆฌํค๋ ํ์ผ (๋๋ ํ์ค์ ์ถ๋ ฅ)์ ๋์ผํ๊ฒ ๊ฐ๋ฆฌํจ๋ค.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
int main()
{
int fd1, fd2;
fd1 = open("a.txt",O_RDWR);
fd2 = dup(fd1);
write(fd2, "abc", 3);
printf("fd1 : %d, fd2 : %d", fd1, fd2);
close(fd1);
close(fd2);
return (0);
}
์ด ํ๋ก๊ทธ๋จ์ a.txt์ ํ์ผ์ ์ด์ด ํด๋น ํ์ผ ๋์คํฌ๋ฆฝํฐ๋ฅผ fd1์ ์ ์ฅํ๊ณ ,
dup์ ์ด์ฉํด fd1์ ํ์ผ ๋์คํฌ๋ฆฝํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ํ์ผ์ fd2๋ ๊ฐ๋ฆฌํค๊ฒ ํ๋ฉฐ, fd2๋ฅผ fd1์ ๋ค์ ์ซ์๋ก ๋ถ์ฌํ๋ค.
๊ฒฐ๊ณผ - std_output)
| fd1 : 3, fd2 : 4 |
๊ฒฐ๊ณผ - a.txt)
| abc |


๊ทธ๋ฆผ์ผ๋ก ์ค๋ช ์ ๋ง๋ถ์ด์๋ฉด, ์ฒซ๋ฒ์งธ ๊ทธ๋ฆผ์์ fd1์ ํ์ค์ ์ถ๋ ฅ ๋ฐ ์๋ฌ ์ดํ ๊ฐ์ฅ ์์ fd์ธ 3์ผ๋ก ๋ฐฐ์น๋๊ณ ,
open์ ํตํด a.txt์ ํ์ผ ๋์คํฌ๋ฆฝํฐ๊ฐ ๋๋ค. ๊ทธ๋ฆฌ๊ณ , ์ฐ๋ฆฌ๋ dup์ ํตํด fd2๋ฅผ ์์ฑํด์ฃผ๋๋ฐ, ์ด๋ 3 ์ดํ ๊ฐ์ฅ ์์
ํ์ผ ๋์คํฌ๋ฆฝํฐ ์ซ์์ธ 4๊ฐ ๋ถ์ฌ๋๋ฉฐ, fd1์ด ๋ณต์ ๋์๊ธฐ์, fd1์ด ๊ฐ๋ฆฌํค๋ a.txt๋ฅผ ํจ๊ป ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
๊ทธ๋ฆฌํ์ฌ, fd2์ write๋ฅผ ํ๊ฒ๋๋ฉด, a.txt์ ์์ฑ์ด ๋๋ค.
'CS & Engineering > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [function] execve ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.30 |
|---|---|
| [function] dup2 ํจ์ ์์๋ณด๊ธฐ (2) | 2024.05.28 |
| [function] access ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.24 |
| [function] strerror ํจ์ ์์๋ณด๊ธฐ (0) | 2024.05.23 |
| [function] perror ํจ์ ์์๋ณด๊ธฐ (2) | 2024.05.20 |