How to use dup2() and pipe() to implement “ls | wc” in my shell? [duplicate]Classic C. Using pipes in execvp function, stdin and stdout redirectionHow do you set, clear, and toggle a single bit?pipe not being read by subprocess?Pipe with printf commanddup2 - Creating a piping programexecvp() never finishes on pipeUnix - Pipe, forks, execlp, dup2, c programPipes and Redirection, small program to test understandingC - dup2() not executingdup2 for Input File Redirection Not WorkingPiping/dup2() not working properly (Implementing Unix Shell in C)

Do infinite dimensional systems make sense?

Are the number of citations and number of published articles the most important criteria for a tenure promotion?

What typically incentivizes a professor to change jobs to a lower ranking university?

Does an object always see its latest internal state irrespective of thread?

Is it legal for company to use my work email to pretend I still work there?

Approximately how much travel time was saved by the opening of the Suez Canal in 1869?

How do I deal with an unproductive colleague in a small company?

Can you really stack all of this on an Opportunity Attack?

"You are your self first supporter", a more proper way to say it

Why are electrically insulating heatsinks so rare? Is it just cost?

Can I ask the recruiters in my resume to put the reason why I am rejected?

What does the "remote control" for a QF-4 look like?

How to format long polynomial?

Modeling an IP Address

How is it possible to have an ability score that is less than 3?

What would happen to a modern skyscraper if it rains micro blackholes?

DC-DC converter from low voltage at high current, to high voltage at low current

A newer friend of my brother's gave him a load of baseball cards that are supposedly extremely valuable. Is this a scam?

How does quantile regression compare to logistic regression with the variable split at the quantile?

Is it possible to do 50 km distance without any previous training?

Accidentally leaked the solution to an assignment, what to do now? (I'm the prof)

What does "Puller Prush Person" mean?

How much of data wrangling is a data scientist's job?

What is a clear way to write a bar that has an extra beat?



How to use dup2() and pipe() to implement “ls | wc” in my shell? [duplicate]


Classic C. Using pipes in execvp function, stdin and stdout redirectionHow do you set, clear, and toggle a single bit?pipe not being read by subprocess?Pipe with printf commanddup2 - Creating a piping programexecvp() never finishes on pipeUnix - Pipe, forks, execlp, dup2, c programPipes and Redirection, small program to test understandingC - dup2() not executingdup2 for Input File Redirection Not WorkingPiping/dup2() not working properly (Implementing Unix Shell in C)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








-2
















This question already has an answer here:



  • Classic C. Using pipes in execvp function, stdin and stdout redirection

    2 answers



How would I use the dup2() and pipe() functions to commit a redirect of the output of ls into the input of wc. Therefore printing the word count.



here is my attempt, however this is not working.



 if(!strcmp(*ptr,"|")) //argv[] contains the input partitioned by spaces
//ignore the ptr for this case
char arr[2];
pipe(arr); // 0 is read 1 is write
dup2(stdout,arr[1]); // my redirected output

int a = fork();
if(a == 0)
close(arr[0]);
execvp(argv[0],argv);

wait(NULL);

dup2(0,arr[0]); // my redirected input

int b = fork();
if(b == 0)
close(arr[1]);
execvp(argv[2],argv);

wait(NULL);

close(arr[0]);
close(arr[1]);

return;











share|improve this question















marked as duplicate by Charles Duffy shell
Users with the  shell badge can single-handedly close shell questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 9 at 21:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 2





    We really should build a canonical instance for this -- someone asks a new version of this question every week at least.

    – Charles Duffy
    Mar 9 at 1:25







  • 1





    stdout is a FILE *. The first argument to dup2 should be an int.

    – William Pursell
    Mar 9 at 1:25











  • @CharlesDuffy: homework + each question unique issues each time

    – Joshua
    Mar 9 at 1:27











  • @CharlesDuffy I would definitely support a canonical instance for "how do I pipe two programs together in C"

    – that other guy
    Mar 9 at 1:33











  • @Joshua, ...not that unique. I'm pretty sure we've seen 80% of the ways people can screw it up by now. Even if we can tell people to fix the mistakes covered in the canonical instance and come back only with new/different bugs, that's a big improvement from the status quo.

    – Charles Duffy
    Mar 9 at 3:07


















-2
















This question already has an answer here:



  • Classic C. Using pipes in execvp function, stdin and stdout redirection

    2 answers



How would I use the dup2() and pipe() functions to commit a redirect of the output of ls into the input of wc. Therefore printing the word count.



here is my attempt, however this is not working.



 if(!strcmp(*ptr,"|")) //argv[] contains the input partitioned by spaces
//ignore the ptr for this case
char arr[2];
pipe(arr); // 0 is read 1 is write
dup2(stdout,arr[1]); // my redirected output

int a = fork();
if(a == 0)
close(arr[0]);
execvp(argv[0],argv);

wait(NULL);

dup2(0,arr[0]); // my redirected input

int b = fork();
if(b == 0)
close(arr[1]);
execvp(argv[2],argv);

wait(NULL);

close(arr[0]);
close(arr[1]);

return;











share|improve this question















marked as duplicate by Charles Duffy shell
Users with the  shell badge can single-handedly close shell questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 9 at 21:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.













  • 2





    We really should build a canonical instance for this -- someone asks a new version of this question every week at least.

    – Charles Duffy
    Mar 9 at 1:25







  • 1





    stdout is a FILE *. The first argument to dup2 should be an int.

    – William Pursell
    Mar 9 at 1:25











  • @CharlesDuffy: homework + each question unique issues each time

    – Joshua
    Mar 9 at 1:27











  • @CharlesDuffy I would definitely support a canonical instance for "how do I pipe two programs together in C"

    – that other guy
    Mar 9 at 1:33











  • @Joshua, ...not that unique. I'm pretty sure we've seen 80% of the ways people can screw it up by now. Even if we can tell people to fix the mistakes covered in the canonical instance and come back only with new/different bugs, that's a big improvement from the status quo.

    – Charles Duffy
    Mar 9 at 3:07














-2












-2








-2









This question already has an answer here:



  • Classic C. Using pipes in execvp function, stdin and stdout redirection

    2 answers



How would I use the dup2() and pipe() functions to commit a redirect of the output of ls into the input of wc. Therefore printing the word count.



here is my attempt, however this is not working.



 if(!strcmp(*ptr,"|")) //argv[] contains the input partitioned by spaces
//ignore the ptr for this case
char arr[2];
pipe(arr); // 0 is read 1 is write
dup2(stdout,arr[1]); // my redirected output

int a = fork();
if(a == 0)
close(arr[0]);
execvp(argv[0],argv);

wait(NULL);

dup2(0,arr[0]); // my redirected input

int b = fork();
if(b == 0)
close(arr[1]);
execvp(argv[2],argv);

wait(NULL);

close(arr[0]);
close(arr[1]);

return;











share|improve this question

















This question already has an answer here:



  • Classic C. Using pipes in execvp function, stdin and stdout redirection

    2 answers



How would I use the dup2() and pipe() functions to commit a redirect of the output of ls into the input of wc. Therefore printing the word count.



here is my attempt, however this is not working.



 if(!strcmp(*ptr,"|")) //argv[] contains the input partitioned by spaces
//ignore the ptr for this case
char arr[2];
pipe(arr); // 0 is read 1 is write
dup2(stdout,arr[1]); // my redirected output

int a = fork();
if(a == 0)
close(arr[0]);
execvp(argv[0],argv);

wait(NULL);

dup2(0,arr[0]); // my redirected input

int b = fork();
if(b == 0)
close(arr[1]);
execvp(argv[2],argv);

wait(NULL);

close(arr[0]);
close(arr[1]);

return;






This question already has an answer here:



  • Classic C. Using pipes in execvp function, stdin and stdout redirection

    2 answers







c






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 21 at 18:19









Joshua

24.3k550104




24.3k550104










asked Mar 9 at 0:58









pgh78pgh78

1




1




marked as duplicate by Charles Duffy shell
Users with the  shell badge can single-handedly close shell questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 9 at 21:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Charles Duffy shell
Users with the  shell badge can single-handedly close shell questions as duplicates and reopen them as needed.

StackExchange.ready(function()
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();

);
);
);
Mar 9 at 21:19


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









  • 2





    We really should build a canonical instance for this -- someone asks a new version of this question every week at least.

    – Charles Duffy
    Mar 9 at 1:25







  • 1





    stdout is a FILE *. The first argument to dup2 should be an int.

    – William Pursell
    Mar 9 at 1:25











  • @CharlesDuffy: homework + each question unique issues each time

    – Joshua
    Mar 9 at 1:27











  • @CharlesDuffy I would definitely support a canonical instance for "how do I pipe two programs together in C"

    – that other guy
    Mar 9 at 1:33











  • @Joshua, ...not that unique. I'm pretty sure we've seen 80% of the ways people can screw it up by now. Even if we can tell people to fix the mistakes covered in the canonical instance and come back only with new/different bugs, that's a big improvement from the status quo.

    – Charles Duffy
    Mar 9 at 3:07













  • 2





    We really should build a canonical instance for this -- someone asks a new version of this question every week at least.

    – Charles Duffy
    Mar 9 at 1:25







  • 1





    stdout is a FILE *. The first argument to dup2 should be an int.

    – William Pursell
    Mar 9 at 1:25











  • @CharlesDuffy: homework + each question unique issues each time

    – Joshua
    Mar 9 at 1:27











  • @CharlesDuffy I would definitely support a canonical instance for "how do I pipe two programs together in C"

    – that other guy
    Mar 9 at 1:33











  • @Joshua, ...not that unique. I'm pretty sure we've seen 80% of the ways people can screw it up by now. Even if we can tell people to fix the mistakes covered in the canonical instance and come back only with new/different bugs, that's a big improvement from the status quo.

    – Charles Duffy
    Mar 9 at 3:07








2




2





We really should build a canonical instance for this -- someone asks a new version of this question every week at least.

– Charles Duffy
Mar 9 at 1:25






We really should build a canonical instance for this -- someone asks a new version of this question every week at least.

– Charles Duffy
Mar 9 at 1:25





1




1





stdout is a FILE *. The first argument to dup2 should be an int.

– William Pursell
Mar 9 at 1:25





stdout is a FILE *. The first argument to dup2 should be an int.

– William Pursell
Mar 9 at 1:25













@CharlesDuffy: homework + each question unique issues each time

– Joshua
Mar 9 at 1:27





@CharlesDuffy: homework + each question unique issues each time

– Joshua
Mar 9 at 1:27













@CharlesDuffy I would definitely support a canonical instance for "how do I pipe two programs together in C"

– that other guy
Mar 9 at 1:33





@CharlesDuffy I would definitely support a canonical instance for "how do I pipe two programs together in C"

– that other guy
Mar 9 at 1:33













@Joshua, ...not that unique. I'm pretty sure we've seen 80% of the ways people can screw it up by now. Even if we can tell people to fix the mistakes covered in the canonical instance and come back only with new/different bugs, that's a big improvement from the status quo.

– Charles Duffy
Mar 9 at 3:07






@Joshua, ...not that unique. I'm pretty sure we've seen 80% of the ways people can screw it up by now. Even if we can tell people to fix the mistakes covered in the canonical instance and come back only with new/different bugs, that's a big improvement from the status quo.

– Charles Duffy
Mar 9 at 3:07













1 Answer
1






active

oldest

votes


















0














So you have 6obvious mistakes.



  1. Indent your code.

  2. Only call dup2 in the child processes.

  3. 1 not stdout, 0 not stdin.

  4. If execvp fails, call _exit.

  5. wait(NULL) is before the second fork not after

  6. arguments to dup2 are swapped

Because this is homework I'm not going to just give you the answer. Get this into shape and ping me and I'll see what more I can find.






share|improve this answer




















  • 1





    Also, the dup2 arguments are switched

    – that other guy
    Mar 9 at 1:29






  • 1





    Also his arr is the wrong type.

    – Shawn
    Mar 9 at 1:38

















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














So you have 6obvious mistakes.



  1. Indent your code.

  2. Only call dup2 in the child processes.

  3. 1 not stdout, 0 not stdin.

  4. If execvp fails, call _exit.

  5. wait(NULL) is before the second fork not after

  6. arguments to dup2 are swapped

Because this is homework I'm not going to just give you the answer. Get this into shape and ping me and I'll see what more I can find.






share|improve this answer




















  • 1





    Also, the dup2 arguments are switched

    – that other guy
    Mar 9 at 1:29






  • 1





    Also his arr is the wrong type.

    – Shawn
    Mar 9 at 1:38















0














So you have 6obvious mistakes.



  1. Indent your code.

  2. Only call dup2 in the child processes.

  3. 1 not stdout, 0 not stdin.

  4. If execvp fails, call _exit.

  5. wait(NULL) is before the second fork not after

  6. arguments to dup2 are swapped

Because this is homework I'm not going to just give you the answer. Get this into shape and ping me and I'll see what more I can find.






share|improve this answer




















  • 1





    Also, the dup2 arguments are switched

    – that other guy
    Mar 9 at 1:29






  • 1





    Also his arr is the wrong type.

    – Shawn
    Mar 9 at 1:38













0












0








0







So you have 6obvious mistakes.



  1. Indent your code.

  2. Only call dup2 in the child processes.

  3. 1 not stdout, 0 not stdin.

  4. If execvp fails, call _exit.

  5. wait(NULL) is before the second fork not after

  6. arguments to dup2 are swapped

Because this is homework I'm not going to just give you the answer. Get this into shape and ping me and I'll see what more I can find.






share|improve this answer















So you have 6obvious mistakes.



  1. Indent your code.

  2. Only call dup2 in the child processes.

  3. 1 not stdout, 0 not stdin.

  4. If execvp fails, call _exit.

  5. wait(NULL) is before the second fork not after

  6. arguments to dup2 are swapped

Because this is homework I'm not going to just give you the answer. Get this into shape and ping me and I'll see what more I can find.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 9 at 1:32

























answered Mar 9 at 1:26









JoshuaJoshua

24.3k550104




24.3k550104







  • 1





    Also, the dup2 arguments are switched

    – that other guy
    Mar 9 at 1:29






  • 1





    Also his arr is the wrong type.

    – Shawn
    Mar 9 at 1:38












  • 1





    Also, the dup2 arguments are switched

    – that other guy
    Mar 9 at 1:29






  • 1





    Also his arr is the wrong type.

    – Shawn
    Mar 9 at 1:38







1




1





Also, the dup2 arguments are switched

– that other guy
Mar 9 at 1:29





Also, the dup2 arguments are switched

– that other guy
Mar 9 at 1:29




1




1





Also his arr is the wrong type.

– Shawn
Mar 9 at 1:38





Also his arr is the wrong type.

– Shawn
Mar 9 at 1:38





Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page