ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm going to try my hand at writing a daemon to monitor a directory and if a file is dropped it detects what is new and runs a script to read that file, etc.
I'm new to Linux daemon programming but have an example of how to properly create a working daemon (just the bare bones). I also know C and C++ (this will be done in C using gcc, of course). I'll be working on the Ubuntu Server 6.06.1 version.
My question is, how much load is on a server when a daemon is running? I mean, I know mysqld will do something when someone accesses the MySQL database, for example, and it can get quite busy if you have a lot of folks doing database queries.
If I have my daemon running say, once a second, would that be about right? Should I use a timer to wait 1 second before polling the directory for a file?
/* Finish up */
syslog( LOG_NOTICE, "terminated");
Which is part of int main... Now does this mean that it does the program in int main... then ends? Is it that the program ends and then reloads itself? This btw, will be started from init.d I presume (someone correct me if I'm wrong in where it's supposed to be started/loaded on boot).
"If I have my daemon running say, once a second, would that be about right? Should I use a timer to wait 1 second before polling the directory for a file?"
Rather than looping on a directory read to find out if any processing needs to be done you should have your program called only when it is needed. You should have the program which creates the file pipe the information to your new program instead, or something similar.
As long as you use some level of latency in the daemon, that will be a huge relief on the system. I find that even a .01s wait in a monitor loop doesn't draw that much from the system. A few things will drain resources, though, such as intensive algorithms and the load of multiple threads. Allocation of dynamic/shared memory will draw resources, also.
If the daemon is just there to wait for an event, it shouldn't draw any more resources than calling a program to do the operation manually at those times. If it's meant to run constantly, that's more of an issue of if you can afford to have that program run even if it isn't a daemon.
On a reasonably fast system (1GHz) I'd say any wait loop latency higher than .1s will be close to unnoticeable.
Turns out I've been instructed that we'll be monitoring it using a script (I have to write) from a cron job. So I won't need to write the daemon after all.
But I still want to do it just to see how daemons work and how to write them, for my own information. So I'll be trying your code. Another thing is, that originally, there was to be a php script to write a file and thus a directory monitor notice the file and launch a script to read the file and do what the file is asking (codes to initiate different actions). Now I see my idea was bit cumbersome.
But still going to play around with it just for grins.
And I just in general want to learn to write daemons in case I need to in the future.