#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#define K 1024
int main(void)
{
int result = -1;
int fd[2], nbytes;
pid_t pid;
char string[ ] = "hello, pipe";
char readbuffer[K];
int *write_fd = &fd[1];
int *read_fd = &fd[0];
result = pipe(fd);
if (result == -1) {
printf("build pipe failed.\n");
return -1;
}
pid = fork();
if (pid == -1) {
printf("fork progess failed.\n");
return -1;
}
if (pid == 0) {
close(*read_fd);
result = write(*write_fd, string, strlen(string));
return 0;
} else {
close(*write_fd);
nbytes = read(*read_fd, readbuffer, sizeof(readbuffer));
printf("recv: %d bytes, data: %s\n", nbytes, readbuffer);
}
return 0;
}
pipe管道
最新推荐文章于 2020-06-04 17:39:03 发布