Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/wait.h>
- #define R_FILE "/proc/meminfo"
- #define GREP_EXEC "/bin/grep"
- #define SORT_EXEC "/bin/sort"
- #define HEAD_EXEC "/usr/bin/head"
- #define BSIZE 256
- int main()
- {
- int status;
- pid_t pid_1, pid_2, pid_3;
- int pipe1[2], pipe2[2];
- if (pipe(pipe1) || pipe(pipe2))
- {
- printf("Error with pipes.\n");
- return -1;
- }
- pid_1 = fork();
- if (pid_1 == 0)
- {
- close(pipe1[0]);
- close(pipe2[0]);
- close(pipe2[1]);
- dup2(pipe1[1], STDOUT_FILENO);
- /* grep process */
- execl(GREP_EXEC, "grep", "-cr", "Thread", "nachos/", (char *) 0);
- return 0;
- }
- pid_2 = fork();
- if (pid_2 == 0)
- {
- close(pipe1[1]);
- close(pipe2[0]);
- dup2(pipe1[0], STDIN_FILENO);
- dup2(pipe2[1], STDOUT_FILENO);
- /* sort process */
- execl(SORT_EXEC, "sort", "-t", ":", "+1.0", "-2.0", "--numeric", "--reverse", (char *) 0);
- return 0;
- }
- pid_3 = fork();
- if (pid_3 == 0)
- {
- close(pipe1[0]);
- close(pipe1[1]);
- close(pipe2[1]);
- dup2(pipe2[0], STDIN_FILENO);
- /* head process */
- execl(HEAD_EXEC, "head", "--lines=5", (char *) 0);
- return 0;
- }
- /* shell process */
- /* Remember to close unused pipes! */
- close(pipe1[0]);
- close(pipe1[1]);
- close(pipe2[0]);
- close(pipe2[1]);
- close(STDIN_FILENO);
- close(STDOUT_FILENO);
- if ((waitpid(pid_1, &status, 0)) == -1)
- {
- fprintf(stderr, "Process 1 encountered an error. ERROR%d", errno);
- return EXIT_FAILURE;
- }
- if ((waitpid(pid_2, &status, 0)) == -1)
- {
- fprintf(stderr, "Process 2 encountered an error. ERROR%d", errno);
- return EXIT_FAILURE;
- }
- if ((waitpid(pid_3, &status, 0)) == -1)
- {
- fprintf(stderr, "Process 3 encountered an error. ERROR%d", errno);
- return EXIT_FAILURE;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement