무슨 뜻인지 설명 할 수있는 example입니다 :왜 파이프를 읽고 쓸 때 fds를 닫아야합니까? 여기
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
int fd[2], nbytes;
pid_t childpid;
char string[] = "Hello, world!\n";
char readbuffer[80];
pipe(fd);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
/* Child process closes up input side of pipe */
close(fd[0]);
/* Send "string" through the output side of pipe */
write(fd[1], string, (strlen(string)+1));
exit(0);
}
else
{
/* Parent process closes up output side of pipe */
close(fd[1]);
/* Read in a string from the pipe */
nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
printf("Received string: %s", readbuffer);
}
return(0);
} 그러나
, 내 프로세스 중 하나가 다른 파이프를 읽을 필요가있는 동안 지속적으로 파이프에 기록 할 필요가있는 경우 무엇?
위 예제는 하나의 쓰기 및 하나의 읽기에 대해서만 작동하는 것처럼 보입니다.
작업의 원 자성을 보장하기 위해 하나의 프로세스가 파이프를 사용하면 "파이프를 잠글"수 있습니다. 모든 프로세스가 파이프로 끝나면 그 파이프 중 하나가 닫힙니다. – Skurmedel
그들은 프로그램 기간 동안 파이프로 끝나지 않을 것입니다. –
확인. 그런 다음 마지막 마무리 프로세스가 종료됩니다. 하나의 파이프에 여러 개의 파이프를 열거 나 하나의 파이프에 대한 액세스를 동기화하십시오. – Skurmedel