LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Daemons and Server Load question (https://www.linuxquestions.org/questions/programming-9/daemons-and-server-load-question-575486/)

RavenLX 08-07-2007 07:59 PM

Daemons and Server Load question
 
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?

I'm also unclear on another thing. In the example program (2nd example at http://www-theorie.physik.unizh.ch/~...owto/daemonize

His second example ends with:

/* Finish up */
syslog( LOG_NOTICE, "terminated");
closelog();
return 0;

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).

I'm not quite sure what to do here... :scratch:

Any helpful hints and tips would be appreciated.

jailbait 08-08-2007 12:54 PM

"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.

-----------------
Steve Stites

ta0kira 08-08-2007 01:23 PM

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.
Code:

#include <unistd.h>

void daemon_loop()
{
    struct timespec loop_latency = { 0, 100 * 1000 * 1000 };

    while (true)
    {
    if (/*event*/)
    {
    /*action*/
    }

    nanosleep(&loop_latency, NULL);
    }
}

ta0kira

RavenLX 08-08-2007 07:40 PM

Thank you! :)

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.

ta0kira 08-08-2007 08:25 PM

Here is a working example:
Code:

#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>


int main(int argc, char *argv[])
{
        if (argc != 3)
        {
        fprintf(stderr, "%s [filename] [wait time]\n", argv[0]);
        return 1;
        }

        int wait_value = strtoul(argv[2], NULL, 10);
        wait_value *= 10;

        if (wait_value) daemon(1, 1);
        else
        {
        fprintf(stderr, "%s: bad time value '%s'\n", argv[0], argv[2]);
        return 1;
        }

        fprintf(stderr, "%s: daemon loop watching for '%s' (%ss max)\n", argv[0], argv[1], argv[2]);

        struct timespec loop_latency = { 0, 100 * 1000 * 1000 };
        struct stat file_status;

        while (stat(argv[1], &file_status) < 0 && wait_value-- > 0)
        nanosleep(&loop_latency, NULL);

        if (wait_value > 0)
        fprintf(stderr, "%s: file '%s' now exists\n", argv[0], argv[1]);

        else
        fprintf(stderr, "%s: timout waiting for file '%s'\n", argv[0], argv[1]);

        return 0;
}

ta0kira


All times are GMT -5. The time now is 04:07 PM.