2016-10-26 5 views
0

다음 c 프로그램은 부모 프로세스에서 파이프를 통해 fork()를 사용하여 생성 된 자식 프로세스로 메시지를 보내고 Linux 터미널에서 실행되는 데 사용됩니다!메시지 끝에서 원하지 않는 문자 가져 오기

#include <stdio.h> 
#include <unistd.h> 
#include <sys/types.h> 

int main(int argc,char *arg[]){ 
    pid_t child; 
    int pipefd[2]; 
    int ret; 
    char message[20]; 
    ret =pipe(pipefd); 


      if((child=fork())==0){ 
           printf("The child process \n"); 
           close(pipefd[0]); 
            write(pipefd[1],"Hello from parent",17); 

           } 

           else{ 
            close(pipefd[1]); 
           read(pipefd[0],message,17); 
           printf("Message from parent %s\n",message); 
            } 
            return 0; 
            } 

위의 코드는 "Hello from parent"라는 메시지를 인쇄하지만 부모 부분의 끝 부분에 @ 기호가 인쇄됩니다! 이유는 무엇이며 어떻게 수정할 수 있습니까?

답변

1

문자열 끝에있는 null 문자도 전송합니다. 독서와 동일합니다.

write(pipefd[1],"Hello from parent",18);