简体   繁体   English

简单的Linux管道程序,没有收到数据

[英]Simple Linux pipe program, data is not received

I have a simple Linux C program that I'm writing to help me better understand IPC, right now I'm trying to build it with pipes. 我有一个简单的Linux C程序,我正在编写,以帮助我更好地理解IPC,现在我正在尝试用管道构建它。
I have a single code base that I run in two different terminal windows as two different executables (so they can talk to each other). 我有一个代码库,我在两个不同的终端窗口中作为两个不同的可执行文件运行(因此它们可以相互通信)。 However I'm not doing something correct, because I never get any data to read, but I'm not sure what... 但是我没有做正确的事情,因为我从来没有得到任何数据可读,但我不确定是什么......

NOTE This is not the full code, I chopped out the output/input/validation to save space. 注意这不是完整的代码,我切断了输出/输入/验证以节省空间。 But it's noted in the comments in the program below. 但是在下面的程序评论中注意到了这一点。

void main()
{
  int pipefd[2], n;
  char input = 0;
  char buffer[100] = {0};
  char outpipe[100] = {0};

  if(pipe(pipefd) < 0) {
    printf("FAILED TO MAKE PIPES\n");
    return;
  }

  printf("Starting up, read fd = %d, write fd = %d\n", pipefd[0],pipefd[1]);

  do {
    //print menu options (send message, get message, get my fd, 
    // set a fd to talk to, quit)

    // if "send a message":
    {
      printf("What would you like to send?\n");
      fgets(buffer, 100, stdin);
      write(pipefd[1], buffer, strlen(buffer));
    }
    //else if "read a message":
    {
      if(open(outpipe, 0) < 0)
          printf("Couldn't open the pipe!\n");
      else {
        n = read(outpipe, buffer, 100);
        printf("I got a read of %d bytes\nIt was %s\n",n, buffer);
        close(outpipe);
      }
    }
    //else if "get my file descriptor":
      printf("My fd tag is: /proc/%d/fd/%d\n", (int)getpid(), pipefd[0]);
    //else if "set a file descriptor to talk to":
    {
      printf("What is the pipe's file descriptor?\n");
      fgets(outpipe, 100, stdin);
      n = strlen(outpipe) - 1;
      outpipe[n] = '\0';
    }
  } while (input != 'Q');
return;
}

I know the pipes are created successfully, I verified the file descriptors are in place: 我知道管道创建成功,我验证了文件描述符到位:

lr-x------ 1 mike users 64 Sep 26 23:31 3 -> pipe:[33443]
l-wx------ 1 mike users 64 Sep 26 23:31 4 -> pipe:[33443]

Looks like the permissions are OK (read on pipe 3, write on pipe 4). 看起来权限是正常的(在管道3上读取,在管道4上写入)。

I use it as such: 我这样使用它:

//terminal 1
Pick an option:
3
My fd tag is: /proc/8956/fd/3

//terminal 2
Pick an option:
4
What is the pipe's file descriptor?
/proc/8956/fd/3

Pick an option:
1
What would you like to send?
hello

//terminal 1
Pick an option:
2
I got a read of -1 bytes
It was 

Is there anything obviously wrong that I'm doing here? 我在这里有什么明显的错误吗? My reads always get "-1" return value... 我的读取总是得到“-1”的返回值...

It seems you have misunderstood how pipe works. 你似乎误解了管道是如何工作的。 A pipe is an anonymous file descriptor that is not going by file in the file system. 管道是匿名文件描述符,不是文件系统中的文件。 The files in /proc/<pid>/fd you don't have to care about. /proc/<pid>/fd你不必关心。

Here is a rewrite of what you are trying to do: 这是对您要做的事情的重写:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(void)
{
    int pipefds[2];
    char input[128];
    char output[128];
    ssize_t nread;

    if (pipe(pipefds) == -1)
    {
        perror("Could not create pipe");
        return EXIT_FAILURE;
    }

    printf("Enter input: ");
    if (fgets(input, sizeof(input), stdin) == NULL)
    {
        perror("Could not read input");
        return EXIT_FAILURE;
    }

    /* "Remove" newline from input */
    if (input[strlen(input) - 1] == '\n')
        input[strlen(input) - 1] = '\0';

    /* Now write the received input to the pipe */
    if (write(pipefds[1], input, strlen(input) + 1) == -1)
    {
        perror("Could not write to pipe");
        return EXIT_FAILURE;
    }

    /* Now read from the pipe */
    if ((nread = read(pipefds[0], output, sizeof(output))) == -1)
    {
        perror("Could not reaf from pipe");
        return EXIT_FAILURE;
    }

    /* We don't need to terminate as we send with the '\0' */

    printf("Received: \"%s\"\n", output);

    return EXIT_SUCCESS;
}

Here is your primary concern: 这是您最关心的问题:

./ipctest.c: In function ‘main’:

./ipctest.c:32:9: warning: passing argument 1 of ‘read’ makes integer from pointer without a cast [enabled by default]
/usr/include/unistd.h:361:16: note: expected ‘int’ but argument is of type ‘char *’

./ipctest.c:34:9: warning: passing argument 1 of ‘close’ makes integer from pointer without a cast [enabled by default]
/usr/include/unistd.h:354:12: note: expected ‘int’ but argument is of type ‘char *’

Look at the data types required for a certain function... :) 查看某个函数所需的数据类型...... :)

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

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