Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

uses pipes and fork() call to implement the shell pipe |. The code is following: //This program executes ls -ltr | grep 33340, by dividing

uses pipes and fork() call to implement the shell pipe |. The code is following:

//This program executes "ls -ltr | grep 33340", by dividing the two command among the child and parent process

#include

#include

#include

#include

#include

#include

int main(int argc, char **argv) {

int status;

int childpid;

char *cat_args[] = {"ls", "-ltr", NULL};

char *grep_args[] = {"grep", "3340", NULL};

// create one pipe to send the output of "ls" process to "grep" process

int pipes[2]; pipe(pipes);

// fork the first child (to execute cat)

if((childpid = fork()) == -1){

perror("Error creating a child process");

exit(1);

}

if (childpid == 0) {

// replace cat's stdout with write part of 1st pipe

dup2(pipes[1], 1);

// close all pipes (very important!); end we're using was safely copied

close(pipes[0]);

close(pipes[1]);

execvp(*cat_args, cat_args);

exit(0);

}

else {

// replace grep's stdin with read end of 1st pipe

dup2(pipes[0], 0);

close(pipes[0]);

close(pipes[1]);

execvp(*grep_args, grep_args);

}

return(0);

}

Change code to execute the following double pipe command: ls -ltr | grep 3376 | wc l

1- Use one parent and two children to do the work. Call the file TwoPipesTwoChildren.cpp.

2- Write another version where the parent create 3 children, and the children will execute the commands (parent will do nothing, just lay in the sofa ! ). Call the file TwoPipesThreeChildren.cpp

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

More Books

Students also viewed these Databases questions

Question

Give examples of stress, eustress, and distress.

Answered: 1 week ago

Question

Explain strong and weak atoms with examples.

Answered: 1 week ago

Question

Explain the alkaline nature of aqueous solution of making soda.

Answered: 1 week ago

Question

Comment on the pH value of lattice solutions of salts.

Answered: 1 week ago