Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C dup和dup2命令_C_Operating System_Dup2_Dup - Fatal编程技术网

C dup和dup2命令

C dup和dup2命令,c,operating-system,dup2,dup,C,Operating System,Dup2,Dup,我要做的是将ls命令的输出放在一个文件中,然后使用grep命令读取该文件并将其存储在一个新文件中,然后根据该文件的内容在终端上打印一些内容 因此,存在以下输出重定向: 1) 从标准输出到名为oi.txt的文件(用于ls命令) 2) 从oi.txt到grep.txt(对于grep命令) 3) 从grep.txt返回到终端 这是我的代码: #include<stdio.h> #include<sys/wait.h> #include<unistd.h> #incl

我要做的是将ls命令的输出放在一个文件中,然后使用grep命令读取该文件并将其存储在一个新文件中,然后根据该文件的内容在终端上打印一些内容

因此,存在以下输出重定向:

1) 从标准输出到名为oi.txt的文件(用于ls命令)
2) 从oi.txt到grep.txt(对于grep命令)
3) 从grep.txt返回到终端

这是我的代码:

#include<stdio.h>
#include<sys/wait.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
        //if(!fork())
                //execl("/bin/grep","grep","input","oioioi.txt",0);
        FILE *fp1,*fp2;
        int fd,fd2;
        fp1=fopen("oioioi.txt","w+");
        fp2=fopen("grep.txt","w+");
        fd=fileno(fp1);
        fd2=fileno(fp2);
        char filename[40],id[40],marks[40],filename2[40];
        scanf("%s %s %s",filename,id,marks);
        char *execarg2[]={"ls",0};
        dup2(fd,1); //1st redirection
        //close(1);
        if(!fork())
        {
                execv("/bin/ls",execarg2);
        }
        sleep(1);
        wait(NULL);
        //dup(fd2);
        close(fd);
        dup(1);
        dup2(fd2,1); //2nd redirection
        if(!fork())
        {
                //sleep(1);
                execl("/bin/grep","grep",filename,"oioioi.txt",0);
        }
        sleep(1);
        wait(NULL);
        close(fd2);
        dup2(1,fd2); //3rd one which is the incorrect one
        fscanf(fp2,"%s",filename2);
                printf("%s",filename2);
        if(strcmp(filename2,filename)==0)
                printf("FILE FOUND");

        return(0);
}
#包括
#包括
#包括
#包括
#包括
#包括
内部主(空)
{
//如果(!fork())
//execl(“/bin/grep”,“grep”,“input”,“oi.txt”,0);
文件*fp1,*fp2;
int-fd,fd2;
fp1=fopen(“oi.txt”,“w+”);
fp2=fopen(“grep.txt”,“w+”);
fd=文件编号(fp1);
fd2=文件号(fp2);
char filename[40],id[40],marks[40],filename2[40];
scanf(“%s%s%s”,文件名、id、标记);
char*execarg2[]={“ls”,0};
dup2(fd,1);//第一次重定向
//关闭(1);
如果(!fork())
{
execv(“/bin/ls”,execarg2);
}
睡眠(1);
等待(空);
//dup(fd2);
关闭(fd);
dup(1);
dup2(fd2,1);//第二次重定向
如果(!fork())
{
//睡眠(1);
execl(“/bin/grep”,“grep”,文件名,“oi.txt”,0);
}
睡眠(1);
等待(空);
关闭(fd2);
dup2(1,fd2);//第三个是不正确的一个
fscanf(fp2,“%s”,文件名2);
printf(“%s”,filename2);
if(strcmp(filename2,filename)==0)
printf(“找到文件”);
返回(0);
}

我认为第三个是不正确的。然而,我也不确定前两个。如果你们能看看我的代码,告诉我哪里出了问题,或者给我一个在以下重定向中使用dup的例子,我真的非常感激问题从您的第一次dup调用开始:
dup(fd,1)。在该行中,标准输出将丢失到终端。如果你想秘密地保留对它的引用,你应该
dup(STDOUT\u FILENO)事先

因此,对
printf
的后续调用实际上将重定向到
fd2
,而不是
stdout
的原始fd(1)

这里可能还隐藏着一些其他问题,但这是我所能摸索到的(请参阅我对这个问题的评论)

n、 b

dup2
的主页说明,如有必要,它将关闭新的fd(第二个参数),使
关闭

close(fd2);
dup(1, fd2);

冗余。

如果变量名是(1)对称的(即fd1=fileno(fp1);fd2=fileno(fp2);和(2)为变量提供更有意义的名称,则会有所帮助。在
dup2
之前关闭目标fd不仅是冗余的,而且会在多线程代码中引入竞争条件。