[PR] この広告は3ヶ月以上更新がないため表示されています。
ホームページを更新後24時間以内に表示されなくなります。

名前なしパイプ(1)

パイプを生成する.
成功した場合は 0
失敗した場合は -1(EOF)

with line numberplain text
  1. #include<unistd.h>
  2. int pipe(int pipefd[2]);
#include<unistd.h>
int pipe(int pipefd[2]);
                        
次のプログラムは, 読み書きするパイプを生成し, pipe[1]にmsgを書き込み,
pipe[0]で読み込んだmsgをbuffに格納する.
with line numberplain text
  1. /* pipe1.c */
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<unistd.h>
  6. #include<fcntl.h>
  7. #include<sys/stat.h>
  8. #include<sys/types.h>
  9.  
  10. void closepipe(int *pipe);
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.     int pp[2];
  15.     char *msg = "This is pipe Massage.\n";
  16.     char buff[BUFSIZ];
  17.  
  18.     if(pipe(pp) == EOF) {
  19.         perror("pipe");
  20.         exit(1);
  21.     }
  22.  
  23.     if(write(pp[1], msg, strlen(msg) + 1) == EOF) {
  24.         perror("write");
  25.         closepipe(pp);
  26.         exit(1);
  27.     }
  28.  
  29.     if(read(pp[0], buff, BUFSIZ) == EOF) {
  30.         perror("read");
  31.         closepipe(pp);
  32.         exit(1);
  33.     }
  34.  
  35.     printf("message: %s\n", buff);
  36.  
  37.     return 0;
  38. }
  39.  
  40. void closepipe(int *pipe) {
  41.     close(pipe[0]);
  42.     close(pipe[1]);
  43. }
                        /* pipe1.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>

void closepipe(int *pipe);

int main(int argc, char **argv)
{
    int pp[2];
    char *msg = "This is pipe Massage.\n";
    char buff[BUFSIZ];

    if(pipe(pp) == EOF) {
        perror("pipe");
        exit(1);
    }

    if(write(pp[1], msg, strlen(msg) + 1) == EOF) {
        perror("write");
        closepipe(pp);
        exit(1);
    }

    if(read(pp[0], buff, BUFSIZ) == EOF) {
        perror("read");
        closepipe(pp);
        exit(1);
    }

    printf("message: %s\n", buff);

    return 0;
}

void closepipe(int *pipe) {
    close(pipe[0]);
    close(pipe[1]);
}

$ ./pipe1
message: This is pipe Message.


pipe example

子プロセスから受け取ったメッセージを, パイプを通じて親プロセスに通信するプログラム.

with line numberplain text
  1. /* pipe2.c */
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<unistd.h>
  6. #include<sys/types.h>
  7.  
  8. #define BUFFSIZE 4096
  9.  
  10. void closepipe(int *pp);
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.     int pp[2];
  15.     int n, status;
  16.     char *msg = "Fork Message\n";
  17.     char buff[BUFFSIZE];
  18.     pid_t pid;
  19.  
  20.     if(pipe(pp) == EOF) {
  21.         perror("pipe");
  22.         exit(1);
  23.     }
  24.  
  25.     if((pid = fork()) == EOF) {
  26.         perror("fork");
  27.         closepipe(pp);
  28.         exit(1);
  29.     }
  30.  
  31.     if(pid == 0) {
  32.         close(pp[0]);
  33.         printf("(PID:%d): CHILD PROCESS\n", getpid());
  34.         printf("Writting Message for Pipe...\n\n");
  35.         if(write(pp[1], msg, strlen(msg) + 1) == EOF) {
  36.             perror("write");
  37.             closepipe(pp);
  38.             exit(1);
  39.         }
  40.     }
  41.  
  42.     else {
  43.         waitpid(pid, &status, 0);
  44.         close(pp[1]);
  45.         if(read(pp[0], buff, BUFFSIZE) == EOF) {
  46.             perror("read");
  47.             close(pp[0]);
  48.             exit(1);
  49.         }
  50.         printf("(PID:%d): Mother Process\n", getpid());
  51.         printf("message: %s\n", buff);
  52.         close(pp[0]);
  53.     }
  54.  
  55.     return 0;
  56. }
  57.  
  58. void closepipe(int *pp) {
  59.     close(pp[0]);
  60.     close(pp[1]);
  61. }
  62.  
/* pipe2.c */
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include<sys/types.h>

#define BUFFSIZE 4096

void closepipe(int *pp);

int main(int argc, char **argv)
{
    int pp[2];
    int n, status;
    char *msg = "Fork Message\n";
    char buff[BUFFSIZE];
    pid_t pid;

    if(pipe(pp) == EOF) {
        perror("pipe");
        exit(1);
    }

    if((pid = fork()) == EOF) {
        perror("fork");
        closepipe(pp);
        exit(1);
    }

    if(pid == 0) {
        close(pp[0]);
        printf("(PID:%d): CHILD PROCESS\n", getpid());
        printf("Writting Message for Pipe...\n\n");
        if(write(pp[1], msg, strlen(msg) + 1) == EOF) {
            perror("write");
            closepipe(pp);
            exit(1);
        }
    }

    else {
        waitpid(pid, &status, 0);
        close(pp[1]);
        if(read(pp[0], buff, BUFFSIZE) == EOF) {
            perror("read");
            close(pp[0]);
            exit(1);
        }
        printf("(PID:%d): Mother Process\n", getpid());
        printf("message: %s\n", buff);
        close(pp[0]);
    }

    return 0;
}

void closepipe(int *pp) {
    close(pp[0]);
    close(pp[1]);
}

                            
実行例

$ ./pipe2
(PID:10952): CHILD PROCESS
Writting Message for Pipe...

(PID:10951): Mother Process
message: Fork Message