C:被dup2()卡住了:-(
我准备了一个程序,它使用管道模拟shell(cmd)接口。该程序有两个版本: 1.使用一个管道(使用从父到子通信的管道) 2.使用双管(使用从父到子和从子到父的两个管道进行通信) 因此,第一个程序提供了所需的接口并按照我的要求工作,但我无法在第二个程序中获得相同的结果(接口)(使用dup2()和类似的方法) 所以,我依靠你的帮助,把这两个代码放在下面 注意:您可以使用以下命令以相同的方式编译和尝试两个程序: $gcc prog1.c-o prog1 接下来,让我们运行: $。/prog1 接下来,让我们运行new terminal并尝试将一些数据写入input.txt: $echo pwd>input.txt 然后在第一个终端观看结果 (对于第一个程序来说,这很好,但是我需要在第二个程序中使用相同的接口来实现这一点) 第一个程序的代码(工作正常):C:被dup2()卡住了:-(,c,dup2,C,Dup2,我准备了一个程序,它使用管道模拟shell(cmd)接口。该程序有两个版本: 1.使用一个管道(使用从父到子通信的管道) 2.使用双管(使用从父到子和从子到父的两个管道进行通信) 因此,第一个程序提供了所需的接口并按照我的要求工作,但我无法在第二个程序中获得相同的结果(接口)(使用dup2()和类似的方法) 所以,我依靠你的帮助,把这两个代码放在下面 注意:您可以使用以下命令以相同的方式编译和尝试两个程序: $gcc prog1.c-o prog1 接下来,让我们运行: $。/prog1 接下来
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
void do_child(int data_pipe[]) {
int c;
int rc;
close(data_pipe[1]);
dup2(data_pipe[0], 0); /* This string provides the desired interface of the program */
char* cmd[] = { "bash", (char *)0 };
execvp("bash", cmd);
while ((rc = read(data_pipe[0], &c, 1)) > 0)
{
putchar(c);
}
exit(0);
}
void do_parent(int data_pipe[])
{
int c;
int rc;
FILE *in;
close(data_pipe[0]);
while (1)
{
in = fopen("input.txt", "r");
while ((c = fgetc(in)) > 0)
{
rc = write(data_pipe[1], &c, 1);
if (rc == -1)
{
perror("Parent: write");
close(data_pipe[1]);
exit(1);
}
}
fclose(in);
}
close(data_pipe[1]);
exit(0);
}
int main(int argc, char* argv[])
{
int data_pipe[2];
int pid;
int rc;
umask(0);
mknod("input.txt", S_IFIFO|0666, 0);
rc = pipe(data_pipe);
if (rc == -1)
{
perror("pipe");
exit(1);
}
pid = fork();
switch (pid)
{
case -1:
perror("fork");
exit(1);
case 0:
do_child(data_pipe);
default:
do_parent(data_pipe);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
/* Original version got from http://www.iakovlev.org */
int parent_to_child[2];
int child_to_parent[2];
void do_parent()
{
int c;
char ch;
int rc;
FILE *in;
close(child_to_parent[1]); /* we don't need to write to this pipe. */
close(parent_to_child[0]); /* we don't need to read from this pipe. */
while (1)
{
in = fopen("input.txt", "r");
while ((c = fgetc(in)) > 0) {
ch = (char)c;
/* write to child */
rc = write(parent_to_child[1], &ch, 1);
if (rc == -1) {
perror("child: write");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
/* read back from child */
rc = read(child_to_parent[0], &ch, 1);
c = (int)ch;
if (rc <= 0) {
perror("parent: read");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
putchar(c);
}
fclose(in);
}
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(0);
}
void do_child()
{
int c;
char ch;
int rc;
close(parent_to_child[1]); /* we don't need to write to this pipe. */
close(child_to_parent[0]); /* we don't need to read from this pipe. */
//dup2(parent_to_child[0], STDIN_FILENO);
//dup2(child_to_parent[1], STDOUT_FILENO);
/* Some dup2() routines must be added here
to get this working as the first program above */
char* cmd[] = { "bash", (char *)0 };
execvp("bash", cmd);
while (read(parent_to_child[0], &ch, 1) > 0) {
c = (int)ch;
ch = (char)c;
putchar(ch);
rc = write(child_to_parent[1], &ch, 1);
if (rc == -1) {
perror("child: write");
close(parent_to_child[0]);
close(child_to_parent[1]);
exit(1);
}
}
close(parent_to_child[0]);
close(child_to_parent[1]);
exit(0);
}
int main(int argc, char* argv[])
{
int pid;
int rc;
umask(0);
mknod("input.txt", S_IFIFO|0666, 0);
rc = pipe(parent_to_child);
if (rc == -1) {
perror("main: pipe parent_to_child");
exit(1);
}
rc = pipe(child_to_parent);
if (rc == -1) {
perror("main: pipe child_to_parent");
exit(1);
}
pid = fork();
switch (pid) {
case -1:
perror("main: fork");
exit(1);
case 0:
do_child();
default:
do_parent();
}
return 0;
}
#包括
#包括
#包括
#包括
void do_子对象(int data_管道[]){
INTC;
int rc;
关闭(数据管道[1]);
dup2(数据管道[0],0);/*此字符串提供程序所需的接口*/
char*cmd[]={bash',(char*)0};
execvp(“bash”,cmd);
而((rc=read(data_pipe[0],&c,1))>0)
{
普查尔(c);
}
出口(0);
}
void do_父对象(整数数据_管道[])
{
INTC;
int rc;
文件*in;
关闭(数据管道[0]);
而(1)
{
in=fopen(“input.txt”,“r”);
而((c=fgetc(in))>0)
{
rc=写入(数据管道[1],&c,1);
如果(rc==-1)
{
perror(“家长:写信”);
关闭(数据管道[1]);
出口(1);
}
}
fclose(in);
}
关闭(数据管道[1]);
出口(0);
}
int main(int argc,char*argv[])
{
int数据_管道[2];
int-pid;
int rc;
乌马斯克(0);
mknod(“input.txt”,S|u IFIFFO | 0666,0);
rc=管道(数据管道);
如果(rc==-1)
{
佩罗(“管道”);
出口(1);
}
pid=fork();
开关(pid)
{
案例1:
佩罗尔(“福克”);
出口(1);
案例0:
do_child(数据管道);
违约:
do_父项(数据管道);
}
返回0;
}
第二个程序的代码(需要稍微更正):
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
void do_child(int data_pipe[]) {
int c;
int rc;
close(data_pipe[1]);
dup2(data_pipe[0], 0); /* This string provides the desired interface of the program */
char* cmd[] = { "bash", (char *)0 };
execvp("bash", cmd);
while ((rc = read(data_pipe[0], &c, 1)) > 0)
{
putchar(c);
}
exit(0);
}
void do_parent(int data_pipe[])
{
int c;
int rc;
FILE *in;
close(data_pipe[0]);
while (1)
{
in = fopen("input.txt", "r");
while ((c = fgetc(in)) > 0)
{
rc = write(data_pipe[1], &c, 1);
if (rc == -1)
{
perror("Parent: write");
close(data_pipe[1]);
exit(1);
}
}
fclose(in);
}
close(data_pipe[1]);
exit(0);
}
int main(int argc, char* argv[])
{
int data_pipe[2];
int pid;
int rc;
umask(0);
mknod("input.txt", S_IFIFO|0666, 0);
rc = pipe(data_pipe);
if (rc == -1)
{
perror("pipe");
exit(1);
}
pid = fork();
switch (pid)
{
case -1:
perror("fork");
exit(1);
case 0:
do_child(data_pipe);
default:
do_parent(data_pipe);
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
/* Original version got from http://www.iakovlev.org */
int parent_to_child[2];
int child_to_parent[2];
void do_parent()
{
int c;
char ch;
int rc;
FILE *in;
close(child_to_parent[1]); /* we don't need to write to this pipe. */
close(parent_to_child[0]); /* we don't need to read from this pipe. */
while (1)
{
in = fopen("input.txt", "r");
while ((c = fgetc(in)) > 0) {
ch = (char)c;
/* write to child */
rc = write(parent_to_child[1], &ch, 1);
if (rc == -1) {
perror("child: write");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
/* read back from child */
rc = read(child_to_parent[0], &ch, 1);
c = (int)ch;
if (rc <= 0) {
perror("parent: read");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
putchar(c);
}
fclose(in);
}
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(0);
}
void do_child()
{
int c;
char ch;
int rc;
close(parent_to_child[1]); /* we don't need to write to this pipe. */
close(child_to_parent[0]); /* we don't need to read from this pipe. */
//dup2(parent_to_child[0], STDIN_FILENO);
//dup2(child_to_parent[1], STDOUT_FILENO);
/* Some dup2() routines must be added here
to get this working as the first program above */
char* cmd[] = { "bash", (char *)0 };
execvp("bash", cmd);
while (read(parent_to_child[0], &ch, 1) > 0) {
c = (int)ch;
ch = (char)c;
putchar(ch);
rc = write(child_to_parent[1], &ch, 1);
if (rc == -1) {
perror("child: write");
close(parent_to_child[0]);
close(child_to_parent[1]);
exit(1);
}
}
close(parent_to_child[0]);
close(child_to_parent[1]);
exit(0);
}
int main(int argc, char* argv[])
{
int pid;
int rc;
umask(0);
mknod("input.txt", S_IFIFO|0666, 0);
rc = pipe(parent_to_child);
if (rc == -1) {
perror("main: pipe parent_to_child");
exit(1);
}
rc = pipe(child_to_parent);
if (rc == -1) {
perror("main: pipe child_to_parent");
exit(1);
}
pid = fork();
switch (pid) {
case -1:
perror("main: fork");
exit(1);
case 0:
do_child();
default:
do_parent();
}
return 0;
}
#包括
#包括
#包括
#包括
/*原始版本来自http://www.iakovlev.org */
int parent_to_child[2];
int child_to_parent[2];
void do_parent()
{
INTC;
char ch;
int rc;
文件*in;
close(child_to_parent[1]);/*我们不需要写入此管道*/
close(parent_to_child[0]);/*我们不需要从此管道读取数据*/
而(1)
{
in=fopen(“input.txt”,“r”);
而((c=fgetc(in))>0){
ch=(char)c;
/*给孩子写信*/
rc=写入(父对象到子对象[1],&ch,1);
如果(rc==-1){
佩罗(“孩子:写”);
关闭(子对象到父对象[0]);
关闭(父对象到子对象[1]);
出口(1);
}
/*从孩子那里读回*/
rc=读取(从子项到父项[0],&ch,1);
c=(int)ch;
if(rc 0){
c=(int)ch;
ch=(char)c;
putchar(ch);
rc=写入(子对象到父对象[1],&ch,1);
如果(rc==-1){
佩罗(“孩子:写”);
关闭(父对象到子对象[0]);
关闭(子项到父项[1]);
出口(1);
}
}
关闭(父对象到子对象[0]);
关闭(子项到父项[1]);
出口(0);
}
int main(int argc,char*argv[])
{
int-pid;
int rc;
乌马斯克(0);
mknod(“input.txt”,S|u IFIFFO | 0666,0);
rc=管道(父管道到子管道);
如果(rc==-1){
perror(“主管道:从父管道到子管道”);
出口(1);
}
rc=管道(子管道到父管道);
如果(rc==-1){
perror(“主管道:从子管道到父管道”);
出口(1);
}
pid=fork();
开关(pid){
案例1:
佩罗尔(“主要:福克”);
出口(1);
案例0:
你有孩子吗;
违约:
做父母();
}
返回0;
}
主要区别在于:
while ((c = fgetc(in)) > 0) {
ch = (char)c;
/* write to child */
rc = write(parent_to_child[1], &ch, 1);
/* .... */
/* read back from child */
rc = read(child_to_parent[0], &ch, 1);
/* .... */
putchar(c);
}
由于我懒得为您编译/测试,我只想推测父进程在read()中被阻塞。因为另一方(bash在子进程中)不能保证回显每个写入的字符。或者它甚至可能决定打印多个您的代码无法处理的字符
在这种情况下,您必须轮询()以查看是否有要读取的内容。或者使用fcntl(F_SETFL)在child_to_parent[0]上设置O_NONBLOCK标志,当errno==EAGAIN时,只需跳过read()分支。并在仍有要读取的字符时循环
Edit1.顺便说一句,我完全错过了这一部分:您在dou parent()循环中必须在
子对象到父对象[0]
和中同时使用poll(),因为另一方可能会写一些东西(read()不会阻塞),即使您不向其写入()任何字符。多亏了您,我才使它开始工作
因此,以下是do_家长的更新代码:
void do_parent()
{
int c;
char ch;
int rc;
FILE *in;
struct pollfd fds[2];
int pol_ret;
fds[0].fd = child_to_parent[0];
close(child_to_parent[1]); /* we don't need to write to this pipe. */
close(parent_to_child[0]); /* we don't need to read from this pipe. */
while (1)
{
in = fopen("input.txt", "r");
fds[1].fd = fileno(in);
pol_ret = poll(fds, 2, 500);
while ((c = fgetc(in)) > 0) {
ch = (char)c;
/* write to child */
rc = write(parent_to_child[1], &ch, 1);
if (rc == -1) {
perror("child: write");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
/* read back from child */
if (fds[0].revents & POLLIN)
{
rc = read(child_to_parent[0], &ch, 1);
c = (int)ch;
if (rc <= 0) {
perror("parent: read");
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(1);
}
putchar(c);
}
}
fclose(in);
}
close(child_to_parent[0]);
close(parent_to_child[1]);
exit(0);
}
我想知道你为什么特别把它命名为“ANSI C”。在标准的ANSI C库中没有像dup2
这样的函数。不幸的是,问题部分解决了。这种方法在Windows中不起作用(通过Cygwin编译)。如果cygwin1.dll与中的程序存在于同一目录中,则结果与以前相同。在Cygwin shell中,一切正常。