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;
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;
c
marked as duplicate by Charles Duffy
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.
|
show 1 more comment
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;
c
marked as duplicate by Charles Duffy
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
stdoutis aFILE *. The first argument todup2should 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
|
show 1 more comment
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;
c
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
c
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
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
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
stdoutis aFILE *. The first argument todup2should 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
|
show 1 more comment
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
stdoutis aFILE *. The first argument todup2should 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
|
show 1 more comment
1 Answer
1
active
oldest
votes
So you have 6obvious mistakes.
- Indent your code.
- Only call dup2 in the child processes.
- 1 not stdout, 0 not stdin.
- If execvp fails, call _exit.
- wait(NULL) is before the second fork not after
- 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.
1
Also, thedup2arguments are switched
– that other guy
Mar 9 at 1:29
1
Also hisarris the wrong type.
– Shawn
Mar 9 at 1:38
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
So you have 6obvious mistakes.
- Indent your code.
- Only call dup2 in the child processes.
- 1 not stdout, 0 not stdin.
- If execvp fails, call _exit.
- wait(NULL) is before the second fork not after
- 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.
1
Also, thedup2arguments are switched
– that other guy
Mar 9 at 1:29
1
Also hisarris the wrong type.
– Shawn
Mar 9 at 1:38
add a comment |
So you have 6obvious mistakes.
- Indent your code.
- Only call dup2 in the child processes.
- 1 not stdout, 0 not stdin.
- If execvp fails, call _exit.
- wait(NULL) is before the second fork not after
- 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.
1
Also, thedup2arguments are switched
– that other guy
Mar 9 at 1:29
1
Also hisarris the wrong type.
– Shawn
Mar 9 at 1:38
add a comment |
So you have 6obvious mistakes.
- Indent your code.
- Only call dup2 in the child processes.
- 1 not stdout, 0 not stdin.
- If execvp fails, call _exit.
- wait(NULL) is before the second fork not after
- 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.
So you have 6obvious mistakes.
- Indent your code.
- Only call dup2 in the child processes.
- 1 not stdout, 0 not stdin.
- If execvp fails, call _exit.
- wait(NULL) is before the second fork not after
- 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.
edited Mar 9 at 1:32
answered Mar 9 at 1:26
JoshuaJoshua
24.3k550104
24.3k550104
1
Also, thedup2arguments are switched
– that other guy
Mar 9 at 1:29
1
Also hisarris the wrong type.
– Shawn
Mar 9 at 1:38
add a comment |
1
Also, thedup2arguments are switched
– that other guy
Mar 9 at 1:29
1
Also hisarris 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
add a comment |
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
stdoutis aFILE *. The first argument todup2should 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