Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <ctype.h>
- void err_sys(const char* x) { perror(x); exit(1); }
- void upper(char *s) { while((*s = toupper(*s))) ++s; }
- void filter(int input, int output)
- {
- char buff[1024];
- bzero(buff, sizeof(buff));
- size_t n = read(input, buff, sizeof(buff));
- printf("process %ld: got '%s'\n", (long) getpid(), buff);
- upper(buff);
- write(output, buff, strlen(buff));
- }
- void filter2(int input, int output)
- {
- if (dup2(input, 0) != 0) err_sys("dup2(input, 0)");
- if (dup2(output, 1) != 1) err_sys("dup2(output, 1)");
- execlp("/usr/bin/tr", "tr", "[a-z]", "[A-Z]" , (char*)0);
- }
- int main(int argc, char** argv)
- {
- int pipe1[2];
- int pipe2[2];
- if (pipe(pipe1) < 0) err_sys("pipe1");
- if (pipe(pipe2) < 0) err_sys("pipe2");
- pid_t pid;
- if ((pid = fork()) < 0) err_sys("fork");
- else if (pid > 0)
- {
- close(pipe1[0]);
- close(pipe2[1]);
- char* s = "Hello there, can you please uppercase this and send it back to me? Thank you!";
- write(pipe1[1], s, strlen(s));
- close(pipe1[1]);
- char buff[1024];
- bzero(buff, sizeof(buff));
- size_t n = read(pipe2[0], buff, sizeof(buff));
- pid_t mypid = getpid();
- printf("process %ld: got '%s'\n", (long) mypid, buff);
- } else
- { // Child.
- close(pipe1[1]);
- close(pipe2[0]);
- filter(pipe1[0], pipe2[1]);
- //filter2(pipe1[0], pipe2[1]); // FIXME: This doesn't work
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement