interview question how does application connects to device

Greg Freemyer greg.freemyer at gmail.com
Sun Jul 17 15:25:06 EDT 2011


On Sun, Jul 17, 2011 at 1:34 PM, Bond <jamesbond.2k.g at gmail.com> wrote:
> On Sun, Jul 17, 2011 at 9:36 PM, Mulyadi Santosa
> <mulyadi.santosa at gmail.com> wrote:
>> On Sun, Jul 17, 2011 at 22:52, Bond <jamesbond.2k.g at gmail.com> wrote:
>>> Let me know what do you understand from this.
>>
>>
>> And Greg already kindly answer that for you too. Didn't you see his
>> answer? And why do you rant here anyway? Simply getting an answer
>> here, you already lucky and you should be thankful.
>>
>> --
> Let me know which part of answer answered as how application connects
> to particular driver.I see answer explaining finer details of device
> driver mehcanism but I did not see any where answer to original
> question or if it has been answered I probably have been stupid enough
> not to be able to
> follow it.

Bond,

Here's a trivial userspace example.

What is the official userspace method for determining if a harddrive
is a traditional rotating disk, or a SSD?

I suspect you won't believe it, but it is just:

cat /sys/block/sda/queue/rotational.

What is the official userspace method for informing the kernel you
want to override its determination of rotating and set it to the SSD
setting?

echo 0 > /sys/block/sda/queue/rotational

Obviously you can code the userspace app for the above in any language you want.

The big thing is that the above is a simple userspace example of
userspace interfacing with the kernel via a formal abi.  Note I say
ABI not API.  ioctl changes in different architectures, so while it
can make a fine API, it is not a very good ABI.  That lack of
consistency in its ABI is one of the reasons it is discouraged.

sysfs is now the preferred solution for most basic userspace / kernel
interaction due to its simplicity and consistent ABI regardless of
platform / architecture.  You can see how almost trivially easy the
userspace side of the interface is.  You can also see that it is
exactly the same for a 32-bit app and for a 64-bit app.

You as a future kernel developer need to know how to write the kernel
side of the above.

As you do that, you will see that sysfs is designed for passing single
parameters back and forth.  If you have a need to pass multiple
parameters in a single atomic block, the sysfs is not the right
choice.  netlink sockets would be the most common recommended
interface for sending a collection of parameters at one time.  ie.
ioctl does the same by passing a structure pointer, but as I said
before ioctl is now discouraged.

Greg



More information about the Kernelnewbies mailing list