You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
830 B

/*
* 成功测试时的输出:
* " Write to pipe successfully."
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <assert.h>
static int fd[2];
void test_pipe(void){
int cpid;
char buf[128] = {0};
int ret = pipe2(fd, 0);
assert(ret != -1);
const char *data = " Write to pipe successfully.\n";
cpid = fork();
printf("cpid: %d\n", cpid);
if(cpid > 0){
close(fd[1]);
while(read(fd[0], buf, 1) > 0)
write(STDOUT_FILENO, buf, 1);
write(STDOUT_FILENO, "\n", 1);
close(fd[0]);
wait(NULL);
}else{
close(fd[0]);
write(fd[1], data, strlen(data));
close(fd[1]);
exit(0);
}
}
int main(void){
test_pipe();
return 0;
}