Вопрос

I have been reading about file descriptors and how the operating system manages files and provide access to them for processes using the C system call "open()", and I have couple of questions:

  • The file descriptors table: is it just one big data structure that all processes share (like, say there are two processes: process A and process B, do they have pointers to the OS's file descriptors table), or does the operating system create a whole new array of file descriptors for each process?
  • What other programming languages can invoke system calls? for example, I know that with C/C++ you can easily invoke system calls using the OS interface (fork(),close(),open(),execl()... and so on), but what about Java? I don't recall that there's a way of invoking these functions. how exactly does it work there? does the JVM just translate (what need to be translated) my code to the above system calls? so then what about other high-level programming languages like Python?
  • What harm can be done if I will not close a file after opening it with the system call close()?

Thanks.

Это было полезно?

Решение

  1. There is a system-wide table of open file descriptions, and there is a per-process table of open file descriptors. Each open file descriptor refers to an open file description (see open() and close()), but several descriptors may refer to the same description (think dup(), dup2() and fork()).

  2. Any language can invoke system calls by calling an appropriate piece of assembler which actually makes the system call. Most scripting languages are built on top of C or C++, so they don't count, but other languages — such as, say, Fortran or Ada or D or Go — can provide access to the system calls in a manner appropriate to their semantics.

  3. You've got a file descriptor open that doesn't need to be open, so you can have one fewer other files open at once. If your program is short and won't run out of resources, you can neglect to close it. Closing the file helps ensure that data is written to disk; the OS kernel knows that any disk blocks it has in its buffer cache won't be needed again if your program is the only process that had the file open and you close it.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top