Do You Coding?

[function] dup ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[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์— ์ž‘์„ฑ์ด ๋œ๋‹ค.