프로세스에 대한 질문입니다.

neon20의 이미지

아래 부분이 현재 문제가 되는 코드 부분입니다.

        while(1){
                if( (pid = fork()) < 0){
                        perror("fork error!");
                        exit(1);
                }
                if(pid != 0){
                        gettimeofday(&time1,NULL);
                        while(1){
                          gettimeofday(&time2,NULL);
                          if( (myusec = time2.tv_usec - time1.tv_usec) > TIME_OVER){
                            printf("kill child - %d, id = %d\n",i,pid);
                            kill(pid,SIGTERM);
                            wait(NULL);
                            break;
                          }
                        //  printf("time : %ld",(long)TIME_OVER);
                        }
                }else{
                        len = recvfrom(sock,recvbuf,1024,0,(struct sockaddr*)&clnt_addr,&recv_len);
                        sendto(sock,recvbuf,len,0,(struct sockaddr*)&clnt_addr,sizeof(clnt_addr));
                        printf("데이터를 보냅니다 : %s\n",recvbuf);
                        close(sock);
                        exit(1);
                }
//              if(i == 100) break;
                i++;
        }

간단히 설명 드리면 부모 프로세스는 일정시간동안 대기를 합니다(TIME_OVER 동안) 그리고 그 시간 이후 자식프로세스를 죽이게(?) 되구요. 자식 프로세스는 열심히 자신에게 오는 데이터를 받아서 보내주는 일을 합니다. 그런데 문제는 이런식으로 계속 부모 프로세스가 자식을 죽이다나 어느 순간부터는 더이상 진행이 안됩니다.

$ ps u
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
874      17779  0.0  0.0  5452 1452 pts/5    S    01:42   0:00 -bash
874      21495 99.1  0.0  1388  312 pts/5    R    02:07   5:35 ./server_v5
874      21794  0.0  0.0     0    0 pts/5    Z    02:07   0:00 [server_v5 <defunct>]
874      21800  0.0  0.0  2856  800 pts/5    R    02:13   0:00 ps u
$

위에 보시는 것과 같이 어느 순간부터는 더이상 자식을 죽이지 못하고 정지해 있는것 같습니다. 더이상 자식이 생성되지 않는거라면 이해가 되는데 자식을 죽이지 못하는 상황이라...도무지 이해가..ㅜㅜ

neon20의 이미지

테스트 해보니 특정 개수 이상의 자식프로세스를 죽임으로 인해서 발생하는 현상은 아닌거 같은데요... 100번정도 죽이고 정지할 때도 있고, 400번 정도 죽이고 정지할 때도 있고... 이유가 뭐지..ㅜㅜ

neon20의 이미지

다음과 같은 형태로 돌리니 잘 되는군요... 음 gettimeofday와 관련해서 문제가 있는건가.. :(

        while(1){
                if( (pid = fork()) < 0){
                        perror("fork error!");
                        exit(1);
                }

                if(pid != 0){
                            printf("kill child - %d, id = %d\n",i,pid);
                            kill(pid,SIGTERM);
                            waitpid(pid,(int*)0,WNOHANG);

                }else{
                        close(sock);
                        exit(1);
                }
                i++;
        }
neon20의 이미지

gettimeofday 함수대신 setitimer 함수를 이용해서 alarm 을 수행 했더니 잘 되는군요.. :D

singlet의 이미지

if( (myusec = time2.tv_usec - time1.tv_usec) > TIME_OVER){해당 상황에서는 myusec < 0 이 될 겝니다.

익명 사용자의 이미지

Quote:
double t1,t2,elapsed;
struct timeval tp;
int rtn;

....
....
rtn=gettimeofday(&tp, NULL);
t1=(double)tp.tv_sec+(1.e-6)*tp.tv_usec;
....
/* do some work */
....
rtn=gettimeofday(&tp, NULL);
t2=(double)tp.tv_sec+(1.e-6)*tp.tv_usec;
elapsed=t2-t1;

tv_usec만을 비교하면 아니되옵니다. 다음과 같은 경우에 문제가 되겠지요.

12시1분2초 + 0.999999초
12시1분3초 + 0.000001초

이때 tv_sec부분을 배제하고 코딩하면(질문자님의 경우에 해당) 음수값이 나오게 될 수 있기 때문입니다.
0.000001 - 0.999999 초
실제 연산에서는 정수형태이므로
1 - 999999 가 되겠군요.

따라서 위에 인용한 사례를 사용하시는게 좋겠습니다.

댓글 달기

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