使用子进程读取输入?
using a child process to read input?
我正在尝试使用子进程来计算控制台输入的字数(我在 UNIX 中键入的内容)。这是我的代码:
int main(){
int pipe1[2];
int child_value;
pipe(pipe1);
child_value= fork();
if(child_value > 0){
/*parent*/
int word_count;
dup2(STDIN_FILENO, pipe1[0]);
close(pipe1[0]);
scanf("%d", &word_count);
printf("%d\n", word_count);
} else if (child_value == 0) {
/*child*/
dup2(pipe1[1], STDOUT_FILENO);
close(pipe1[1]);
execl("/usr/bin/wc", "wc", "-w", NULL);
err(EX_OSERR, "exec error");
} else err(EX_OSERR, "fork error");
return 0;
}
无论我在控制台中输入什么,我的控制台上显示的输出始终为 0,而且我总是收到一条错误消息:
wc: standard input: Input/output error
如评论中所述:
When you use dup2() or dup() to map one end of a pipe to standard input or standard output, it is almost invariably correct to close both ends of the pipe afterwards. The exceptions are very few and far between; you will know when you need to avoid closing both ends of the pipe. However, that isn't the direct cause of your problem.
Compare: dup2(STDIN_FILENO, pipe1[0]);
and dup2(pipe1[1], STDOUT_FILENO);
. They should both list the standard file number as the second argument.
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <sysexits.h>
#include <unistd.h>
int main(void)
{
int pipe1[2];
int child_value;
pipe(pipe1);
child_value = fork();
if (child_value > 0)
{
/*parent*/
int word_count;
dup2(pipe1[0], STDIN_FILENO);
close(pipe1[0]);
close(pipe1[1]);
scanf("%d", &word_count);
printf("%d\n", word_count);
}
else if (child_value == 0)
{
/*child*/
dup2(pipe1[1], STDOUT_FILENO);
close(pipe1[1]);
close(pipe1[0]);
execl("/usr/bin/wc", "wc", "-w", NULL);
err(EX_OSERR, "exec error");
}
else
err(EX_OSERR, "fork error");
return 0;
}
示例输出(程序 xx19
):
$ ./xx19
So she went into the garden
to cut a cabbage-leaf
to make an apple-pie
and at the same time
a great she-bear coming down the street
pops its head into the shop
What no soap
So he died
and she very imprudently married the Barber
and there were present
the Picninnies
and the Joblillies
and the Garyulies
and the great Panjandrum himself
with the little round button at top
and they all fell to playing the game of catch-as-catch-can
till the gunpowder ran out at the heels of their boots
90
$
(您可以在 Google 上搜索 'Panjandrum' 以找出那些胡说八道的文章的出处。)
我正在尝试使用子进程来计算控制台输入的字数(我在 UNIX 中键入的内容)。这是我的代码:
int main(){
int pipe1[2];
int child_value;
pipe(pipe1);
child_value= fork();
if(child_value > 0){
/*parent*/
int word_count;
dup2(STDIN_FILENO, pipe1[0]);
close(pipe1[0]);
scanf("%d", &word_count);
printf("%d\n", word_count);
} else if (child_value == 0) {
/*child*/
dup2(pipe1[1], STDOUT_FILENO);
close(pipe1[1]);
execl("/usr/bin/wc", "wc", "-w", NULL);
err(EX_OSERR, "exec error");
} else err(EX_OSERR, "fork error");
return 0;
}
无论我在控制台中输入什么,我的控制台上显示的输出始终为 0,而且我总是收到一条错误消息:
wc: standard input: Input/output error
如评论中所述:
When you use dup2() or dup() to map one end of a pipe to standard input or standard output, it is almost invariably correct to close both ends of the pipe afterwards. The exceptions are very few and far between; you will know when you need to avoid closing both ends of the pipe. However, that isn't the direct cause of your problem.
Compare:
dup2(STDIN_FILENO, pipe1[0]);
anddup2(pipe1[1], STDOUT_FILENO);
. They should both list the standard file number as the second argument.
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <sysexits.h>
#include <unistd.h>
int main(void)
{
int pipe1[2];
int child_value;
pipe(pipe1);
child_value = fork();
if (child_value > 0)
{
/*parent*/
int word_count;
dup2(pipe1[0], STDIN_FILENO);
close(pipe1[0]);
close(pipe1[1]);
scanf("%d", &word_count);
printf("%d\n", word_count);
}
else if (child_value == 0)
{
/*child*/
dup2(pipe1[1], STDOUT_FILENO);
close(pipe1[1]);
close(pipe1[0]);
execl("/usr/bin/wc", "wc", "-w", NULL);
err(EX_OSERR, "exec error");
}
else
err(EX_OSERR, "fork error");
return 0;
}
示例输出(程序 xx19
):
$ ./xx19
So she went into the garden
to cut a cabbage-leaf
to make an apple-pie
and at the same time
a great she-bear coming down the street
pops its head into the shop
What no soap
So he died
and she very imprudently married the Barber
and there were present
the Picninnies
and the Joblillies
and the Garyulies
and the great Panjandrum himself
with the little round button at top
and they all fell to playing the game of catch-as-catch-can
till the gunpowder ran out at the heels of their boots
90
$
(您可以在 Google 上搜索 'Panjandrum' 以找出那些胡说八道的文章的出处。)