#include <unistd.h>どうです?すごいでしょ!dup2とかread、close関数は気が向いたら解説します。
#include <stdio.h>
#include <signal.h>int main()
{int fd[2];}
int pid,i;
char buff[256];pipe( fd ); /* パイプの作成 */
sigignore( SIGCLD ); /*子プロセスは勝手に死亡するようにする(forkを使うときのお まじない。そのうち解説します)*/
if( (pid=fork()) == 0 ){
/* 子プロセス側 */
/* 子プロセスの標準出力をパイプに関連づける */
close( STDOUT_FILENO );
dup2( fd[1] , STDOUT_FILENO );
close( fd[0] );/* 本来ならここでexecl関数を実行。面倒くさいので代わりに printfを実行 */
printf("this is test!\n"); /* 標準出力ではなくパイプにデータを出力する */
exit(0);
}/* 親プロセス側 */
/* buffを0で初期化 */
for( i=0; i<256 ;i++ ){
buff[i] = 0;
}
read( fd[0] , buff , 256 ); /* パイプに出力されたデータをbuffに 読み込む */printf("buff = %s\n",buff);
return 0;