Question: Modify Program 5.2 in Lecture 5 so that three commands, last , sort , and more can be passed to the program from command line.
-
Modify Program 5.2 in Lecture 5 so that three commands, last, sort, and more can be passed to the program from command line. Each command passed to the program should be connected to the next command via a pipe. When use this new program a three-command sequence such as:
%last | sort | more would be indicated as:
% ./a.out last sort mor
when running your program.
Program 5.2
#include #include #include int main(void) { int f_des[2]; if (pipe(f_des) == -1) { perror(Pipe); exit(1); }
switch (fork()) { case -1: perror(Fork); exit(2); case 0: /*In the child process */ dup2(f_des[1], fileno(stdout)); close(f_des[0]); close(f_des[1]); execl(/usr/bin/last, last, (char *) 0); exit(3); default: dup2(f_des[0], fileno(stdin)); close(f_des[0]); close(f_des[1]); execl(/bin/sort, sort, (char *) 0); exit(4); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
