How to monitor a device? A Linux Programing question.
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
How to monitor a device? A Linux Programing question.
Greetings to all,
I've just started a little project that needs to monitor a character device in Linux for changes (specifically, a Gumstix computer monitoring /dev/gpio-event).
Quote:
Once pins are setup to be monitored, any program can read /dev/gpio-event to get notified of the events. By default, an ASCII string of the form: "pin edge timestamp" followed by a newline will be delivered to /devb/gpio-event for each event that occurs.
...
For C programs, you can opt to use a binary interface instead. The binary versus ASCII setting is maintained on a per-file basis, so each program could each open /dev/gpio-event with a different ASCII/binary setting.
I don't have much experience programing in Linux, so this could (hopefully) come down to a yes/no answer. Here goes:
If programing in C, would the correct way of getting notified of an event from the device, without using polling, be pipe()? If not, please help with pointers to the correct answer/s.
You might get a better informed answer in the programming or embedded forums.
In C you can just use 'read' on a device file. The call will block until there is something to read, and you would have to loop until you got a newline. Is that the behaviour you want?
Alternatively you can use Asynchronous_I/O (Wikipedia), but that is really a form of polling.
The blocking behavior is exactly what I wanted, but the books I read about programming said reading a file from at it's end would return 0, and the only mention about blocking was in pipes. Thanks for clearing that up!
other functions such as fgets and fread should also work after the device is opened with fopen. if you really, really wanted to use pipes, you could execute 'cat /dev/xxxx' with popen. you'd then read from the popen file handle. i'm just throwing this out there, although i can't think of a good reason to use pipes/popen in this scenario.
Last edited by jdiggitydogg; 09-26-2007 at 10:36 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.