Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <fcntl.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <inttypes.h>
- #include <sched.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <stdbool.h>
- int main(int argc, char* argv[]) {
- int cmd_num = 1;
- int in_pair[2];
- int out_pair[2];
- pipe(in_pair);
- pipe(out_pair);
- while (cmd_num != argc) {
- pid_t pid = fork();
- if (pid == 0) {
- if (cmd_num != 1) {
- dup2(in_pair[0], 0);
- close(in_pair[0]);
- close(in_pair[1]);
- }
- if (cmd_num != argc - 1) {
- dup2(out_pair[1], 1);
- close(out_pair[1]);
- }
- execlp(argv[cmd_num], argv[cmd_num], NULL);
- perror("failed to execlp");
- exit(1);
- } else {
- close(in_pair[0]);
- close(in_pair[1]);
- int child_state;
- waitpid(pid, &child_state, 0);
- if (cmd_num == argc - 1) {
- return 0;
- }
- in_pair[0] = out_pair[0];
- in_pair[1] = out_pair[1];
- pipe(out_pair);
- ++cmd_num;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement