[UNIX C] socketpair(), 멀티 프로세스 자식 프로세스간 통신 문제
글쓴이: lmh8502 / 작성시간: 목, 2012/03/15 - 12:54오후
부모 프로세스가 임의 갯수의 자식 프로세스를 생성하고
socketpair 함수를 통해 stdin 을 놔눠가지고 소켓 풀 구조체에 담고 처리하는 프로그램입니다.
최초 실행시 부모는 자식 프로세스 갯수만큼 생성하면서 socketpair 를 놔눠가지는데요.
그리고 부모는 socket listen aceept 에서 대기 하게 됩니다.
그런데 첫번째 요청시 텔넷으로 붙어보면 실제 tcp 소켓이랑 붙어있지 않고 부모 프로세스의 stdin 에
메세지를 기다리고 있네요.. 그 다음부터는 또 정상 작동합니다.. 뭐가 잘못된걸까요?
for (i = 0; i < preforknum; i++) { if( socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0 ) { perror("socketpair error"); } pid = fork(); if (pid < 0) { perror("fork error"); exit(0); } if (pid == 0) { dup2(sv[0], fileno(stdin)); close(sv[1]); close(sv[0]); close(server); prefork_client(0); } else { close(sv[0]); SocketArray[i].pid = pid; SocketArray[i].fd = sv[1]; SocketArray[i].status = 1; } }
-------------------------
while(1) { readfds = allfds; n = select(maxfd + 1, &readfds, (fd_set *)0, (fd_set *)0, NULL); if (n < 0) continue; if (FD_ISSET(server_sockfd, &readfds)) { if ((client_sockfd = accept(server_sockfd, 0, 0) ==-1)) { perror("accept error:"); close(client_sockfd); } else { for (i = 1; i < preforknum; i++) { if(SocketArray[i].status) { printf("\n[STEP-01] ar[%d], client_sockfd = %d", i, client_sockfd); fflush(stdout); fflush(stdin); if( write_fd(SocketArray[i].fd, &ptr, 1, client_sockfd) == -1 ) { perror("write_fd"); continue; } printf("\n[STEP-02] ar[%d], client_sockfd = %d, ptr char = %c", i, client_sockfd, ptr); fflush(stdout); fflush(stdin); SocketArray[i].status = 0; close(client_sockfd); break; } } if (i == preforknum) { fprintf(stderr, "max Client error\n"); STD_PRNT("max Client error\n"); close(client_sockfd); } } }
Forums:
댓글 달기