42Seoul/pipex

pipe(), unlink()

재윤 2023. 9. 13. 01:34
반응형

프로레스끼리 통신을 하기 위해 파이프라는 함수를 제공해줌.

원형

#include <unistd.h>

int pipe(int fd[2]);

fd[0] : 함수 호출 후 fd[0]에 데이터를 입력 받을 수 있는 파일 디스크립터가 담김 → buffer에서 read

fd[1] : 함수 호출 후 데이터를 출력할 수 있는 파일 디스크립터가 담김 → buffer에 write를 할 수가 있음.

# include <fcntl.h>
# include <unistd.h>
# include <stdio.h>
# include <stdlib.h>

# define BUFSIZE 100

int main()
{
	int fd[2];
	char buffer[BUFSIZE];
	pid_t pid;
	int state;

	state = pipe(fd);
	if (state == -1)
	{
		puts("error");
		exit(1);
	}
	pid = fork();

	if (pid == -1)
	{
		puts("err");
		exit( 1);
	}
	else if(pid == 0)
	{
		write(fd[1], "연결성공!!", 25);
		sleep(2);
		read(fd[0], buffer, BUFSIZE);
		printf("Output of child process : %s \\n", buffer);

	}
	else
	{
		read(fd[0], buffer, BUFSIZE);
		printf("Output of parent process : %s \\n", buffer);
		write(fd[1], "정말 좋아!", 25);
		sleep(2);
		
	}
}

출력

./a.out 
Output of parent process : 연결성공!! 
Output of child process : 정말 좋아!

05. pipe() 파이프

 

05. pipe() 파이프

멀티 프로세스 이용시 문제점 : 프로세스는 메모리가 독립적으로 존재하기 때문에 메모리를 프로세스간 프로세스간 데이터를 주고 받는 것은 불가능합니다. 쉽게 설명하자면 fork()이용 하여 serv

smeffect.tistory.com

 

unlink

#include <unistd.h>

int unlink(const char *pathname);
  • 파일을 삭제하는 system call 함수.
  • 반환: int 0 == 성공, -1 == 실패
반응형

'42Seoul > pipex' 카테고리의 다른 글

waitpid()  (0) 2023.09.13
fork()  (0) 2023.09.13
access()  (0) 2023.09.13
dup, dup2  (0) 2023.09.13
execve()  (0) 2023.09.13