繁体   English   中英

在C linux中使用管道的2种方式进行通信

[英]2 way comm using pipes in C linux

我希望父进程和子进程在C linux中使用管道进行通信。 首先,我希望父母传递一个字符串,然后让孩子传递它。 我创建了两个文件描述符。 一个用于父级到子级,即readpipe和其他writepipe,反之亦然。 问题是它没有将我的数据作为输入。 我也希望将printf语句(例如“输入您的数据”)打印一次,但是由于在fork之后,有两个进程,因此它们被显示两次。 还有其他选择吗?

 //readpipe[0] = child read
 //readpipe[1]= parent write

//writepipe[0]=parent read
//writepipe[1]=child write

 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <string.h>

 int main(void)
 {
      pid_t pid;
      int r;
      /* Hope this is big enough. */
     char buf[1024];
     char cp[50];
     char ans;
     int readpipe[2];
     int writepipe[2];
     int a;
     int b;
     a=pipe(readpipe);
     b=pipe(writepipe);
     if (a == -1) { perror("pipe"); exit(EXIT_FAILURE); }
     if (b == -1) { perror("pipe"); exit(EXIT_FAILURE); }

     printf("\nSEND SOMETHING TO CHILD PROCESS\t");
     scanf(" %c",&ans);
     pid=fork();
     if(pid==-1)
     {
         printf("pid:main");
         exit(1);
     }

     while(ans=='y' || ans=='Y')
     {
        printf("\nEnter data\t"); //printed twice
        fgets(cp, 50, stdin);     //not taking data
        printf("\n%s",cp);        
        if(pid==0)
        { //CHILD PROCESS
         close(readpipe[1]);
         close(writepipe[0]);
         read(readpipe[0],buf,sizeof(buf));
         printf("\nSENT\n %s",buf);
         write(writepipe[1],cp,strlen(cp)+1);
       }
      else
      { //PARENT PROCESS
        close(readpipe[0]);
        close(writepipe[1]);
        write(readpipe[1],cp,strlen(cp)+1);
        read(writepipe[0],buf,sizeof(buf));
        printf("\nRECEIVED\n %s",buf);
     }
     printf("\nSEND SOMETHING TO CHILD PROCESS\t");
     scanf(" %c",&ans);
  }
  close(readpipe[1]);
  close(writepipe[0]);
  close(readpipe[0]);
  close(writepipe[1]);

   return 0;
}

你打电话

    printf("\nEnter data\t"); //printed twice
    fgets(cp, 50, stdin);     //not taking data

检查您是在父进程还是子进程中之前。 当然,这会导致两个过程都打印并且都从标准输入中读取。 因此,尚不清楚哪个进程读取您键入的日期。

在这些行中会发生相同的问题:

 printf("\nSEND SOMETHING TO CHILD PROCESS\t");
 scanf(" %c",&ans);

我建议您重新设计程序,以清楚地将在父进程afrer fork()中运行的代码与在子进程中运行的代码分开。

  1. 为什么fgets不获取数据? 在第一个scanf中,您正在读取单个字符,并且换行符仍在
    输入缓冲区,因此可以跳过

    为了解决这个问题,你应该使用的scanf( “%[^ \\ n]的%* C”,与ANS) -参阅了解更多详情

  2. 查找修改后的代码..这可能对您有所帮助(从我进行一些更改并验证基本功能的同时中断)

int main(void){

pid_t pid;
int r;
/* Hope this is big enough. */
char buf[1024];
char cp[50];
char ans;
int readpipe[2];
int writepipe[2];
int a;
int b;
a=pipe(readpipe);
b=pipe(writepipe);
if (a == -1) { perror("pipe"); exit(EXIT_FAILURE); }
if (b == -1) { perror("pipe"); exit(EXIT_FAILURE); }

printf("\nSEND SOMETHING TO CHILD PROCESS\t");
scanf("%[^\n]%*c",&ans);
//ans = getchar();
fflush(stdin);
pid=fork();
if(pid==-1)
{
    printf("pid:main");
    exit(1);
}

while(ans=='y' || ans=='Y')
{
    if(pid==0)
    { 
        //CHILD PROCESS
        close(readpipe[1]);
        close(writepipe[0]);
        if(read(readpipe[0],buf,sizeof(buf)) < 0)
        {
            break;
        }

        printf("\nChild Process Read: %s\n",buf);

        printf("\n(child)Enter data:\t"); //printed twice
        fgets(cp, 50, stdin);     //not taking data
        printf("\nData Written to Parent%s",cp);
        if(!strncmp("Q",cp,1) || write(writepipe[1],cp,strlen(cp)+1) < 0)
        {
            break;
        }
    }
    else
    { 
        //PARENT PROCESS
        close(readpipe[0]);
        close(writepipe[1]);
        printf("\n(Parent)Enter data\t"); //printed twice
        fgets(cp, 50, stdin);     //not taking data

        printf("\nData Writtent to Child: %s",cp);
        if(!strncmp("Q",cp,1) || write(readpipe[1],cp,strlen(cp)+1) < 0)
        {
            break;
        }        

        if(read(writepipe[0],buf,sizeof(buf)) < 0)
        {
            break;
        }
        printf("\nParent Process Read: %s\n",buf);
    }
    ans ='y';
}

close(readpipe[1]);
close(writepipe[0]);
close(readpipe[0]);
close(writepipe[1]);
return 0;

}

暂无
暂无

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

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