名前つきパイプ(2)
名前なしパイプ(2)と同じようなことをここではします. また整数の加算です.
クライアントサイドプログラム
with line numberplain text
- /* client.c */
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<unistd.h>
- #include<sys/stat.h>
- #include<sys/types.h>
- #include<fcntl.h>
- #define FIFO0 "./FIFO0" //Read
- #define FIFO1 "./FIFO1" //Write
- void error_message(const char *name);
- void unlink_pipe(void);
- int main(int argc, char **argv)
- {
- int fd0, fd1;
- char buff[32], result[32];
- if((fd0 = open(FIFO0, O_RDONLY)) == EOF || (fd1 = open(FIFO1, O_WRONLY)) == EOF)
- error_message("open");
- printf("NumA:");
- fgets(buff, 32, stdin);
- if(write(fd1, buff, strlen(buff) + 1) == EOF)
- error_message("write");
- printf("NumB:");
- fgets(buff, 32, stdin);
- if(write(fd1, buff, strlen(buff) + 1) == EOF)
- error_message("write");
- if(read(fd0, result, 32) == EOF)
- error_message("read");
- printf("Answer = %s\n", result);
- unlink_pipe();
- return 0;
- }
- void error_message(const char *name) {
- perror(name);
- exit(EXIT_FAILURE);
- }
- void unlink_pipe(void) {
- if(unlink(FIFO0) == EOF)
- error_message("unlink");
- if(unlink(FIFO1) == EOF)
- error_message("unlink");
- }
with line numberplain text
- /* server.c */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #define FIFO0 "./FIFO0" //Write
- #define FIFO1 "./FIFO1" //Read
- void error_message(const char *name);
- int main(int argc, char **argv)
- {
- int fd0, fd1;
- int na, nb, nc;
- char buff[32], result[32];
- if(access(FIFO0, 0777) == EOF)
- mkfifo(FIFO0, 0777);
- if(access(FIFO1, 0777) == EOF)
- mkfifo(FIFO1, 0777);
- if((fd0 = open(FIFO0, O_WRONLY)) == EOF)
- error_message("open");
- if((fd1 = open(FIFO1, O_RDONLY)) == EOF)
- error_message("open");
- if(read(fd1, buff, 32) == EOF)
- error_message("read");
- na = atoi(buff);
- printf("Get Number A = %d\n", na);
- if(read(fd1, buff, 32) == EOF)
- error_message("read");
- nb = atoi(buff);
- printf("Get Number B = %d\n", nb);
- nc = na + nb;
- printf("Returned Answer to Client...\n");
- sprintf(result, "%d", nc);
- if(write(fd0, result, strlen(result) + 1) == EOF)
- error_message("write");
- return 0;
- }
- void error_message(const char *name) {
- perror(name);
- exit(EXIT_FAILURE);
- }
$ ./server
Get Number A : 1234
Get Number B : 4567
Returned Answer to Client...
$ ./client
NumA:1234
NumB:4567
Answer = 5801