views:

78

answers:

3

While creating a file, you provide the name of a file to operating system and it creates file and returns you the handle.

I am curious to know how it is created.

Does the operating system do some kind of hashing on file name to create a unique file handle or does it increase count of created files and return it as a file handle?

+1  A: 

File handles are only unique within one process at a given time. On Linux I think that it is a simple counter that is incremented. I think that Windows returns the memory address to an information block about the file (the information block's structure is internal to the operating system, so it's not possible to deal with it directly).

Anders Abel
+1  A: 

No, it's an index into an array inside the OS kernel, unique to that one process. File handles are usually just small integers.

Andrew McGregor
A: 

The file handle (file descriptor) is just a number which is unique for that particular process. For instance, standard input, output and error has fds (0, 1, 2). In linux you can check the process' file descriptor in /proc/PID/fd where PID is process id.

Here is an example:

$ pidof executable
4711
$ ls -l /proc/4711/fd
...
$ # Use 'lsof' to show file descriptor opened by this process:
$ lsof -p 4711
...
Martin Wickman