[UNIX C] socketpair(), 멀티 프로세스 자식 프로세스간 통신 문제

lmh8502의 이미지

부모 프로세스가 임의 갯수의 자식 프로세스를 생성하고

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);  
				} 
			} 
		} 

댓글 달기

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