fork()/wait() 질문 ?

lapex의 이미지

void func(int n)
{
    for(int j=0; j < n; j++) {
        if ((pid[j] = fork()) == -1)
            perror("multi client forks");
        if (pid[j] == 0) { /* child */
                // do something..
        } else {
            /* 1. wait() */
        }
    }

    for(int j=0; j < n; j++)
        if (pid[i])
            /* 2. wait() */
}

int main(void)
{
    pid_t pid[NUM_OF_CLIENT];

    for (int i=0; i < NUM_OF_CLIENT; i++)
        func(i);

    return 0;
}

대충 슈도 코드식으로 짜보면 위와 같은 로직을 갖고 있습니다.

제가 원하는 것은 main() 내에서 func() 를 호출할 때
func(0) 의 child 들이 다 마친후에
func(1) 가 수행되고, 또 그 child 들이 일을 다 마친후에
func(2) 가 수행되는 식으로 프로그램을 만들고 싶은데

func() 내에서는 fork() 로 생성된 프로세스끼리 계속
context switch 를 하면서 돌고 있어야 합니다.

질문이 조금 애매한 것 같으니 다시 정리하자면
func() 내에서 for 문을 돌면서 생성된 자식 프로세스가
일을 다 마친후에 func() 을 끝내고
main() 내의 루프로 돌아가고 싶은 루틴을 짜고 싶은데

1.wait() 위치에 쓰는 것이 맞나여 아님
2.wait() 위치에 쓰는 것이 맞는 건가여

아님 다 틀린가여

pynoos의 이미지

wait 은..

SIGCHLD handler를 등록하여 거기서 해주는 것이 좋습니다.

운형의 이미지

2번에 달경우 문제가 좀 있네요(물론 1번도 문제가 있지만.. -_-")

2번경우 자식 프로세스가 동시에 3개이상 죽는 경우 시그널이 중첩되지 않는 관계로 부모가 3번째 이후 자식의 죽음은 알지 못하게 될겁니다.
이때 waitpid에 WNOHANG을 달고 루프를 돌리면 수거는 다 되죠.

1번경우는 시체는 정말 깔끔히 수거하는 반면 생성된 자식이 전부 죽을 때까지 부모는 아무일도 못할겁니다.

시그널을 좀 이용하면 부모도 자기 할일 다하면서 자식 시체 수거 잘하는 코드가 나올껀데...

도움이 되셨을런지..

Do you think that's the air you are breathing now?

seeker의 이미지

#include <signal.h>
#include <sys/wait.h>

main부분에...
struct sigaction act ;
act.sa_handler = sighandler;
act.sa_flags = 0;
sigemptyset( &act.sa_sigset );
sigaction( SIGCHLD , &act , 0 );

시그널핸들러를 ...
void sighandler(int signo )
{
while ( waitpid( -1 , 0 , WNOHANG ) > 0 ) ;
}

댓글 달기

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