C 如何在不同程序之间使用管道?

C 如何在不同程序之间使用管道?,c,linux,C,Linux,再次询问问题并修改代码 我需要在linux中创建三个名为program0 program1和program2的程序 Program0:创建一个包含两个子进程的父进程,并执行程序1和程序2及其子进程,等待它们完成并关闭 程序1:从用户处获取文件名并将文本写入文件。当按下CTNL+D时,它完成写入并创建一个管道。之后,使用cat命令将文件写入标准输出,并使用dup()创建包含文件的管道 Program2:它在dup()的帮助下从管道中读取文件名,然后执行wc命令 到目前为止,我成功地创建了所有程序,

再次询问问题并修改代码

我需要在linux中创建三个名为program0 program1和program2的程序

Program0:创建一个包含两个子进程的父进程,并执行程序1和程序2及其子进程,等待它们完成并关闭

程序1:从用户处获取文件名并将文本写入文件。当按下CTNL+D时,它完成写入并创建一个管道。之后,使用cat命令将文件写入标准输出,并使用dup()创建包含文件的管道

Program2:它在dup()的帮助下从管道中读取文件名,然后执行wc命令

到目前为止,我成功地创建了所有程序,并且没有任何编译错误。程序0执行两个程序。程序1也在工作,并将文件发送到管道,但程序2无法从管道中读取它。它打印了奇怪的符号

当我试图从程序1中的管道中读取时,它会工作(请参阅程序1中的停用代码),但如果我将相同的代码放入程序2中,则该代码不工作

那么,我怎样才能让program2从管道中读取数据呢?在这之后,我将尝试在program2中执行wc命令,但首先我应该能够看到它从stdout获取文件输入,那么如何呢

我知道有点长,但请帮帮我,伙计们

程序0

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#define MAX 999

int main()
{
pid_t pid1, pid2;

pid1 = fork();
if(pid1<0) 
{
fprintf(stderr,"Fork basarisiz");
exit(-1);
}
else if (pid1 ==0)/*child prosesleri*/
{


printf("program1\n");

execlp("./program1","program1",NULL);
execlp("./program2","program2",NULL);
}
else /*parent procsesleri */
{
wait(NULL);
pid2 = fork();
if(pid2<0) 
{
fprintf(stderr,"Fork basarisiz");
exit(-1);
}
else if (pid2 ==0)/*child prosesleri*/
{
printf("\n");
printf("Program 2\n");
printf("\n");
execlp("./program2","program2",NULL);
//printf("\n");
}
else
{
}

/////////////////////////////////////////////////////////////////////////
wait(NULL);
printf("\n");
printf("Parent:Two child processes have successfully been created\n");
printf("Parent:Two child processes have successfully been terminated\n");
printf("Parent:This process will now terminate\n");
printf("\n");
exit(0);
}
}
#包括
#包括
#包括
#包括
#包括
#定义最大999
int main()
{
pid_t pid1,pid2;
pid1=fork();

如果(pid1您可能需要检查手册页中的execve(2)(用于启动
cat
)和dup2(2)(用于根据需要覆盖
stdin
stdout
execve
将用不同的程序(相同的PID,相同的文件描述符)覆盖当前正在执行的程序,而
dup2
将允许您重新定义任何标准文件描述符,以指向您提供给它的任何文件描述符(如管道的任何一端)。

但是启动和终止程序由program0控制,其工作正常,因此我不理解您对Exeve(2)的评论另外,
stdout
中重写stdout和std的是
放入
printf
等写入的标准输出流。
stdin
get'等的标准输入流,方法是
重写`您正在引导受影响进程的输入和/或输出的流。
execve
>:当您调用该函数时,当前进程中程序的所有执行都将停止,程序将被替换为某个对象,该对象的行为就像您在命令行上调用了传递给execve的调用一样——除了它继承了对您指定的
stdin
stdout
stderr
的任何重定向留在原处(和其他一些细节),所以据我所知,我将在我的程序0中使用execve,以便将标准输出从程序1继承到程序2,然后打印它?
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#define MAX 999

int main() 
{ 
    char c[10000];              
    char file[10000];
    int words;
    printf("Child1:A text file will be created\n");
    printf("Child1:Enter the name of the file\n");
    scanf("%123s",file);
    strcat(file,".txt"); 
    FILE * pf; 
    pf = fopen(file, "w" );

   if (!pf)
   fprintf( stderr, "I couldn't open the file.\n" );

   else
   {
        printf("Child1: Input a number of text lines ended, each ended by a CR (carriage return).\n");


/////////////////////////////

  do
{
  if (NULL != fgets(c, sizeof(c), stdin))
  {
    if (0 == strcmp(c, ".\n")) 
    {
      break;
    }

    fprintf(pf, "%s", c);
  }
  else
  {
    if (0 != ferror(stdin))
    {
      fprintf(stderr, "An error occured while reading from stdin\n");
    }
    else
    {
      printf("Child1: Finish the input by CNTL^D\n");
    }

    break;
  }
} while (1);

/////////////////////////////

    }
    printf("\nChild1:The file %s is succesfully created and saved in the current dictionary\n",file);


//////////////////////////////////////////////



/////////////////////////pipe///////////////

    fclose(pf);  // close file  

        char ch;
        int outcount = 0;
        int     fd[2], nbytes;
        pid_t   childpid;
int i;
        char f2[2];

        char    readbuffer[80];

        pipe(fd);

        if((childpid = fork()) == -1)
        {
                perror("fork");
                exit(1);
        }

        if(childpid == 0)

        {       printf("\nChild1:The file written to pipe with cat\n");
                close(1) ;
                dup(fd[1]);
                close(fd[0]);
                execlp("/bin/cat", "cat", file,NULL);

        }
        else
        {
            wait(NULL);  
            //close(0) ; 
            //dup(fd[0]) ;
            //close(fd[1]);
            //nbytes = read(fd[0], readbuffer, sizeof(readbuffer));

            //printf("%s\n",readbuffer);
        }

        return(0);
} 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

int main() 
{ 
        int   fd[2],nbytes;
        pid_t   childpid;
        char    readbuffer[80];

        pipe(fd);

        if((childpid = fork()) == -1)
        {
                perror("fork");
                exit(1);
        }

        if(childpid == 0)
        {
        }
        else
        {
            close(0) ; 
            dup(fd[0]) ;
            close(fd[1]);
            nbytes = read(fd[0], readbuffer, sizeof(readbuffer));
            printf("%s\n",readbuffer);

        }

        return(0);
}