9

What kind of methods exists in linux for low level disk operations in C++? I am attempting to write my own manager of data on a disk. For example, I would like to create a C++ program in the Linux environment that allocated a certain amount (continuous) on a disk and then freely allows me to read/write to that chunk of data. I don't think I want to use the standard fstream::open because then the file is managed by the OS and I might not get a continuous section on the disk.

Thanks.

Matthew
  • 3,886
  • 7
  • 47
  • 84

3 Answers3

6

Generally, "low level" disk operations from user programs1 in Linux involve opening the disk special device. On my computer, these are called names like "/dev/sda" or "/dev/sda4" or even "/dev/disk/by-uuid/2a5150b4-71cb-11e1-b2fe-3b0d270b4e16".

You should take great care in choosing your device file. Writing to your system partition using this is not a good idea. Also, opening the device file needs root access in most cases (for obvious reasons).

The question of whether to use fstream is orthogonal. You may use std::fstream, fopen or even open to open the device. Then use whatever read operation matches the open that you did.

For your specific example, you might reconsider whether you need this functionality. Quoting Wikipedia, which in turn is quoting the Linux System Administrator Guide:

However, as the Linux System Administrator Guide states, "Modern Linux filesystem(s) keep fragmentation at a minimum by keeping all blocks in a file close together, even if they can't be stored in consecutive sectors. Some filesystems, like ext3, effectively allocate the free block that is nearest to other blocks in a file. Therefore it is not necessary to worry about fragmentation in a Linux system."


1 Since you mention C++, I assume you are writing a user program and not a device driver. Truly "low level" disk operations are available only inside the kernel. If you are, in fact, wanting to write a device driver, please restate your question to make that clear.

Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • 2
    Just make sure that it is the right device, writing to your system partition using this is not a good idea. Also needs root access in most cases (for obvious reasons). – josefx Nov 26 '12 at 19:31
  • @josefx - Thanks. Plagiarized. – Robᵩ Nov 26 '12 at 19:35
  • 1
    Thanks @Robᵩ, I am trying to write a Database Management System (DBMS) so it is not a device driver but a piece of software. Thanks for the great information! – Matthew Nov 27 '12 at 03:20
  • Are there any differences in low level access through c++ libraries and getting access at kernel level? – gabber12 May 26 '14 at 13:20
1

I am not aware of any way to do this using standard Linux filesystems. I think you'll have to have a separate partition and do I/O directly on its dev pseudo-file (e.g. /dev/sda2).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
-2

You should use system calls. There's a list here: linux system calls

  • I assume he was downvoted because that page refers 2.2 kernel? Or was it something else... – Aaron H. Nov 26 '12 at 19:29
  • I wrote it as a sample, How can I know which kernel version that he uses? Most of system calls are same on linux kernels. – Hasan Hüseyin Çakır Nov 26 '12 at 19:31
  • Well my company blocks that website for being a "Dangerous Verified fraudulent page or threat source.", but it could be that a list of system calls is simply not a helpful answer. – Mooing Duck Nov 26 '12 at 19:31
  • But how can he know if the downvoter doesn't comment why he downvoted? – Aaron H. Nov 26 '12 at 19:32
  • System calls don't give you low-level disk access. The answer is simply incorrect. – melpomene Nov 26 '12 at 19:37
  • 1
    If you are going to write such a program you have to use system calls. Unless you have to use Assembly in which you have to be careful, because if you can't position the hard disk drive's head correctly then you all got a rubbish hard drive. – Hasan Hüseyin Çakır Nov 26 '12 at 19:41
  • No, you don't have to use system calls. See [Robᵩ's answer](http://stackoverflow.com/a/13571701/1848654). – melpomene Nov 26 '12 at 19:43