读雷锋日记有感:匿名管道
来源:百度文库 编辑:中财网 时间:2024/04/29 03:50:01
#include
#include
#include
#include
#define MAX_LINE 80
void main()
{
int MyPipe[2],ret;
char buf[MAX_LINE + 1];
const char *testbuf = "test string!";
if( pipe ( MyPipe ) == 0 ){
if( fork() == 0){
close(MyPipe[1]);
sleep(2);
ret = read( MyPipe[0] , buf , MAX_LINE );
buf[ret] = 0;
printf("Child read %s \n", buf);
printf("子进程读取数据成功!\n");
close(MyPipe[1]);
exit(1);
}
else
if( fork() == 0){
close(MyPipe[0]);
ret = write( MyPipe[1] , testbuf , strlen(testbuf) );
printf("父进程写管道成功!\n");
close(MyPipe[1]);
printf("父进程关闭写管道成功!\n");
sleep(2);
}
}
return 0;
}
#include
#include
#include
#define MAX_LINE 80
void main()
{
int MyPipe[2],ret;
char buf[MAX_LINE + 1];
const char *testbuf = "test string!";
if( pipe ( MyPipe ) == 0 ){
if( fork() == 0){
close(MyPipe[1]);
sleep(2);
ret = read( MyPipe[0] , buf , MAX_LINE );
buf[ret] = 0;
printf("Child read %s \n", buf);
printf("子进程读取数据成功!\n");
close(MyPipe[1]);
exit(1);
}
else
if( fork() == 0){
close(MyPipe[0]);
ret = write( MyPipe[1] , testbuf , strlen(testbuf) );
printf("父进程写管道成功!\n");
close(MyPipe[1]);
printf("父进程关闭写管道成功!\n");
sleep(2);
}
}
return 0;
}