fork()하고 부모가 먼저 죽은 후 자식의 표준입출력 동작에 대해

lnsium의 이미지

fork하고 부모가 먼저 죽으면 자식이 표준입출력에 대해 어떤 동작을 하더라도 무시되는 현상을 보입니다.

fork시 자식에게도 파일기술자가 복제되어 유효한 걸로 알고 있는데 부모가 죽으면서 파일기술자를 닫는다고 자식에게까지 영향을 주는 것인지 이것이 의문입니다.

#include <unistd.h>
#include <stdio.h>

int main()
{
    char buf[BUFSIZ];

    switch(fork())
    {
        case -1:
            exit(1);
        case 0:
            sleep(1);
            fgets(buf, BUFSIZ-1, stdin);
            printf("I am child\n");
            while(1)
                sleep(1);
            exit(0);
    }

    fgets(buf, BUFSIZ-1, stdin);
    printf("I am parent\n");
    exit(0);
}

fork후 자식이 먼저 동작해서 sleep으로 부모가 먼저 동작하고 죽게 만들었습니다. 이후 자식의 표준입력과 표준출력 모두 동작하지 않습니다.
더욱 의심스러운것은 while루프가 있는데도 자식프로세스가 죽어버린다는 겁니다. 이거참 어떻게 해석해야할지 난감합니다.

익명 사용자의 이미지

setsid()

lnsium의 이미지

setsid로 자식이 죽지않게 하는 방법은 조금 알고 있습니다. 데몬만들 때 쓰더군요. 세션그룹과 관계가 있는 걸루 아는데 자세히 설명해 주시면 좋을텐데...

저가 궁금한 것은 파일입출력에 대한 이상동작입니다. 정말 부모가 죽으면 부모가 열었던 파일기술자들이 자식에게도 무효하게 되는지...

익명 사용자의 이미지

자식프로세스는 죽지 않습니다.
코드를 다음과 같이,

            printf("I am child=%d\n", getpid()); 
            while(1) {
                sleep(1);
                printf("나 아직 안죽었어...\n");
                fflush(stdout);
             }
 

하여 돌려보시고, 자식의 pid를 ps로 검사해보세요.

참고로 다음을 보시기 바랍니다.

Signal: Terminal Input on Background Process 
The process is trying to read from a terminal that it no longer is the controlling terminal for. This process will be stopped until a SIGCONT is sent. This signal can be caught and ignored, see SIGTSTP, SIGSTOP and SIGCONT 

An example is: 

  bash$ cat > /dev/null &
  [1] 31731
  [1]  + suspended (tty input)  cat

By default, cat will read from standard input, but the & character means that this command is run in the background, and the terminal's input goes back to the shell. When cat tries to read from the terminal's stdin, a SIGTTIN signal is raised.

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.