繁体   English   中英

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

[英]Using pipe between diffrent programs how ?

再次提问并修改了代码...

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

程序0:使用两个子进程创建一个父进程,并执行带有子进程的程序1和程序2,等待它们完成并关闭。

程序1:从用户那里获取一个文件名并将文本写入文件。当按下CTNL + D并完成创建管道时,它完成写入操作。之后,使用cat命令将文件写入stdout,并使用dup()创建具有以下内容的管道:文件中。

程序2:它借助dup()从管道读取文件名,然后执行wc命令。

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

当我尝试从program1内的管道读取数据时,它可以工作(请参阅program1中已停用的代码),但是如果将其放入program2中,则相同的代码将不起作用。

那么,如何使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);
}
}

程序1

#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);
} 

程序2

#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);
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM