prefork후 socket descripitor를 전송하는 구조에서..

MackTheKnife의 이미지

멀티프로세스로 서버를 구현하고있읍니다.
서버모델은 차일드를 일정개수동안 prefork하고나서
parent에서 accept한 fd를 child로 전송합니다
child에서는 처리가 완료되고나서 parent에게 완료통지를 합니다.

클라이언트에서 최초 한번의 전송은 제대로 되지만 두번째 부터는 write는 되지만
read는 안되는군여

이때 netstat로 보면
parent의 pid가 100,child의 Pid가 101일때
recvQ pid
tcp 0 ... LISTEN 100/connector <-서버의 Parent Process
tcp 0 ... ESTABLISHED 300/test <-client process
tcp 255 ... ESTABLISHED 100/connector <서버의 child Process

1.child로 descriptor를 전송하고난후는 클라이언트와 child가 연결이 되어야되는데 pid를 보면 parent와 child가 연결이 되는데 이게 맞나여?

2.recvQ에는 client가 2번째로 전송을 하였을경우 데이터사이즈입니다.
연결이 제대로 되었는데 child에서 읽지 못하는 이유는 뭐져?

3.parent에서 listen하고 있는 fd의 넘버가 5입니다.
parent에서 accept한 fd의 넘버가 10이라고할때
accept한 fd를 child로 전송하고 child에서 받으면 fd가 10이 아닌 5가 되는데
이렇게 되는게 맞는거지?..아님 parent에서 accept한 fd..10이 넘어와야되는게 아닌지?..

===================서버 소스=====================

main()
{
// create Child Array
childInfo=(Child*)calloc(nchildren,sizeof(Child));

//prefork child process
for(i=0;i<nchildren;i++)
{
MakeChild(i,listenfd); // if parent return else child never return
FD_SET(childInfo[i].child_pipefd,&masterset);
maxfd=max(maxfd,childInfo[i].child_pipefd);
}

// set Signal Handler
signal(SIGINT,SigIntHandler);

for(;;)
{
rset=masterset;
if(navail<=0)
{
FD_CLR(listenfd,&rset);
}

nsel=select(maxfd,&rset,NULL,NULL,NULL);

if(FD_ISSET(listenfd,&rset))
{
connfd=accept(listenfd,&clientaddr,&clientLen);

for(i=0;i<nchildren;i++)
{
if (childInfo[i].child_status==0)
break;
}

if(i==nchildren)
{
printf("too man children\n");
exit(0);
}

// set childInfo busy
childInfo[i].child_status=1;
childInfo[i].child_count++;
navail--;
n=Write_FD(childInfo[i].child_pipefd,"",1,connfd);
// close(connfd);// close appceted fd

FD_SET(connfd,&masterset);
//test
printChildInfo();

if(--nsel==0) // select event count=1
continue;
}

for(i=0;i<nchildren;i++)
{
if(FD_ISSET(childInfo[i].child_pipefd,&rset))
{
if((n=read(childInfo[i].child_pipefd,&rc,1))==0)
{
printf("child %d terminated unexpectedly\n");
exit(0);
}
// set chlidInfo idle
childInfo[i].child_status=0;
navail++;

if(--nsel==0)
break;
}
}
}


}

pid_t
MakeChild(int i, int listenfd)
{
int sockfd[2];
pid_t pid;

socketpair(AF_LOCAL, SOCK_STREAM, 0, sockfd);

if ( (pid = fork()) > 0) {
close(sockfd[1]);
childInfo[i].child_pid = pid;
childInfo[i].child_pipefd = sockfd[0];
childInfo[i].child_status = 0;
return(pid); /* parent */
}

dup2(sockfd[1], STDERR_FILENO); /* child's stream pipe to parent */
close(sockfd[0]);
close(sockfd[1]);
close(listenfd); /* child does not need this open */
ChildMain(i, listenfd); /* never returns */
}
/* end MakeChild */

/* include ChildMain */
void
ChildMain(int i, int listenfd)
{
char c;
int connfd;
ssize_t n;
int ret;

printf("child %d starting\n", getpid());
for ( ; ; ) {
if ( (n = Read_FD(STDERR_FILENO, &c, 1, &connfd)) == 0)
Error_Quit("Read_FD returned 0");
if (connfd < 0)
Error_Quit("no descriptor from Read_FD");

DoChild(i,connfd); /* process the request */

//close(connfd);

ret=write(STDERR_FILENO, "", 1); /* tell parent we're ready again */
}
}
/* end ChildMain */

void
DoChild(int k,int sockfd)
{
int ntowrite;
ssize_t nread;
char line[BUFFSIZE], result[BUFFSIZE];
char* temp;

memset(line,0x00,sizeof(line));
memset(result,0x00,sizeof(result));

nread=read(sockfd,line,sizeof(line));
printf(" child..nread=%d readstring=%s\n",nread,line);

sprintf(result,"server response=>");
strcat(result,line);

write(sockfd, result, strlen(result));
}

stoneshim의 이미지

여기 또 올리셨군요.

저번에 올리신 것과 질문하시는 내용이 조금 다르긴 하지만 동일한 문제가 원인입니다.
가능하시면 동일 스레드에서 연결해서 질문하시는게 좋습니다.

Quote:
1.child로 descriptor를 전송하고난후는 클라이언트와 child가 연결이 되어야되는데 pid를 보면 parent와 child가 연결이 되는데 이게 맞나여?

2.recvQ에는 client가 2번째로 전송을 하였을경우 데이터사이즈입니다.
연결이 제대로 되었는데 child에서 읽지 못하는 이유는 뭐져?


저번 글에 답글을 올렸습니다. 확인하세요.

Quote:
3.parent에서 listen하고 있는 fd의 넘버가 5입니다.
parent에서 accept한 fd의 넘버가 10이라고할때
accept한 fd를 child로 전송하고 child에서 받으면 fd가 10이 아닌 5가 되는데
이렇게 되는게 맞는거지?..아님 parent에서 accept한 fd..10이 넘어와야되는게 아닌지?..

unix socket으로 fd를 전송하는 경우 fd 번호가 전송되는것이 아니라, fd를 받는 프로세스에서 해당 파일을 오픈하게 되어있습니다.
fd번호는 다른게 정상입니다. 물론 운좋게 같을수도 있겠지요.

우리 모두 리얼리스트가 되자. 그러나 가슴에 이룰 수 없는 꿈을 가지자

댓글 달기

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