Getting System and Process Information Using C Programming and Shell in Linux

Whenever you start a new process in Linux it creates a file in /proc/ folder with the same name as that of the process id of the process. In that folder, there is a file named “status” which has all the details of the process. We can get those Process Information Through shell as follows:

cat /proc/1/status

to-get-process-information-using-shell

As can be seen, it displays most of the information about the process.

Note:, In this case, the process id is 1, it may be changed as per need.

You can get the System Information through the shell. The basic system information is stored in a file named os-release in /etc/ folder.



cat /etc/os-release

System-information-through-shell

You can also get the System Information using C programming. The below code is used to get the details of the system. In this code, utsname maintains a structure that has the details of the system like sysname nodename, release, version, etc.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<sys/utsname.h>
int main()
{
   struct utsname buf1;
   errno =0;
   if(uname(&buf1)!=0)
   {
      perror("uname doesn't return 0, so there is an error");
      exit(EXIT_FAILURE);
   }
   printf("System Name = %s\n", buf1.sysname);
   printf("Node Name = %s\n", buf1.nodename);
   printf("Version = %s\n", buf1.version);
   printf("Release = %s\n", buf1.release);
   printf("Machine = %s\n", buf1.machine);
}

chevron_right


On execution the above code will give the following output:

system-information-though-c

To get Process Information using C programming, use the below code. In this code, we execute the Linux command through a c program to get the details of the process.

filter_none

edit
close

play_arrow

link
brightness_4
code

#include<stdio.h>
#include<stdlib.h>
int main()
{
   int r=system("cat /proc/1/status");
}

chevron_right


On execution the above code will give the following output:

to-get-process-information1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.