I want to know, how can we find user's process statistics about resource utilization( like CPU, Memory) using c program and without using any user command tool. Currently I am running ubuntu 10.10.
Thanks
The canonical way these days is to parse the information in the /proc virtual filesystem procfs. It contains textual information on nearly all aspects of the system, including detailed per-process statistics. The information is structured, and is intended for ease of parsing and programmatic access. (This is how tools such as ps work.)
For example, to query the I/O metrics of a given process, you would read the file under /proc/<pid>/io. This contains a series of name: value pairs, like so:
rchar: 14823550
wchar: 138670414
syscr: 11549
syscw: 3013
read_bytes: 483328
write_bytes: 8192
cancelled_write_bytes: 0
For detailed information, see:
"The /proc Filesystem" - kernel reference documentation
Related
Context: I'm a student who just finished an operating systems course and is currently taking a databases course.
I'm confused about how the OS and the DBMS interact with one another.
For example, what happens when a user program tries to access a file? Does a system call get invoked that is then handled by the OS to find the correct file and data? Or is the call handled by the DBMS, which can then more efficiently find the data (tuple/record) using a B+ tree for example? And then the DBMS makes a call to the OS to actually get the data?
Is the database only accessed if using a programming language like SQL? If I just write a simple C program that writes a file to disk, is the data really stored in a "database" or just in some block on disk where the information for the file is stored within the inode for that file?
I apologize if this isn't the correct forum to ask this question and also if this question is too simple. I tried looking online, but surprisingly didn't find much info (maybe I was searching for the wrong key words?)
For example, what happens when a user program tries to access a file?
That depends on how the user program is accessing the file. fopen/read/write calls are offered by the filesystem managed by the OS.
Does a system call get invoked that is then handled by the OS to find the correct file and data?
If a database is used, the database manages it's own set of data files, and indexes into those data files. The database engine executes IO requests which are handled by the underlying OS. Additionally, the database most possibly will also do caching to reduce file IO.
Or is the call handled by the DBMS, which can then more efficiently find the data (tuple/record) using a B+ tree for example? And then the DBMS makes a call to the OS to actually get the data?
Depending on the database query, the data can be read sequentially, or accessed randomly via an index lookup.
Is the database only accessed if using a programming language like SQL? If I just write a simple C program that writes a file to disk, is the data really stored in a "database" or just in some block on disk where the information for the file is stored within the inode for that file?
It's stored in the filesystem that might used a block based design. A simple C program should use an SDK to connect to a database, and then invoke SQL statements.
Hope this helps!
The answer really depends upon the operating system and the database system.Industrial-strength operating systems have support for multiple file structures and record level locking. A database system can make use of the facilities provide by the operating system for locking and indices. In that case, much of the database works by invoking system service calls to the operating system.
With the rise of brain damaged operating systems that do not support records at all, let alone record or column locking, database system does nearly of the work, beyond low level I/O calls. Some operating systems are so brain damaged that database systems have to create their own partitions and effectively manage their own file systems within the partition. There are no files at all from the perspective of the operating system.
I want to get information about the battery in C on linux. I don't want to read or parse any file! Is there any low-level interface to acpi/the kernel or any other module to get the information I want to have?
I already searched the web, but every question results in the answer "parse /proc/foo/bar". I really don't want to do this because I think, low-level interfaces won't change as fast as Files do.
best regards.
The /proc filesystem does not exist on a disk. Instead, the kernel creates it in memory. They are generated on-demand by the kernel when accessed. As such, your concerns are invalid -- the /proc files will change as quickly as the kernel becomes aware of changes.
Check this for more info about /proc file system.
In any case, I don't believe there's any alternative interface.
You might be looking for UPower: http://upower.freedesktop.org/
This is a common need for both desktop environments and mobile devices, so there have been many solutions over time. For example, one of the oldest ones was acpid, which is pretty much obsolete now.
While I'd recommend using a light-weight abstraction like UPower for code clarity reasons, the files in /proc and (to some extent) /sys are considered part of the Linux kernel ABI, which means that changing them is generally frowned upon.
I am wondering if it's possible to write an application that will access a foreign filesystem, but without needing support for that filesystem from the operating system. For example, I'd like to write an app in C that runs on Mac OS X that can browse / copy files from an ext2/ext3 formatted disk. Of course, you'd have to do all the transfers through the application (not through the system using cp or the Finder), but that would be OK for my purpose. Is this possible?
There are user space libraries that allow you to access file systems.
The Linux-NTFS library (libntfs) allows you to access NTFS file systems and there are user space programs like ntfsfix to do things to the file system.
E2fsprogs does the same for ext2, ext3 and ext4 filesystems.
As Basile mentioned, Mtools is another one that provides access to FAT partitions.
There was even a program that does exactly what you're looking for on Windows. It's called ext2explore and allows you to access ext2 partitions from Windows.
It is possible. For example the GNU mtools utility are doing that (assuming a way to access the raw device or partition) for MS-DOS FAT file systems.
However, file systems inside the kernel are usually very well tested and optimized.
Yes and No. For a regular user Application is usually not possible because access to block devices is restricted to root only. Every block device should give read/write to the needed block device for that effect. This would need at best a server/client approach where a service is started on the machine and configured to give the permissions on a per block device manner.
The somewhat easier alternative would be you to use the MacFUSE implementation.
Look here:
http://code.google.com/p/macfuse/
http://groups.google.com/group/macfuse?pli=1
The MacFuse project seems no longer mantained, but can give you a starting point for your project.
The dirty and quick approach is the following as root chmod 666 /dev/diskN
You can hijack syscalls and library calls from your application and then redirect reads/writes to anything like a KV store or a distributed DB layer (using the regular calls for the "virtual devices" that you do not support).
Then, the possibilities are boundless because you don't have to reach the physical/virtual devices when someone asks for them (resolving privilege issues).
i used to get a list of processs under linux by enumerating the /proc file system, since it had plain-text files that i can read data from (stat, status, exe link....) but thats not the case on solaris, i tried porting my tools to Oracle Solaris 11 (my first solaris) but it wont work, i tried accessing the /proc folder manually, but couldn't find anything readable, but ps -fu user works !
is it possible that someone can point me on how to get a list of processes uneder solaris?
im coding in gcc btw.
thanks.
Unlike Linux, Solaris /proc is providing binary data, not text one.
Solaris has an extensive and detailed manual page proc(4) describing what the different files under a process number hierarchy contain, how to access them and what structures to use in order to get their content.
This manual page is of course also accessible locally with man -s 4 proc
Similar to this: How to get process info programmatically in C/C++ from a Solaris system?
You want the interface described by /usr/include/procfs.h and /usr/include/sys/procfs.h to decode the binary data in /proc
Can anyone suggest some learning materials to accomplish my question? I'm working with the Linux kernel.
I'm planning on conducting an experiment, but need access to my process' page table, and so far have only found kernel space code to accomplish what I want.
Thanks
You'll probably find the information you want in the proc filesystem, under /proc/self. In particular /proc/self/maps contains the list of memory mappings in the process, with an indication of their permission and file when applicable. There is a little more information in /proc/self/smaps. You'll have to look in the kernel documentation for the format, specifically Documentation/filesystems/proc.txt.
Accurate memory metrics can be given by pagemap kernel interface - utilized in libpagemap library https://fedorahosted.org/libpagemap/. Library provides also userspace utils so you can start monitor memory immediately.