http://blog.chinaunix.net/uid-26000296-id-3408970.html
/*
* \File
* main.c
* \Descript
* father-process reads input file and sends to child-process by anonymous-pipe
* client-process transform and write into output file
*/ #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h> FILE* fp_in = NULL;
FILE* fp_out = NULL;
#define TESTF_IN "test.dat"
#define TESTF_OUT "out.dat"
#define MAX_LINE 512
#define OP_LEN 100 /*
* \File
* trans.h
* \Descript
*
*/ #ifndef __TRANS_H__
#define __TRANS_H__ int trans_lower2upper(char* buf_in, char* buf_out, int len); #endif /*
* \Func
* main
* \Descript
*
*/ int main(char argc, char* argv[])
{
int pidstatus;
int fdfile_in, fdfile_out; int fdpipe_a[];
int fdpipe_b[];
int pid_trans = -, pid_write = -; /* Create anonymous-pipe */
if( (pipe(fdpipe_a) < ) || (pipe(fdpipe_b) < )) //创建两个管道
{
printf("open pipe failed.\n");
exit();
} if ( (fp_in = fopen(TESTF_IN, "r")) < ) //打开test.dat文件,用于读
{
printf("open input file failed: %s\n", TESTF_IN);
exit();
} if ( (fp_out = fopen(TESTF_OUT, "w")) < ) //新建out.dat文件,用于写
{
printf("open input file failed: %s\n", TESTF_OUT);
exit();
} if ( (pid_trans = fork()) && (pid_write = fork()) ) //创建两个子进程,父进程工作内容
{
/*
* PARENT_PROCESS:
* read data from in-file, write it into anonymos-pipe.
*/ int s_read = , s_write;
char bufparent[]; while((s_read = fread(bufparent, sizeof(char), OP_LEN ,fp_in) ) ) //读test.dat文件
{
printf("***** %d, %s\n", s_read, bufparent);
if ( (s_write = write(fdpipe_a[], bufparent, s_read)) < )//向管道写
{
printf("write pipe failed.\n");
exit();
}
memset(bufparent, , );
if( feof(fp_in) != ) //检查文件是否结束
{
printf("\n***** read test.dat over ******\n");
exit();
}
}
}
else if ( pid_trans == )
{
/*
* TRANS_PROCESS:
* read anonymous-pipe, transcode, write anonymos-pipe.
*/ char buftrans_in[], buftrans_out[];
int size_read, size_write;
int ret; while(size_read = read(fdpipe_a[], buftrans_in, OP_LEN)) //读管道
{
ret = trans_lower2upper(buftrans_in, buftrans_out, size_read); if ( (size_write = write(fdpipe_b[], buftrans_out, size_read)) < ) //写管道
{
printf("trans-process write failed.\n");
exit();
}
}
}
else if ( pid_write == )
{
/*
* WRITE_PROCESS:
* read anonymous-pipe, write it into out-file
*/
int s_read, s_write;
char bufwrite[]; while ( s_read = read(fdpipe_b[], bufwrite, OP_LEN) ) //读管道
{
if( (s_write = fwrite(bufwrite, sizeof(char), s_read, fp_out)) < ) //写out.dat文件
{
printf("..... write error.\n");
exit();
}
if(s_read < OP_LEN)
{
break;
}
}
}
else
{
printf("fork process failed.\n");
exit();
} waitpid(pid_trans, &pidstatus, );
waitpid(pid_write, &pidstatus, ); fclose(fp_out);
fclose(fp_in); int s_read2 = ;
char bufparent2[];
if ( (fp_out = fopen(TESTF_OUT, "r")) < ) //新建out.dat文件,用于写
{
printf("open input file failed: %s\n", TESTF_OUT);
exit();
} s_read2 = fread(bufparent2, sizeof(char), OP_LEN ,fp_out);
printf("***** %d, %s\n", s_read2, bufparent2);
if( feof(fp_out) != ) //检查文件是否结束
{
printf("\n***** pirint out.dat over ******\n");
exit();
} fclose(fp_out); return ; } /*
* \Func
* trans_lower2upper
* \Descript
* Lowercase turn uppercase
*/
int trans_lower2upper(char* buf_in, char* buf_out, int buf_len) //逐个字符转换成大写字母
{
int len = buf_len;
char* cp_in = buf_in;
char* cp_out = buf_out;
char atom;
char offset; while(len--)
{
atom = *(cp_in++); if( (atom >= 'a') && (atom <= 'z') )
{
offset = atom - 'a';
atom = 'A' + offset;
}
*(cp_out++) = atom;
} return ;
}
运行结果如下
出现的问题是 打印out.dat文件内容时,打印了wujing@Ubuntu:$
http://www.cnblogs.com/biyeymyhjob/archive/2012/11/03/2751593.html