Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <math.h>
- #include <iostream>
- #include <assert.h>
- #include <string>
- #include <stdlib.h>
- #include <map>
- #include <stdio.h>
- #include <curl/curl.h>
- #include <vector>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <arpa/inet.h>
- using namespace std;
- int main()
- {
- int pid;
- int pc[2]; /* Parent to child pipe */
- int cp[2]; /* Child to parent pipe */
- char ch;
- int incount = 0, outcount = 0;
- /* Make pipes */
- if( pipe(pc) < 0)
- {
- perror("Can't make pipe");
- exit(1);
- }
- if( pipe(cp) < 0)
- {
- perror("Can't make pipe");
- exit(1);
- }
- /* Create a child to run command. */
- switch( pid = fork() )
- {
- case -1:
- perror("Can't fork");
- exit(1);
- case 0:
- /* Child. */
- close(STDOUT_FILENO); /* Close current stdout. */
- dup2(cp[1], STDOUT_FILENO);
- close(STDIN_FILENO);
- dup2(pc[0], STDIN_FILENO);
- close( pc[1]);
- close( cp[0]);
- execlp("cat", "cat", NULL);
- exit(1);
- default:
- /* Parent. */
- /* Close what we don't need. */
- printf("Input to child:\n");
- string theinput("Hey there baby");
- write(pc[1], theinput.c_str(), theinput.size());
- close(pc[1]);
- cout << "The input : " << theinput << endl;
- printf("\nOutput from child:\n");
- close(cp[1]);
- while( read(cp[0], &ch, 1) == 1)
- {
- write(1, &ch, 1);
- outcount++;
- }
- exit(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement