콘텐츠로 바로가기

now0930 일지

이런저런 생각

  • 홈
  • 비공개
  • 강좌
  • 잔여 작업 조회
  • 위치

fifo 예제. unix network programming 55p

pipe는 이름을 없어 parent, child process만 통신이 가능하다. 이를 보안한게 named pipe인데, fifo라고도 한다. 책은 다른 유닉스 버전으로 작성하여, linux에 맞게 수정했다. 결론적으로 pipe와 비슷하고 더 쉽다.

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>


#define	FIFO1	"/tmp/fifo.1"
#define	FIFO2	"/tmp/fifo.2"
#define MAXLINE 100

void	client(int, int), server(int, int);

void server(int readfd, int writefd){
	/*
	char	buff[MAXLINE];
	int n;
	n = read(readfd, buff, MAXLINE);
	printf("서버가 읽은 data는 %s\n",buff);
	// 샘플 테스트용..

	*/
	int fd;
	ssize_t n;
	char	buff[MAXLINE+1];

	/* 4read pathname from IPC channel */
	if ( (n = read(readfd, buff, MAXLINE)) == 0)
		perror("end-of-file while reading pathname");
	buff[n] = '\0';		/* null terminate pathname */

	if ( (fd = open(buff, O_RDONLY)) < 0) {
			/* 4error: must tell client */
		snprintf(buff + n, sizeof(buff) - n, ": can't open, %s\n",
				 strerror(errno));
		n = strlen(buff);
		write(writefd, buff, n);

	} else {
			/* 4open succeeded: copy file to IPC channel */
		while ( (n = read(fd, buff, MAXLINE)) > 0)
			write(writefd, buff, n);
		close(fd);
	}

}

void client(int readfd, int writefd)
{
	size_t	len;
	ssize_t	n;
	char	buff[MAXLINE];

	/* 4read pathname */
	fgets(buff, MAXLINE, stdin);
	len = strlen(buff);		/* fgets() guarantees null byte at end */
	if (buff[len-1] == '\n')
		len--;				/* delete newline from fgets() */

	/* 4write pathname to IPC channel */
	write(writefd, buff, len);

	/* 4read from IPC, write to standard output */
	while ( (n = read(readfd, buff, MAXLINE)) > 0)
		write(STDOUT_FILENO, buff, n);
}


int
main(int argc, char **argv)
{
	int		readfd, writefd;
	pid_t	childpid;

		/* 4create two FIFOs; OK if they already exist */
	if ((mkfifo(FIFO1, 0666) < 0) && (errno != EEXIST))
		printf("can't create %s", FIFO1);
	if ((mkfifo(FIFO2, 0666) < 0) && (errno != EEXIST)) {
		unlink(FIFO1);
		printf("can't create %s", FIFO2);
	}

	if ( (childpid = fork()) == 0) {		/* child */
		readfd = open(FIFO1, O_RDONLY, 0);
		writefd = open(FIFO2, O_WRONLY, 0);

		server(readfd, writefd);
		exit(0);
	}
		/* 4parent */
	writefd = open(FIFO1, O_WRONLY, 0);
	readfd = open(FIFO2, O_RDONLY, 0);

	client(readfd, writefd);

	waitpid(childpid, NULL, 0);		/* wait for child to terminate */

	close(readfd);
	close(writefd);

	unlink(FIFO1);
	unlink(FIFO2);
	exit(0);
}
pi@raspberrypi:~/Project/cCode/IPC $ ls
a.out      fifo.c  simplepipe.c  unpv22e
fduplex.c  pipe.c  test.txt      unpv22e.tar
pi@raspberrypi:~/Project/cCode/IPC $ cat test.txt 
안녕하세요.
hello.
pi@raspberrypi:~/Project/cCode/IPC $ ./a.out 
./test.txt
안녕하세요.
hello.
pi@raspberrypi:~/Project/cCode/IPC $ 

이 장에 여러 내용도 많은데 일단 다른 유닉스 버전도 보이고 시간도 없어 다음 장으로 넘어간다.

https://www.geeksforgeeks.org/named-pipe-fifo-example-c-program/

이 글 공유하기:

  • Tweet
발행일 2020-07-14글쓴이 이대원
카테고리 생활코딩 태그 c, fifo, ipc, linux, network program

댓글 남기기응답 취소

이 사이트는 Akismet을 사용하여 스팸을 줄입니다. 댓글 데이터가 어떻게 처리되는지 알아보세요.

글 내비게이션

이전 글

pipe 예제. unix network programming 47p

다음 글

message queue, unix network programming 75p ~ 90p

2025 5월
일 월 화 수 목 금 토
 123
45678910
11121314151617
18192021222324
25262728293031
4월    

최신 글

  • common mode, differential mode 2025-05-11
  • signal conditioner, 신호 처리기 2025-05-10
  • strain gage 2025-05-09
  • 칼만 필터 2025-05-01
  • positioner(I/P) 2025-04-26

카테고리

  • 산업계측제어기술사
  • 삶 자국
    • 책과 영화
    • 투자
  • 생활코딩
    • LEGO
    • ROS
    • tensorflow
  • 전기기사
  • 피아노 악보

메타

  • 로그인
  • 엔트리 피드
  • 댓글 피드
  • WordPress.org

페이지

  • 소개
  • 잔여 작업 조회
    • 작업 추가
    • 작업의 사진 조회
    • 작업 수정 페이지
  • 사진
    • GPS 입력된 사진
    • 사진 조회
  • 위치
    • 하기 휴가 방문지
    • 해외 출장

태그

android bash c docker driver FSM gps java kernel LEGO linux mysql network program opcua open62541 plc programmers python raspberry reinforcementLearning ros state space system program tensorflow transfer function 경제 미국 민수 삼국지 세계사 실기 에너지 역사 유전자 일본 임베디드 리눅스 전기기사 조선 중국 채윤 코딩 테스트 통계 한국사 한국어

팔로우하세요

  • Facebook
now0930 일지
WordPress로 제작.