fcntl 관련 질문입니다.

하하의 이미지

fcntl 함수 옵션 중

file descripter flag로 F_SETFD, F_GETFD 가 있습니다.

man page를 보면

Quote:

F_GETFD close-on-exec를 읽는다. 만일 FD_CLOEXEC 비트가 0이면, 파일은 exec 를 통해 열린 상태로 남겨지고, 그렇지
않으면, 닫힌다.

F_SETFD close-on-exec 플래그를 .IR arg의 FD_CLOEXEC 비트에 의해 지정된 값으로 설정한다.

이렇게 써있습니다.

제가 이해 하길.

fcntl(fd, F_SETFD, 0);

이렇게 주면 위 fd를 부모 프로세스가 execl 했을 때의 자식프로세스에 오픈 된 상태로

넘겨 준다 이해 했습니다.

예를 들어...



#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>


int main()
{

        int fd, res;

        fd = open("41.c", O_RDONLY);


        printf("파일 기술자:%d\n", fd);



        fcntl(fd, F_SETFD, 0); 



        if ( execl("./b", "b", &fd,(char *)0) < 0 ) {

                printf(" error ? %s \n", strerror(errno));

        }

}

------------------------------------------------------------------------
실행파일명이 b인 소스..
위 파일에서 오픈한 fd를 아래 소스에서 인자로 받는다.

main (int argc, char *argv[])
{
                char buf[100];


                printf("num of arg %d \n", argc);
                printf("argument %s \n", argv[0]);
                printf("argument %d \n", atoi(argv[1]));


}


이러면 첨 소스의 실행 파일이 두번째 소스의 실행파일로

fd를 넘겨 주게 되는데...

보면 execl 은 (const char *)타입의 아규를 넘겨 주는데 (int *)를 넘겨

주니 이것부터가 말이 안된다고 생각합니다.

해서 질문은 F_SETFD를 이렇게는 사용안할거란 생각이

듭니다. 그럼 어떨때 이 플래그 값을 쓰는 지 알고 싶습니다.

그럼 즐거운 하루 되세요. ^_____^;;;;;;

하하의 이미지

음.. 질문이 애매했나 생각되어 집니다.

답변이 없는걸 보니.. - - ;;;;

“바람에게도 길은 있다. 나는 비로소 나의 길을 가느니. 길은 언제나 어디에나 있다.”

pynoos의 이미지

Quote:
음.. 질문이 애매했나 생각되어 집니다.

답변이 없는걸 보니.. - - ;;;;


다들 귀찮았나보죠... :)

int 형을 execl에 바로 넘기면안되고, sprintf 등으로 문자열로 치환후에 넘겨야됩니다.

그리고 기본적으로 descriptor는 F_SETFD 를 0 으로 setting하여 만들어지기 때문에, exec에서 넘어가는 것이 일반적입니다.
오히려, 0 을 setting하는 일보다 FD_CLOEXEC 를 setting하여 exec를 통해 넘어가지 않게 할때 사용합니다.

모두 CLOSE하고 특정한 FD값만 넘기기 위해 주로 위와 같이 argument로 넘기거나 환경변수로 넘기면 됩니다. 아니면, 프로그램내부의 일로 생각하면, 잘 안열릴만한 60 번대 descriptor로 dup2를 하신뒤 코드내에 hard coding하여 사용해도 됩니다.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.