linux 프로세서에 관한 질문입니다

doremii의 이미지

부모가 자식둘을 생성하고 각 자식들은 자손을 3개씩 생성하여 총 부모1,자식2,자손6 9개의 결과를 출력하고자 합니다.
코드를 만들긴 만들었는데 결과가 부모1,자식3,자손3 이렇게 나오네요,,
변수를 두개 주어 각각 fork로 자식을 생성하고 그다음 for문안에서 fork를 사용하여 자손을 만드는 코드 입니다.
어딜 어떻게 고쳐야 할지 감이 오지 않아 질문드려요 도움 부탁드립니다
아래는 메인함수의 내용입니다

int main()
{
        int ch1,ch2;
        int i;
 
        ch1=fork();
        if((ch1 < 0)) { printf("not created"); return -1;}
 
        ch2=fork();
        if((ch2 < 0)) { printf("not created"); return -1;}
 
        else if(ch1 == 0){
                printf("child1 (%d,%d).\n",getpid(),getppid());
                        for(i=0;i<3;i++){
                                if(fork())
                                        printf("grandchild(%d)\n",getpid());
                                        exit(0);}
                }
        else if(ch2 == 0){
                printf("child2(%d,%d).\n",getpid(),getppid());
                        for(i=0;i<3;i++){
                                if(fork())
                                        printf("grandchild(%d).\n",getpid());
                                        exit(0); }
                }
        else
                printf("parents(%d,%d).\n",getpid(),getppid());
 
}
 의 이미지

직접 다 풀어드릴 수도 있지만 그래서야 전혀 도움이 되지 않겠죠.
몇 군데 짚어드리고 문제를 쪼개 드릴테니 직접 풀어보세요.

1.

int main()
{
        int ch1,ch2;
        int i;
 
        ch1=fork();
        if((ch1 < 0)) { printf("not created"); return -1;}
 
        ch2=fork();
        if((ch2 < 0)) { printf("not created"); return -1;}

위 부분까지 실행되었을 때, 총 몇 개의 프로세스가 실행되고 있을까요? 각각은 어떤 ch1, ch2 값을 가지고 있을까요?
익숙하지 않으면 종이에 실행 흐름을 그려 가면서 생각해 보시기 바랍니다.
(fork는 실패하지 않는다고 가정합니다.)

2.

                        for(i=0;i<3;i++){
                                if(fork())
                                        printf("grandchild(%d)\n",getpid());
                                        exit(0);}

위 코드에서, fork 이후 printf를 실행하는 쪽은 누구일까요?
그건 그렇고, 어느 쪽이든 과연 첫 루프를 살아서 나갈 수 있을까요?
(힌트: 들여쓰기를 믿지 마세요.)

번외.

프로세스(process)와 프로세서(processor)가 이쪽 분야에서 각각 어떤 뜻을 가지는 단어인지 조사해 보세요.
단순한 오타로 전혀 다른 단어를 쓰게 될 가능성이 있다면 좀 더 주의하는 편이 좋겠습니다.

댓글 달기

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