#include <sys/types.h>
#include <sys/stat.h>
int mkfifo ( const char *pathname, mode_t mode );

mkfifo 는 특수 파일인 FIFO 파일을 만들기 위해서 사용되며, pathname 이름을 가지며 mode 의 권한을 가지는 FIFO 파일을 만들어낸다. 주로 IPC 용도로 사용된다.

FIFO 파일은 pipe 와 매우 비슷하지만, pipe 와 달리 이름있는 파일을 통해서 통신을 할수 있도록 해준다는 점이 다르다. 그러므로 관계없는 프로세스들이라고 할지라도 FIFO 파일이름만 알면 통신이 가능하도록 만들수 있다.

일단 FIFO 파일이 만들어지면 open, write, read 등의 표준 함수를 이용해서 보통의 파일처럼 접근이 가능하다.

FIFO 는 First In First Out 의 뜻을가진다. 먼저들어온 데이타가 먼저 나가는 queue 방식의 입/출력을 지원한다.

 

ls -al 했을때 맨앞에 p로 시작하는 파일 -> PIPE파일

 

수신

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

#define  FIFO_FILE   "/tmp/fifo"
#define  BUFF_SIZE   1024

int main( void)
{
    int   counter = 0;
    int   fd;
    char  buff[BUFF_SIZE];

    if(mkfifo(FIFO_FILE, 0666) == -1)
    {
        perror( "mkfifo() failed\n");
        return -1;
    }

    if (( fd = open( FIFO_FILE, O_RDWR)) == -1)
    {
        perror( "open() failed\n");
        return -2;
    }
    printf("FD=%d\n", fd);

    while( 1 )
    {
        memset( buff, 0x00, BUFF_SIZE);
        read( fd, buff, BUFF_SIZE);
        printf( "%d: %s\n", counter++, buff);
    }
    close(fd);
    return 0;
}

 

송신

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

int main(){
        int fd = open("/tmp/fifo",O_WRONLY);
        char *data = "AAAAAAiqtgjvamgoxp";
        printf("fd = %d\n",fd);
        write(fd,data,strlen(data));
        close(fd);
        return 0;
}

'나 보려고 만든거' 카테고리의 다른 글

방학동안 해야할거  (4) 2021.06.20
녹스, VMware, WSL2 hyper-v 끄고 키기  (0) 2021.05.20
c++ Explicit & Mutable  (0) 2021.01.06
c++ constructor member initializer list  (0) 2021.01.05
iOS WebView Hook  (0) 2020.10.28

+ Recent posts