기본 콘텐츠로 건너뛰기

기본으로 열리는 FD

Linux에서 기본으로 열리는 FD는 흔히 표준입력(0), 표준출력(1), 표준오류(2)로 알고 있으며, 각 FD는 0, 1, 2이다. 이는 POSIX에서 STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO라는 매크로로 정의하였다. 과연 그뿐일까?

아래와 같은 프로그램을 만들어 컴파일 한 뒤 돌려보자.

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>

int
main(int,char**)
{
    printf("%d\n",socket(AF_INET, SOCK_STREAM, 0));
    return 0;
}


컴파일하여 쉘에서 바로 실행하면 3을 얻을 수 있다. -1이 나온다면 버려. ㅡ_-)
$./fdcnt
3


gdb에서 실행하면 어떻게 될까?
$gdb ./fdcnt
GNU gdb Red Hat Linux (6.6-43.fc8rh)
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu"...
(no debugging symbols found)
Using host libthread_db library "/lib64/libthread_db.so.1".
(gdb) r
Starting program: /home/daldev/purewell/tmp/fdcnt
6

Program exited normally.


헉! 갑자기 3개나 뻥튀기 했네? 뭐지? 브레이크 걸고 /proc/[pid]/fd에서 그 내용을 봤다.

$ ls -al /proc/[pid]/fd/

합계 0
dr-x------ 2 purewell daldev 0 2008-03-24 18:20 .
dr-xr-xr-x 6 purewell daldev 0 2008-03-24 18:20 ..
lrwx------ 1 purewell daldev 64 2008-03-24 18:20 0 -> /dev/pts/1
lrwx------ 1 purewell daldev 64 2008-03-24 18:20 1 -> /dev/pts/1
lrwx------ 1 purewell daldev 64 2008-03-24 18:20 2 -> /dev/pts/1
lr-x------ 1 purewell daldev 64 2008-03-24 18:20 3 -> pipe:[839126]
l-wx------ 1 purewell daldev 64 2008-03-24 18:20 4 -> pipe:[839126]

lr-x------ 1 purewell daldev 64 2008-03-24 18:20 5 -> /home/daldev/purewell/tmp/fdcnt


파이프라는군. GDB와 통신을 위해서 열린 것 같다. 혹시나 해서 GDB쪽도 봤다.
$ ls -al /proc/[gdb-pid]/fd
합계 0
dr-x------ 2 purewell daldev 0 2008-03-24 18:20 .
dr-xr-xr-x 6 purewell daldev 0 2008-03-24 18:17 ..
lrwx------ 1 purewell daldev 64 2008-03-24 18:24 0 -> /dev/pts/1
lrwx------ 1 purewell daldev 64 2008-03-24 18:24 1 -> /dev/pts/1
lrwx------ 1 purewell daldev 64 2008-03-24 18:20 2 -> /dev/pts/1
lr-x------ 1 purewell daldev 64 2008-03-24 18:24 3 -> pipe:[839126]
l-wx------ 1 purewell daldev 64 2008-03-24 18:24 4 -> pipe:[839126]


헐헐 맞구먼 맞아. 3, 4번은 그렇다 치고, 5번은 뭐데... 이건 나중에 알아보기로 하자.

그나저나 이건 GDB라는 특수한 녀석이고, 단순히 BASH에서 지원하는 파이프기호('|')를 쓴 것도 동일하게 작동할까?

$ ./fdcnt | more
3

$ cat fdcnt.cpp|./fdcnt
3

$ ./fdcnt > /dev/stdout
3


역시... GDB가 특수한 경우였다. 일반적으로 BASH를 통한 redirection은 프로세스 간 stdin/stdout fd를 pipe로 교체하는 것이다.

댓글

이 블로그의 인기 게시물

탐색기에서 OneDrive 이 2개로 보이는 문제

왜 2개가 보이는지 모르겠지만, Registry 삭제하면 됨 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Desktop\NameSpace 하위 키에서 OneDrive 둘 중에 하나만 지워도 바로 반영됨. 참조:  https://answers.microsoft.com/en-us/msoffice/forum/all/duplicate-onedrives-in-file-explorer/49c935a6-287b-43a5-aed5-2dee2a1c1b22

절전을 깨운 녀석 알아내기

가끔씩 윈도우 절전을 깨우는 녀석이 있는데, 보통은 USB Keyboard/Mouse 이다. 요런 녀석들은 장치관리자에서 "이 장치를 사용하여 컴퓨터의 대기 모드를 종료할 수 있음"을 꺼주면 된다. 그래도 가끔씩 알 수 없는 이유로 켜졌을 경우, 관리자 권한으로 커맨드 창 열고 확인한다. C:\> powercfg -lastwake 절전 모드 해제 기록 카운트 - 1 절전 모드 해제 기록[0] 절전 모드 해제 소스 카운트 - 1 절전 모드 해제 소스[0] 종류: 장치 인스턴스 경로: XXX 이름: YYY 설명: ZZZ 제조업체: AAA