I am trying to implement a small C program which will execute similarly to linux shell command $ sort < Names.txt | uniq | wc
I am trying to implement a small C program which will execute similarly to linux shell command
"$ sort < Names.txt | uniq | wc - 1"
the resulting program will sort the arbitrary list of names and removes the duplicates. it sorts the list because it needs duplicate lines adjacent for them to be removed. below is the pseudocode
/* include any needed header files here */
int main(int argc, char *arv[]) {
create first pipe fd1
pid = fork();
// create first child for sort
if (pid < 0) { // fork error }
if (pid == 0) {
// first child process, run sort
// tie write end of pipe fd1 to standard output (file descriptor 1)
// close read end of pipe fd1
// start the sort command using execlp
// should not get here
}
create second pipe fd2
pid = fork();
// create second child for uniq
if (pid < 0) {
// fork error
}
if (pid == 0) {
// second child process, run uniq
// tie read end of fd1 to standard input (file descriptor 0)
// tie write end of fd2 to standard output (file descriptor 1)
// close write end of pipe fd1
// close read end of pipe fd2
// start the uniq command using execlp
// should not get here
}
pid = fork()
// create third child for wc -l
if (pid < 0) { // fork error }
if (pid == 0) {
// third child process, run wc -l
// tie read end of fd2 to standard input (file descriptor 0)
// close write end of pipe fd2
// close read end of pipe fd1
// close write end of pipe fd1
// start the wc -l command using execlp
// should not get here
}
// parent process code
// close both ends of pipes fd1 and fd2
// wait for third process to end
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started