【问题标题】:Close file descriptor关闭文件描述符
【发布时间】:2013-11-11 11:54:21
【问题描述】:

我正在尝试使用 2 个管道编写客户端-服务器程序,但是当我运行以下程序 (server.c) 时:

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "err.h"

#define BUF_SIZE    1024

char message[] = "Hello from your parent!\n";
char response[] = ""; 
int main (int argc, char *argv[])
{
  int fd1 [2];
  int fd2 [2];  
  int buf_len;  
  if (pipe (fd1) == -1) syserr("Error in pipe\n");
  if (pipe (fd2) == -1) syserr("Error in pipe\n");

  switch (fork ()) {
    case -1: 
      syserr("Error in fork\n");

    case 0:

      if (close (0) == -1)       syserr("child, close (0)");
      if (dup (fd1 [0]) != 0)    syserr("child, dup (pipe_dsc [0])");
      if (close (fd1 [0]) == -1) syserr("child, close (pipe_dsc [0])");
      if (close (fd1 [1]) == -1) syserr("child, close (fd1 [1]");

      if (close (1) == -1)       syserr("child, close (1)");
      if (dup (fd2 [1]) != 1)    syserr("child, dup (pipe_dsc [1]"); 
      if (close (fd2 [1]) == -1) syserr("child, close (pipe_dsc [1])");
      if (close (fd2 [0]) == -1) syserr("chiled, close (fd2 [0]");

      execl("./client", "client", (char*) 0); 
      syserr ("child, execvp");

      exit (0);

    default:

      printf("%d\n", fd1[0]);
      printf("%d\n", fd1[1]);
      printf("%d\n", fd2[0]);
      printf("%d\n", fd2[1]);
      if (close (fd1[0] != -1)) syserr("parent, close (fd1[0])");
      if (close (fd2[1] != -1)) syserr("parent, close (fd2[1])");
      while (fgets(message, sizeof(message), stdin) != NULL) {
          if (write (fd1 [1], message, sizeof(message) - 1) == -1) 
              syserr("write");
          if ((buf_len = read(fd2[0], response, BUF_SIZE -1)) == -1) 
              syserr("Error in read");
          printf("%s%s", "response: ", response);
      }   

      if (close (fd1 [1]) == -1) syserr("parent, close (pipe_dsc [0])");
      if (close (fd2 [0]) == -1) syserr("parent, close (pipe_dsc [1])");

      if (wait (0) == -1)
          syserr("wait");
      exit (0);
  } /* switch (fork ()) */
}

还有client.c

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdbool.h>
#include "err.h"

#define BUF_SIZE                1024

int main (int argc, char *argv[])
{
    char buf[BUF_SIZE];
    while (fgets(buf, sizeof(buf), stdin) != 0) {
        printf("%s%s","Write to stdout",  buf);
    }   
    return 0;
}

我得到以下错误:

3
4
5
6
ERROR: parent, close (fd2[1]) (9; Bad file descriptor)

知道为什么我不能关闭描述符吗?

提前问好。

【问题讨论】:

  • 因为它没有打开。如果你得到 EBADF,文件描述符要么是一个完全伪造的整数并且从未初始化(例如假设open 成功),要么它曾经是一个 fd 但你已经关闭了它。

标签: c process operating-system pipe


【解决方案1】:

你写的

close (fd2[1] != -1)

由于 fd2[1] 是 3 或 5 或其他任何值,fd2[1] != -1 的计算结果为 1,而您则为 close(1)

我觉得你更想要

close (fd2[1]) != -1

【讨论】:

  • 此外,我认为只有在 close() 返回 -1 时才应打印错误消息。所以这一行应该是: close( fd2[1] ) == -1;
猜你喜欢
  • 2017-09-05
  • 2011-06-03
  • 1970-01-01
  • 1970-01-01
  • 2012-04-29
  • 2017-08-22
  • 2014-04-06
  • 1970-01-01
  • 2021-03-12
相关资源
最近更新 更多