fork()함수 부모프로세스와 자식프로세스

vdsl88의 이미지

#include
#include
#include

int main()
{
pid_t pid;
pid = fork();
if(pid<0){
fprintf(stderr, "Fork Failed");
return 1;
}
else if(pid==0){
sleep(10);
}
else{
printf("pid=%d\n", pid);
}
return 0;
}

저는 정답이 3번과 4번에서 많이 헷갈리는데

sleep인 상태에서도 프로세스는 살아있다라고 보는게 맞는 건지요??

또한 부모 프로세스가 종료하면 자식프로세스도 종료되는것도 맞는것 같은데

아래 문제의 답을 알수 있을까요?

Q.리눅스 시스템에서 다음 프로그램의 실행결과로 알맞은 것은?

1.부모 프로세스는 자신의 프로세스 pid 값을 출력하고 종료된다.
2.부모 프로세스가 먼저 종료된후 자식 프로세스의 부모 프로세스 pid는 1이다.
3.10초동안 자식 프로세스는 살아 있다.
4.부모 프로세스가 종료될때, 자식 프로세스도 종료된다

익명 사용자의 이미지


이 소스에서, 2번과 3번은 항상 맞습니다.

4번은 사실이 아닙니다.

이 예제는 적용이 안되지만,
상황에따라 부모 프로세스가 session leader(또는 controlling process)
인 경우, 그 부모가 죽으면, 자식들에게 SIGHUP이 보내지기도 합니다.

하지만, 이것도 여러 조건들이 따라 붙으며, 보내기도 쉽지 않습니다.
또한, SIGHUP이 보내진다는것 자체가 자식이 죽어야한다는것을 의미하지도 않습니다.
단지, SIGHUP핸들러의 기본 설정이 termination일뿐입니다. 이것마저 자식에서
무시할수도 있죠.

아래문서를 참조
http://www.unixguide.net/unix/programming/1.15.shtml

리눅스에서는 ptctrl()을 이용하여, 부모가 죽을 때 특정 시그널이 보내지게 할수도 있습니다..

어쨋든, 여차저차해서 SIGHUP으로 자식을 죽인다하더라도, 이것은 부모로서 죽었을때의 경우가 아닌
session leader로서 죽었기때문에 발생하는경우입니다.

댓글 달기

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