Question: Modify Program 5.2 so that three commands, last , sort , and more can be passed to the program from command line. Each command passed

Modify Program 5.2 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. Recommend to use execvp() when running last, sort and more. When use this new program a three-command sequence such as:

%last | sort | more would be indicated as:

% ./a.out last sort more

when running your program.

Program 5.2 is as follows:

/*

*A last | sort command pipeline

*/

#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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!