名前なしパイプ(1)
パイプを生成する.
成功した場合は 0
失敗した場合は -1(EOF)
- #include<unistd.h>
- int pipe(int pipefd[2]);
pipe[0]で読み込んだmsgをbuffに格納する.
- /* 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.