简体   繁体   中英

How does software reference a file on a hard drive

Sometimes, knowing what happens 1 layer underneath is a good thing but IMO .NET is becoming so abstract (in a good way) it can make learning difficult as I don't understand what goes on below the surface!

I assume .NET is very coupled with the MS operating system. So, when using Directory() or File() classes, I can get data on the file and can understand (just) the theory on how it works.

But, something like Java is not native to Windows. So, how can it reference a file on my hard drive? Although I would have liked my question to be about any non Microsoft language, I'm limiting to Java to keep it focused

In my head, there are only 2 ways.

  1. Get the file direct from the hard drive (and therefore not use the OS.
    OR (and this is where I am getting lost)

  2. Would the Java Runtime environment have to create an Instance of the OS to then get the public method which exposes a 'HereAreTheFiles()' method. EG the below pseudo code

      WindowsOS windowsOs = new WindowsOS(); string[] files = windowsOs.GetAllFolders() 

I hope this makes sense.

Every operating system will have system apis to interact with OS / System related tasks.

In Java case, JVM / JRE which is specific to the Windows is being run on the Windows Environment. so all the Java System functions interact with the OS/System APIs to do file read / write .

Java provides a layer of abstraction to do these operations, which inturn it implements by calling a System API/Native API call to the Operating System

+1 for inquisitiveness. I would like to point out however, that Java API is abstract from the underlying OS, but the actual Java Runtime is not. You'll notice that there are different Java bundles for each OS. So under the covers Java file open operation translates to very OS-specific API calls written in C.

Here's a simplistic overview of everything that's going on.

At the core of the operating system is the kernel, all programs ask kernel to do things for them. This includes reading files and directories. The kernel handles reading data directly off the disks.

Userspace (that is. non-kernel) programs communicate with the kernel through system calls, these involve setting up your cpu and memory in a particular way, and then calling a special cpu instruction.

The operating system provides libraries that does these system calls for you (in C), you can use these libraries in your own C program to make these system calls. There's also libraries on top of that which handles more complex actions.

Your java program runs in a java runtime. This runtime is written in a language which can manipulate the cpu/memory directly (in the sense that it's not abstracted behind a virtual machine, which your java program is). As such, it can make system calls too (it actually links with the libraries the OS provides to do so).

Finally, your java program, when it reads a file, will call into a native function in the runtime, which will call into an OS provided library, which will make a system call, which will be handled by the kernel, which will read the data from the disk.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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