2012. 7. 13. 16:33

#include <stdio.h>
#include <unistd.h>
#define SIZE 4

main()
{
    char *arg[3] = {"abc", "def", "ghi"};
    char buffer[SIZE];
    int pipes[2], i;

    /* 파이프 생성. pipes[0]은 파이프로부터 데이터를 읽어 들일 때 사용하는
      파일 식별자가 되고 pipes[1]은 파이프에 데이터를 쓸 때 사용하는 파일 식별자가 됨 */
    if(pipe(pipes) == -1) {
       perror("pipe failed");
       exit(1);
    }

    for(i=0; i<3; i++) {
       /* 파이프에 arg[i]를 쓰기 */
       write(pipes[1], arg[i], SIZE);
    }
    for(i=0; i<3; i++) {
       /* 파이프로부터 데이터 읽기 */
       read(pipes[0], buffer, SIZE);
       printf("%s\n", buffer);
    }

    exit(0);
}

[localhost@local]#a.out
abc
def
ghi

FIFO 방식으로 동작

--------------------------------------------------------------------------------------------------------
pipe를 이용한 부모 자식 프로세스에서의 통신 예제

같은 파이프로 읽고 쓸수 있으므로 혼란스러울 수 있음

#include <stdio.h>
#include <unistd.h>
#define SIZE 4

main()
{
    char *arg[3] = {"abc", "def", "ghi"};
    char buffer[SIZE];
    int pipes[2], i;

    /* 파이프 생성 */
    if(pipe(pipes) == -1) {
       perror("pipe failed");
       exit(1);
    }

    /* 자식 프로세스를 생성하고 부모 프로세스는 */
    if(fork()) {
       for(i=0; i<3; i++) {
          /* 파이프에 arg[i]를 쓰기 */
          write(pipes[1], arg[i], SIZE);
          printf("parent process write to pipe: %s\n", arg[i]);
       }
    /* 자식 프로세스는 */
    } else {
       for(i=0; i<3; i++) {
          /* 파이프로부터 읽기 */
          read(pipes[0], buffer, SIZE);
          printf("child process read from pipe: %s\n", buffer);
       }
    }
    exit(0);
}

[localhost@local]# a.out
parent process write to pipe : abc
child process read from : abc
parent process write to pipe : def
child process read from : def
parent process write to pipe : ghi
child process read from : ghi

--------------------------------------------------------------------------------------------------------
위의 프로그램을 개선

사용하지 않는 식별자를 없앰(부모프로세스 pipes[0]없앰, 자식프로세스 pipes[1]없앰)

#include <stdio.h>
#include <unistd.h>
#define SIZE 4

main()
{
    char *arg[3] = {"abc", "def", "ghi"};
    char buffer[SIZE];
    int pipes[2], i;

    /* 파이프를 생성 */
    if(pipe(pipes) == -1) {
       perror("pipe failed");
       exit(1);
    }

    /* 부모 프로세스는 */
    if(fork()) {
       /* 파이프의 읽기 파일 식별자 닫기 */
       close(pipes[0]);
       for(i=0; i<3; i++) {
          /* 파이프에 arg[i]를 쓰기 */
          write(pipes[1], arg[i], SIZE);
          printf("parent process write to pipe: %s\n", arg[i]);
       }
    /* 자식 프로세스는 */
    } else {
       /* 파이프의 쓰기 파일 식별자 닫기 */
       close(pipes[1]);
       for(i=0; i<3; i++) {
          /* 파이프로부터 읽기 */
          read(pipes[0], buffer, SIZE);
          printf("child process read from pipe: %s\n", buffer);
       }
    }
    exit(0);
}

[localhost@local]# a.out
parent process write to pipe : abc
child process read from pipe : abc
parent process write to pipe : def
child process read from pipe : def
parent process write to pipe : ghi
child process read from pipe : ghi


--------------------------------------------------------------------------------------------------------
두 프로세스간에 데이터를 주고받는 프로그램.

파이프 두 개를 생성. 한 파이프는 부모에서 자식으로 데이타를 전달하는 역할

다른 파이프는 자식에서 부모로 데이터를 전달하는 역할을 하도록 하면 됨

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#incldue <sys/types.h>
#incldue <sys/wait.h>
#define SIZE 256

main()
{
    int pipes1[2],pipes2[2],length;
    char read_buffer[SIZE], write_buffer[SIZE];

    /*두개의 파이프 생성 */
    if(pipe(pipes1)==-1 || pipe(pipes2)==-1) {
        perror("pipe failed");
        exit(1);
    }
    
    /*부모 프로세스는*/
    if(fork()) {
    /*pipes1의 쓰기 파일 식별자 닫기 */
    close(pipes1[1]);
    /*pipes2의 읽기 파일 식별자 닫기 */
    close(pipes2[0]);

    /*pipes1 파이프로 부터 데이터 읽기*/
    if((length=read(pipes1[0],read_buffer,SIZE) == -1) {
        perror("read failed");
        exit(1);
    }
    write(STDOUT_FILENO, "receive message : " ,strlen("receive message: "));
    write(STDOUT_FILENO, read_buffer , length);
    sprint(write_buffer,"Hi client!\n");
    /*pipes2 파이프에 데이터 쓰기 */
    write(pipes2[1], write_buffer,strlen(write_buffer));

    wait(NULL);
    /*자식 프로세스는 */
    } else {
        /*pipes1의 읽기 파일 식별자 닫기*/
        close(pipes1[0]);
        /*pipes2의 쓰기 파일 식별자 닫기*/
        close(pipes1[1]);

        sprintf(write_buffer,"Hi server!\n");
        /*pipes1 파이프에 데이터 쓰기 */
        write(pipes1[1],write_buffer,strlen(write_buffer));
        /*pipes2 파이프로부터 데이터 읽기*/
        if((length=read(pipes2[0],read_buffer,SIZE) == -1) {
        perror("read failed");
        exit(1);
     }
     write(STDOUT_FILENO, "receive message : " ,strlen("receive message: "));
     write(STDOUT_FILENO, read_buffer , length);
    }
    exit(0);
}

Posted by 몰라욧