1、实验代码
pipe.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define handle_error(msg) \
{perror(msg);exit(EXIT_FAILURE);}
int main (int argc, char *argv[])
{
int pipe_fd[2];
if (pipe (pipe_fd) == -1)
handle_error("pipe");
int ret_from_fork;
ret_from_fork = fork ();
if (ret_from_fork == -1)
handle_error("fork");
if (ret_from_fork == 0) //child process
{
char str[100] = {0};
printf("child process:\n input string:");
scanf ("%s", str);
//gets(str);
write (pipe_fd[1], str, strlen (str)); // write data to pipe
close (pipe_fd[1]);
_exit (EXIT_SUCCESS);
}
if (ret_from_fork > 0) // parent process
{
char buf[30] = {0};
read (pipe_fd[0], buf, 30); // read data from pipe
printf ("parent process: %s\n", buf);
close (pipe_fd[0]);
exit (EXIT_SUCCESS);
}
return 0;
}
pipe-v2.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#define handle_error(msg) \
{perror(msg);exit(EXIT_FAILURE);}
int main (int argc, char *argv[])
{
int pipe_fd1[2];
int pipe_fd2[2];
if (pipe (pipe_fd1) == -1 || pipe (pipe_fd2) == -1)
handle_error("pipe");
int ret_from_fork;
ret_from_fork = fork ();
if (ret_from_fork == -1)
handle_error("fork");
if (ret_from_fork == 0) //child process
{
char read_buf1[100] = {0};
char write_buf1[100] = {0};
printf("child process:");
scanf ("%s", write_buf1);
//gets(str);
write (pipe_fd1[1], write_buf1, strlen (write_buf1));
close (pipe_fd1[1]);
//sleep(5);
read (pipe_fd2[0], read_buf1, 100);
printf ("in child process: %s\n", read_buf1);
close (pipe_fd2[0]);
_exit (EXIT_SUCCESS);
}
if (ret_from_fork > 0) // parent process
{
char read_buf2[100] = {0};
char write_buf2[100] = {0};
read (pipe_fd1[0], read_buf2, 100); // read data from pipe
printf ("in parent process: %s\n", read_buf2);
close (pipe_fd1[0]);
printf ("parent process:");
scanf ("%s", write_buf2);
write (pipe_fd2[1], write_buf2, strlen (write_buf2));
close (pipe_fd2[1]);
exit (EXIT_SUCCESS);
}
return 0;
}
2、执行结果