9

I am using Ubuntu and I want to read the version of its kernel. I found a file named version in /proc/ that records the version of the current kernel.

If I dont want to read file, is there any other way, like built-in function in C, that I can read the version in C?

Thanks

2
  • 2
    Why don't you want to read a file?
    – icktoofay
    Commented Jun 7, 2010 at 6:58
  • @icktoofay - Because that would be an inefficient way to perform this.
    – Geoffrey
    Commented Aug 25, 2012 at 9:36

6 Answers 6

22

You can use the uname() system call.

10

Check the uname function. It gives you a lot of information without the need to parse output of some linux executables.

2
  • 1
    Thanks a lot, that's what I need. Thanks all for your support.
    – ipkiss
    Commented Jun 10, 2010 at 3:35
  • how about actually including the name of that function in your answer, instead of hiding it in the link?
    – Alnitak
    Commented Sep 24, 2015 at 14:31
7

You might want to try using the uname function.

0

This should do:

system("uname -r");

EDIT: type man uname in a terminal to get the list of options you can use with uname

1
  • 7
    This just prints the info on the standard output. To use it in the program, better use the system call. Commented Jun 7, 2010 at 7:10
0

Or you can read /proc/version, but this is not as good as calling uname(2) directly. uname(2) is more natural to C.

-1

Look at this article for the shell based way of getting kernel information. You can suitably run all of this using the system() call. But I am assuming that wouldn't be enough in your case. You'd need someway to parse the shell output. Hence make use of popen() call.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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