匿名管道-实例

很多书本或是参考资料讲解进程间通信的例子,
通常都只是一个父进程与子进程间的实现,对于实用的启发性不大,

本例是我自己写的一个实现:
生成了两个子进程:转换进程和写进程;
实现了读取文件,转换成大写,并写输出文件的功能。

父进程不断读文件的数据并通过匿名管道发送到转换进程;
转换进程从匿名管道读取数据,转换后,通过匿名管道发送到写进程;
写进程从匿名管道读取数据并写到文件。

下面是流程图:

这样的设计是对多进程并行工作的模拟,
可以使得读取,转码,写进程实现流水线般的并行,
稍作改进,添加功能后,能用在很多的场合。

以下是代码实现:
main.c

  1. /*
  2.  * \File
  3.  *   main.c
  4.  * \Descript
  5.  *   father-process reads input file and sends to child-process by anonymous-pipe
  6.  *   client-process transform and write into output file
  7.  */

  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <fcntl.h>
  13. #include <unistd.h>

  14. #include "trans.h"

  15. FILE* fp_in = NULL;
  16. FILE* fp_out = NULL;
  17. #define TESTF_IN "test.dat"
  18. #define TESTF_OUT "out.dat"
  19. #define MAX_LINE 512
  20. #define OP_LEN 100
  21. /*
  22.  * \Func
  23.  * main
  24.  * \Descript
  25.  *
  26.  */
  27. int main(char argc, char* argv[])
  28. {
  29.   int pidstatus;
  30.   int fdfile_in, fdfile_out;

  31.   int fdpipe_a[2];
  32.   int fdpipe_b[2];
  33.   int pid_trans = -1, pid_write = -1;


  34.   /* Create anonymous-pipe */
  35.   if( (pipe(fdpipe_a) < 0) || (pipe(fdpipe_b) < 0))
  36.   {
  37.     printf("open pipe failed.\n");
  38.     exit(1);
  39.   }

  40.   if ( (fp_in = fopen(TESTF_IN, "r")) < 0 )
  41.   {
  42.     printf("open input file failed: %s\n", TESTF_IN);
  43.     exit(1);
  44.   }

  45.   if ( (fp_out = fopen(TESTF_OUT, "w")) < 0 )
  46.   {
  47.     printf("open input file failed: %s\n", TESTF_OUT);
  48.     exit(1);
  49.   }

  50.   if ( (pid_trans = fork()) && (pid_write = fork()) )    
  51.   {
  52.     /* 
  53.      * PARENT_PROCESS: 
  54.      * read data from in-file, write it into anonymos-pipe.
  55.      */

  56.      int s_read = 0, s_write;
  57.      char bufparent[512];    

  58.      while((s_read = fread(bufparent, sizeof(char), OP_LEN ,fp_in) ) )
  59.      {
  60.        printf("***** %d, %s\n", s_read, bufparent);
  61.        if ( (s_write = write(fdpipe_a[1], bufparent, s_read)) < 0 )
  62.        {
  63.          printf("write pipe failed.\n");
  64.          exit(1);
  65.        }
  66.        memset(bufparent, 0, 512);
  67.        if( feof(fp_in) != 0 )
  68.        {
  69.          printf("\n***** OVER ******\n");
  70.          exit(1);
  71.        }
  72.      }
  73.   }
  74.   else if ( pid_trans == 0 )
  75.   {
  76.     /* 
  77.      * TRANS_PROCESS: 
  78.      * read anonymous-pipe, transcode, write anonymos-pipe. 
  79.      */

  80.     char buftrans_in[512], buftrans_out[512];
  81.     int size_read, size_write;
  82.     int ret;

  83.     while(size_read = read(fdpipe_a[0], buftrans_in, OP_LEN))
  84.     {
  85.       ret = trans_lower2upper(buftrans_in, buftrans_out, size_read);

  86.       if ( (size_write = write(fdpipe_b[1], buftrans_out, size_read)) < 0 )
  87.       {
  88.         printf("trans-process write failed.\n");
  89.         exit(2);
  90.        }
  91.     }
  92.   }
  93.   else if ( pid_write == 0 )
  94.   {
  95.     /* 
  96.      * WRITE_PROCESS:
  97.      * read anonymous-pipe, write it into out-file
  98.      */
  99.     int s_read, s_write;
  100.     char bufwrite[512];

  101.     while ( s_read = read(fdpipe_b[0], bufwrite, OP_LEN) )
  102.     {
  103.       if( (s_write = fwrite(bufwrite, sizeof(char), s_read, fp_out)) < 0)
  104.       {
  105.         printf("..... write error.\n");
  106.         exit(3);
  107.       }
  108.       if(s_read < OP_LEN)
  109.       {
  110.         break;
  111.       }
  112.     }
  113.   }
  114.   else
  115.   {
  116.       printf("fork process failed.\n");
  117.       exit(1);
  118.   }

  119.   waitpid(pid_trans, &pidstatus, 0);
  120.   waitpid(pid_write, &pidstatus, 0);
  121.   fclose(fp_out);
  122.   fclose(fp_in);
  123.   return 0;
  124. }

trans.h

  1. /*
  2.  * \File
  3.  * trans.h 
  4.  * \Descript 
  5.  * 
  6.  */ 
  7.     
  8. #ifndef __TRANS_H__ 
  9. #define __TRANS_H__ 
  10.                                                                                           
  11. int trans_lower2upper(char* buf_in, char* buf_out, int len);
  12.       
  13. #endif

trans.c

  1. /*
  2.  * \File
  3.  * trans.c
  4.  * \Descript 
  5.  * 
  6.  */ 
  7.     
  8. #include <stdio.h>
  9. #include <stdlib.h> 
  10.     
  11. #include "trans.h"
  12.                                                                                        
  13. /*
  14.  * \Func
  15.  * trans_lower2upper
  16.  * \Descript
  17.  * Lowercase turn uppercase
  18.  */
  19. int trans_lower2upper(char* buf_in, char* buf_out, int buf_len)
  20. {
  21.   int len = buf_len;
  22.   char* cp_in = buf_in;
  23.   char* cp_out = buf_out;
  24.   char atom;
  25.   char offset;

  26.   while(len--)
  27.   {
  28.     atom = *(cp_in++);
  29.     
  30.     if( (atom >= 'a') && (atom <= 'z') )
  31.     {
  32.       offset = atom - 'a';
  33.       atom = 'A' + offset;
  34.     }
  35.     *(cp_out++) = atom;
  36.   }

  37.   return 0;
  38. }

makefile

  1. OBJECTS = main.o trans.o
  2. CC = gcc

  3. anonymous_pipe : $(OBJECTS) 
  4.   $(CC) -o anonymous_pipe $(OBJECTS)

  5. main.o:trans.h
  6. trans.o:trans.h

  7. .PHONY:clean
  8. clean:
  9.   rm anonymous_pipe $(OBJECTS)
测试通过;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值