用C linux的儿童和家长进程之间的通信:父进程不堵 [英] communication between child and parent processes in C linux: parent process not blocking

查看:108
本文介绍了用C linux的儿童和家长进程之间的通信:父进程不堵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想父子进程使用的管道用C linux的沟通。首先我想父母传递一个字符串,然后孩子承认这一点。我创建了两个文件描述符。一个是父母和孩子之间,即readpipe等writepipe为反之亦然。
问题是,它不是把我的数据作为输入。此外,我想printf的语句,如请输入您的数据进行一次打印,但由于后叉,所以他们正在显示两次有两个进程。任何替代了??

I want parent and child processes to communicate in C linux using pipes. First I want parent to pass a string and then child to acknowledge it. I have created two file descriptors. one for parent to child i.e. readpipe and other writepipe for viceversa. The problem is its not taking my data as input. Also I want the printf statements such as "Enter your data" to be printed once but since after fork, there are two processes so they are being displayed twice. Any alternative to that??

 //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;
}

谢谢:)

推荐答案

有在code的几个问题:

There are a few problems in your code:

1

close(writepipe[1]);

您应该你以后需要无法关闭文件描述符。

You should not close file descriptor you needed later.

2

read(readpipe[0],ch,sizeof(ch));

CH NULL ,这意味着你没有为它指向分配任何空间。

ch is NULL, which means you did not allocate any space for it to point to.

3。

ch1="YES";
write(writepipe[1],ch1,sizeof(ch1));

您应该使用的strlen(CH)+1 而不是的的sizeof(CH)来找出有多少个字节需要写入。

You should use strlen(ch)+1 instead of sizeof(ch) to find out how many bytes need to be written.

4。

有在code的父进程类似的问题。

There are similar problems in your code for parent process.

下面是根据在这个问题上的code一个固定的版本:

Here is a fixed version based on the code in this question:

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

    int readpipe[2];
    int writepipe[2];
    int a;
    int b;
    a=pipe(readpipe);
    b=pipe(writepipe);
    // check a and b

    pid=fork();
    // check pid

    if(pid==0)
    { //CHILD PROCESS
        close(readpipe[1]);
        close(writepipe[0]);
        read(readpipe[0],buf,sizeof(buf));
        printf("\nREAD = %s",buf);
        close(readpipe[0]);
        cp="YES\n";
        write(writepipe[1],cp,strlen(cp)+1);
        close(writepipe[1]);
    }
    else
    { //PARENT PROCESS
        close(readpipe[0]);
        close(writepipe[1]);
        cp="HI!! YOU THERE";
        write(readpipe[1],cp,strlen(cp)+1);
        close(readpipe[1]);
        read(writepipe[0],buf,sizeof(buf));
        printf("\nACK RECEIVED %s",buf);
        close(writepipe[0]);
    }
    return 0;
}

这篇关于用C linux的儿童和家长进程之间的通信:父进程不堵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆