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

How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229