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

名前つきパイプ(2)

名前なしパイプ(2)と同じようなことをここではします. また整数の加算です.

クライアントサイドプログラム

with line numberplain text
  1. /* client.c */
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<string.h>
  5. #include<unistd.h>
  6. #include<sys/stat.h>
  7. #include<sys/types.h>
  8. #include<fcntl.h>
  9.  
  10. #define FIFO0 "./FIFO0" //Read
  11. #define FIFO1 "./FIFO1" //Write
  12.  
  13. void error_message(const char *name);
  14. void unlink_pipe(void);
  15.  
  16. int main(int argc, char **argv)
  17. {
  18.     int fd0, fd1;
  19.     char buff[32], result[32];
  20.  
  21.     if((fd0 = open(FIFO0, O_RDONLY)) == EOF || (fd1 = open(FIFO1, O_WRONLY)) == EOF)
  22.         error_message("open");
  23.  
  24.     printf("NumA:");
  25.     fgets(buff, 32, stdin);
  26.     if(write(fd1, buff, strlen(buff) + 1) == EOF)
  27.         error_message("write");
  28.  
  29.     printf("NumB:");
  30.     fgets(buff, 32, stdin);
  31.     if(write(fd1, buff, strlen(buff) + 1) == EOF)
  32.         error_message("write");
  33.  
  34.     if(read(fd0, result, 32) == EOF)
  35.         error_message("read");
  36.  
  37.     printf("Answer = %s\n", result);
  38.  
  39.     unlink_pipe();
  40.  
  41.     return 0;
  42. }
  43.  
  44. void error_message(const char *name) {
  45.     perror(name);
  46.     exit(EXIT_FAILURE);
  47. }
  48.  
  49. void unlink_pipe(void) {
  50.     if(unlink(FIFO0) == EOF)
  51.         error_message("unlink");
  52.     if(unlink(FIFO1) == EOF)
  53.         error_message("unlink");
  54. }
/* 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
  1. /* server.c */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <fcntl.h>
  9.  
  10. #define FIFO0 "./FIFO0" //Write
  11. #define FIFO1 "./FIFO1" //Read
  12.  
  13. void error_message(const char *name);
  14.  
  15. int main(int argc, char **argv)
  16. {
  17.     int fd0, fd1;
  18.     int na, nb, nc;
  19.     char buff[32], result[32];
  20.  
  21.     if(access(FIFO0, 0777) == EOF)
  22.         mkfifo(FIFO0, 0777);
  23.     if(access(FIFO1, 0777) == EOF)
  24.         mkfifo(FIFO1, 0777);
  25.  
  26.     if((fd0 = open(FIFO0, O_WRONLY)) == EOF)
  27.         error_message("open");
  28.     if((fd1 = open(FIFO1, O_RDONLY)) == EOF)
  29.         error_message("open");
  30.  
  31.     if(read(fd1, buff, 32) == EOF)
  32.         error_message("read");
  33.     na = atoi(buff);
  34.  
  35.     printf("Get Number A = %d\n", na);
  36.  
  37.     if(read(fd1, buff, 32) == EOF)
  38.         error_message("read");
  39.     nb = atoi(buff);
  40.  
  41.     printf("Get Number B = %d\n", nb);
  42.  
  43.     nc = na + nb;
  44.  
  45.     printf("Returned Answer to Client...\n");
  46.  
  47.     sprintf(result, "%d", nc);
  48.  
  49.     if(write(fd0, result, strlen(result) + 1) == EOF)
  50.         error_message("write");
  51.  
  52.     return 0;
  53. }
  54.  
  55. void error_message(const char *name) {
  56.     perror(name);
  57.     exit(EXIT_FAILURE);
  58. }
  59.  
/* 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


プログラムの説明

クライアントサイドの説明

with line numberplain text
  1. #define FIFO0 "./FIFO0" //Read
  2. #define FIFO1 "./FIFO1" //Write
#define FIFO0 "./FIFO0" //Read
#define FIFO1 "./FIFO1" //Write
                            
with line numberplain text
  1. if((fd0 = open(FIFO0, O_RDONLY)) == EOF || (fd1 = open(FIFO1, O_WRONLY)) == EOF)
  2.     error_message("open");
if((fd0 = open(FIFO0, O_RDONLY)) == EOF || (fd1 = open(FIFO1, O_WRONLY)) == EOF)
    error_message("open");
                            
"define"で指定した名前つきパイプを開きます.
with line numberplain text
  1. printf("NumA:");
  2. fgets(buff, 32, stdin);
  3. if(write(fd1, buff, strlen(buff) + 1) == EOF)
  4.     error_message("write");
  5.  
  6. printf("NumB:");
  7. fgets(buff, 32, stdin);
  8. if(write(fd1, buff, strlen(buff) + 1) == EOF)
  9.     error_message("write");
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");
整数を文字列として名前つきパイプに書き込みます.
この時, 同じファイルディスクリプタに書き込んでいますが, 実際はサーバーを実行しているため違う値としてサーバー側に書き込まれています.
with line numberplain text
  1. if(read(fd0, result, 32) == EOF)
  2.     error_message("read");
  3.  
  4. printf("Answer = %s\n", result);
if(read(fd0, result, 32) == EOF)
    error_message("read");

printf("Answer = %s\n", result);
サーバーから送信されたデータを読み込み, 表示させます.

サーバサイドの説明
with line numberplain text
  1. if(access(FIFO0, 0777) == EOF)
  2.     mkfifo(FIFO0, 0777);
  3. if(access(FIFO1, 0777) == EOF)
  4.     mkfifo(FIFO1, 0777);
if(access(FIFO0, 0777) == EOF)
    mkfifo(FIFO0, 0777);
if(access(FIFO1, 0777) == EOF)
    mkfifo(FIFO1, 0777);
"access"関数でファイルが存在しない場合のみ, 名前つきパイプを作成させる.
with line numberplain text
  1. if(read(fd1, buff, 32) == EOF)
  2.     error_message("read");
  3. na = atoi(buff);
  4.  
  5. if(read(fd1, buff, 32) == EOF)
  6.     error_message("read");
  7. nb = atoi(buff);
if(read(fd1, buff, 32) == EOF)
    error_message("read");
na = atoi(buff);

if(read(fd1, buff, 32) == EOF)
    error_message("read");
nb = atoi(buff);
名前つきパイプを通じて取得したデータを整数に変換してあらかじめ用意した変数に格納.
with line numberplain text
  1. sprintf(result, "%d", nc);
  2.  
  3. if(write(fd0, result, strlen(result) + 1) == EOF)
  4.     error_message("write");
sprintf(result, "%d", nc);

if(write(fd0, result, strlen(result) + 1) == EOF)
    error_message("write");
加算処理させた変数"nc"を文字列"result"に格納し書き込み専用で開いた名前つきパイプに書き込む.

プログラム別に記述しましたが, 一連の流れはプログラム同士が連帯しているので実行しながら確認してください.