LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-07-2007, 07:59 PM   #1
RavenLX
Member
 
Registered: Oct 2004
Posts: 88

Rep: Reputation: 15
Question 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...

Any helpful hints and tips would be appreciated.
 
Old 08-08-2007, 12:54 PM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,503

Rep: Reputation: 174Reputation: 174
"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
 
Old 08-08-2007, 01:23 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 08-08-2007 at 01:30 PM.
 
Old 08-08-2007, 07:40 PM   #4
RavenLX
Member
 
Registered: Oct 2004
Posts: 88

Original Poster
Rep: Reputation: 15
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.
 
Old 08-08-2007, 08:25 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
  


Reply

Tags
daemon, gcc, linux, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regarding server load gimmereeson Linux - Networking 1 01-29-2007 10:52 AM
x server will not load StealthZyxel Linux - Laptop and Netbook 11 08-01-2006 02:18 PM
Websites load slow, but once they load, any links inside them load fast smurcoch Mandriva 3 02-06-2006 12:47 AM
Cant load X Server NewToLinuxOS Suse/Novell 1 03-14-2005 11:37 PM
X server won't load in RH 9.0 quijanom Linux - Software 5 08-22-2003 01:39 PM


All times are GMT -5. The time now is 11:01 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration