기본 콘텐츠로 건너뛰기

ProxySQL 에서 Unknown system variable 'query_cache_size' 오류

기본으로 열리는 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로 교체하는 것이다.

댓글

이 블로그의 인기 게시물

Bash Array, Map 정리

Bash에서 Array, Map에 대한 정리. (매번 찾기 귀찮) 찾아보진 않았지만, Bash에서 Array든 Map이든 동일하게 Map(C++에서 Unordered Map)으로 동작하는 것 같다. 왜냐하면, Array의 Index가 연속하지 않아도 동작한다. 그저 Key가 0 이상의 정수인 Map이랑 비슷하게 동작한다. 예) 1, 2, 3, 9, 10 Array # 생성 declare -a empty_array declare -a ar=(haha hoho baba "long string haha hoho") # 접근 echo "ar[0]=${ar[0]}" echo "all as array=${ar[@]}" # 큰따옴표 안에서 각 원소를 따로따로 전달한다. echo "all as one=${ar[*]}" # 큰따옴표 안에서 각 원소를 문자열 하나로 합쳐 전달한다. echo "indexes=${!ar[@]}" echo "indexes=${!ar[*]}" echo "length=${#ar[@]}" echo "length=${#ar[*]}" echo "last=${ar[-1]}" echo "last=${ar[@]: -1}" # 콜론 뒤에 빈 칸이 꼭 필요하다. 옛 방식 # 현재 상황 declare -p ar #(출력) declare -a ar=([0]="haha" [1]="hoho" [2]="baba" [3]="long string haha hoho") ar[100]=hello # 인덱스를 건너 뛰어도 동작한다. declare -p ar #(출력) declare -a ar=([0]="haha" [1]="hoho" [2]="baba" [3]=&

설치한 패키지에서 RPM 추출하기

오래된 패키지를 관리할 저장소가 없어졌고, 기존 패키지로 다른 서버를 세팅해야할 일이 생겼다면 RPM의 리패키지 기능을 이용해보자. $ rpm -e --repackage [PACKAGE_NAME] 위와 같이 리패키지하면, /var/spool/repackage/ 에 생성한 RPM파일이 있다. :-)