1

In many languages there are libraries, either built into the language itself or built separately outside of the language, which allow information to be taken from the operating system at some level. Examples are metadata about a file, current memory usage and the OS version itself.

How is this information generally accessed?

At the moment the only way I can see it working is through executing command line functions and parsing the answer.

For example, to list the files in a current directory on Linux your application would execute 'ls' at the specified directory and then parse the output to produce the required format. Is this the only way?

This example assumes you are using no libraries which do it for you of course (or maybe you're building a library for this purpose.

  • Your question shows that you have not understood the UNIX philosophy yet. Please consider this: The shell, as well as so-called system commands like ls, rm, mkdir and so on are all pretty normal user programs you could have written. So, given this, your idea is absurd, for it would mean that ls had to parse its own output to get information from the system. – Ingo Jan 26 '14 at 22:48
  • It was maybe just an ill thought out example, but I understand the general idea was absurd, that's why I asked ;) – Sam Jan 26 '14 at 22:51
10

Every operating system offers so-called application programming interfaces (APIs) that allow a user program to access such information. The command line commands effectively use these APIs -- e.g. ls does just that.

Wikipedia has a list of operating system APIs.

  • 1
    Good point about the standard command line utilities, like ls. The OP seems to think that ls is part of the Linux core -- it's really just a program like any other program. – Alex D Jan 26 '14 at 20:26
  • Advanced Linux Programming is a good primer for the Linux API. – user16764 Jan 26 '14 at 22:24
  • Ah thanks very much, I was aware it was through an API but I didn't know whether that referenced the library itself giving an API or the OS having an API. This makes sense. Thank you – Sam Jan 26 '14 at 22:49
  • One thing I like about Linux is how many pieces of information are exposed via the filesystem and virtual files e.g. files under /proc/sys. Generally APIs are the better way to access them when available, but sometimes they're particularly useful for grabbing information. – J Trana Jan 27 '14 at 1:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.