IPC with pipes, also dup2 redirect stream handle

时间:2023-03-09 00:15:48
IPC with pipes, also dup2 redirect stream handle
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h> int main()
{
int fds[];
pid_t pid; pipe( fds );
pid = fork();
if( pid == (pid_t) )
{
close( fds[] );
dup2(fds[], STDIN_FILENO );
execlp( "sort", "sort", );
}
else
{
FILE *stream;
close(fds[]);
stream = fdopen( fds[], "w" );
fprintf( stream, "This is a test.\n" );
fprintf( stream, "Hello, world.\n" );
fprintf( stream, "My dog has fleas.\n" );
fprintf( stream, "My dream will come true.\n");
fflush( stream );
close( fds[] );
waitpid( pid, NULL, );
}
}