请问如何用c语言实现pipe

tomlee368 2011-02-23 10:31:07
大家好:

我想用c语言实现一个pipe, 例如


command1 1<< | command2 1>> | command3 1>>


两个输出传递给一个输入

请问该用什么技术实现?

谢谢
...全文
491 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2011-03-14
  • 打赏
  • 举报
回复
Example 1

/* PIPE.C: This program uses the _pipe function to pass streams of
* text to spawned processes.
*/

#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <math.h>

enum PIPES { READ, WRITE }; /* Constants 0 and 1 for READ and WRITE */
#define NUMPROBLEM 8

void main( int argc, char *argv[] )
{

int hpipe[2];
char hstr[20];
int pid, problem, c;
int termstat;

/* If no arguments, this is the spawning process */
if( argc == 1 )
{

setvbuf( stdout, NULL, _IONBF, 0 );

/* Open a set of pipes */
if( _pipe( hpipe, 256, O_BINARY ) == -1 )
exit( 1 );


/* Convert pipe read handle to string and pass as argument
* to spawned program. Program spawns itself (argv[0]).
*/
itoa( hpipe[READ], hstr, 10 );
if( ( pid = spawnl( P_NOWAIT, argv[0], argv[0],
hstr, NULL ) ) == -1 )
printf( "Spawn failed" );

/* Put problem in write pipe. Since spawned program is
* running simultaneously, first solutions may be done
* before last problem is given.
*/
for( problem = 1000; problem <= NUMPROBLEM * 1000; problem += 1000)
{

printf( "Son, what is the square root of %d?\n", problem );
write( hpipe[WRITE], (char *)&problem, sizeof( int ) );

}

/* Wait until spawned program is done processing. */
_cwait( &termstat, pid, WAIT_CHILD );
if( termstat & 0x0 )
printf( "Child failed\n" );


close( hpipe[READ] );
close( hpipe[WRITE] );

}

/* If there is an argument, this must be the spawned process. */
else
{

/* Convert passed string handle to integer handle. */
hpipe[READ] = atoi( argv[1] );

/* Read problem from pipe and calculate solution. */
for( c = 0; c < NUMPROBLEM; c++ )
{

read( hpipe[READ], (char *)&problem, sizeof( int ) );
printf( "Dad, the square root of %d is %3.2f.\n",
problem, sqrt( ( double )problem ) );

}
}
}


Output

Son, what is the square root of 1000?
Son, what is the square root of 2000?
Son, what is the square root of 3000?
Son, what is the square root of 4000?
Son, what is the square root of 5000?
Son, what is the square root of 6000?
Son, what is the square root of 7000?
Son, what is the square root of 8000?
Dad, the square root of 1000 is 31.62.
Dad, the square root of 2000 is 44.72.
Dad, the square root of 3000 is 54.77.
Dad, the square root of 4000 is 63.25.
Dad, the square root of 5000 is 70.71.
Dad, the square root of 6000 is 77.46.
Dad, the square root of 7000 is 83.67.
Dad, the square root of 8000 is 89.44.


Example 2

// This is a simple filter application. It will spawn
// the application on command line. But before spawning
// the application, it will create a pipe that will direct the
// spawned application's stdout to the filter. The filter
// will remove ASCII 7 (beep) characters.

// Beeper.Cpp

/* Compile options needed: None */
#include <stdio.h>
#include <string.h>

int main()
{
int i;
for(i=0;i<100;++i)
{
printf("\nThis is speaker beep number %d... \n\7", i+1);
}
return 0;
}


// BeepFilter.Cpp
/* Compile options needed: none
Execute as: BeepFilter.exe <path>Beeper.exe
*/
#include <windows.h>
#include <process.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>

#define OUT_BUFF_SIZE 512
#define READ_HANDLE 0
#define WRITE_HANDLE 1
#define BEEP_CHAR 7

char szBuffer[OUT_BUFF_SIZE];

int Filter(char* szBuff, ULONG nSize, int nChar)
{
char* szPos = szBuff + nSize -1;
char* szEnd = szPos;
int nRet = nSize;

while (szPos > szBuff)
{
if (*szPos == nChar)
{
memmove(szPos, szPos+1, szEnd - szPos);
--nRet;
}
--szPos;
}
return nRet;
}

int main(int argc, char** argv)
{
int nExitCode = STILL_ACTIVE;
if (argc >= 2)
{
HANDLE hProcess;
int hStdOut;
int hStdOutPipe[2];

// Create the pipe
if(_pipe(hStdOutPipe, 512, O_BINARY | O_NOINHERIT) == -1)
return 1;

// Duplicate stdout handle (next line will close original)
hStdOut = _dup(_fileno(stdout));

// Duplicate write end of pipe to stdout handle
if(_dup2(hStdOutPipe[WRITE_HANDLE], _fileno(stdout)) != 0)
return 2;

// Close original write end of pipe
close(hStdOutPipe[WRITE_HANDLE]);

// Spawn process
hProcess = (HANDLE)spawnvp(P_NOWAIT, argv[1],
(const char* const*)&argv[1]);

// Duplicate copy of original stdout back into stdout
if(_dup2(hStdOut, _fileno(stdout)) != 0)
return 3;

// Close duplicate copy of original stdout
close(hStdOut);

if(hProcess)
{
int nOutRead;
while (nExitCode == STILL_ACTIVE)
{
nOutRead = read(hStdOutPipe[READ_HANDLE],
szBuffer, OUT_BUFF_SIZE);
if(nOutRead)
{
nOutRead = Filter(szBuffer, nOutRead, BEEP_CHAR);
fwrite(szBuffer, 1, nOutRead, stdout);
}

if(!GetExitCodeProcess(hProcess,(unsigned long*)&nExitCode))
return 4;
}
}
}

printf("\nPress \'ENTER\' key to continue... ");
getchar();
return nExitCode;
}


  • 打赏
  • 举报
回复
自己实现一个pipe?
个人建议:
建立一个缓冲区(如何建立可以用共享内存,数组,内存映射)
然后直接访问缓冲区就可以
tomlee368 2011-02-24
  • 打赏
  • 举报
回复
你是说可以三个process或者是thread实现,后两个算结果,第一个等结果? 可以这样理解吗?
bobo364 2011-02-24
  • 打赏
  • 举报
回复
其实如果把这三个看做三个函数的话,可以用2个变量来保存前2个函数的返回值,在传给第三个函数作为参数
TMAC10052120353 2011-02-24
  • 打赏
  • 举报
回复
LINUX C下有对应函数,可以实现,LZ可以去参考
辰岡墨竹 2011-02-24
  • 打赏
  • 举报
回复
Shell里Pipe是|,<<和>>是输入输出重定向……
xiaocai0001 2011-02-23
  • 打赏
  • 举报
回复
看不懂你写的例子.

Shell里的Pipe是用'|'表示, <<, >> 表示什么意思? cin/cout ?

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧