You are not logged in.

#1 2008-10-04 19:30:49

mfolnovic
Member
From: Zagreb, Croatia
Registered: 2008-01-03
Posts: 104

Writing a daemon

hello, I would like to write a daemon which monitors on mysql table and process all rows that he didn't already process, how would I do that ?
I'm kind of experienced in cpp, but only on competitions, so I don't know how could I connect to mysql ...
If there's easier way in some other programming language, then please tell me which and how, tnx.

Offline

#2 2008-10-04 22:23:51

Myav
Member
Registered: 2007-05-07
Posts: 58

Re: Writing a daemon

Hello, mfolnovic!

To solve your problem in C++ you need to read the topics of reference manual related to C/C++ API (which both can be used in your case): http://dev.mysql.com/doc/refman/5.1/en/ … -apis.html

I think there should be books describing those APIs in more obvious way than the official documentation. Or you can try to find some tutorials.

You'll find the excellent explanation how to make daemons using POSIX API in the following book: http://apuebook.com/. See the source code coming with book, to find the examples.

Take a glance at what Qt can do. There can be special classes for making daemons and for interaction with databases (but I'm not sure about that). Also if you want your program to have GUI Qt can be the best choice for you.

Here is the documentation to the last release of Qt: http://doc.trolltech.com/4.4/index.html

But I beleive that C/C++ aren't the best languages to use in your case tongue Your problem is where Python and Ruby give a taste of one's quality.

Offline

#3 2008-10-05 11:24:32

madhatter
Member
From: Freudenstadt, Germany
Registered: 2004-09-01
Posts: 59

Re: Writing a daemon

Here's a python daemon script: http://hathawaymix.org/Software/Sketches/daemon.py
It drops privileges, logs, etc., a good starting point for writing your own daemon.
MySQL in Python is pretty easy, there should be a lot of resources available.

Offline

#4 2008-10-05 12:32:05

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,000
Website

Re: Writing a daemon

mfolnovic wrote:

If there's easier way in some other programming language, then please tell me which and how, tnx.

Uhm... yes! php, perl, python, ruby, ... you could even do it in sh cool


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

#5 2008-10-05 14:07:23

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Writing a daemon

You can make a daemon-like script in PHP which is very easy to use, and I can help you if you want - I've never really gotten into databases though so it'd be a learning experience for both of us tongue

-dav7

Last edited by dav7 (2008-10-05 14:07:42)


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#6 2008-10-05 14:45:10

mfolnovic
Member
From: Zagreb, Croatia
Registered: 2008-01-03
Posts: 104

Re: Writing a daemon

yes, finaly, I decided to make it in php, and made it, and it works smile
but I don't know how to make it daemon, so it runs every 15 s ...

Last edited by mfolnovic (2008-10-05 14:56:54)

Offline

#7 2008-10-05 16:06:18

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Writing a daemon

Ah, you want pcntl_fork, which is a lot easier to use than it might look.

You simply say $pid = pcntl_fork(), then check $pid. If it's -1, the fork failed for some reason (so you probably want to die() or something to end the whole script), if the $pid is not -1 and not 0 you're the parent process and you want to die() because that's how you become a daemon (you create a child process, then kill the parent process, and the child process continues to execute but detached from the terminal that created the parent process), and if the PID is 0, we are the child process, and we want to put all our code here.

Code example modified from php.net:

<?php

$pid = pcntl_fork();
if ($pid == -1) {
     die("fork() call asploded!\n");
} else if ($pid) {
     // we are the parent
     print "fork()ed successfully.\n");
     die();
}

// We'd only leave the if/then block if we're the child
// Put your existing code here

?>

I didn't test the above code however, so see how it goes. Beware that since it should work as a daemon, you're going to want to check your system for stray copies of php lurking around if/as you test it since it'll be forking itself and remaining resident like a daemon should!

-dav7

Last edited by dav7 (2008-10-05 16:08:03)


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#8 2008-10-05 16:16:59

mfolnovic
Member
From: Zagreb, Croatia
Registered: 2008-01-03
Posts: 104

Re: Writing a daemon

dav7 wrote:

Ah, you want pcntl_fork, which is a lot easier to use than it might look.

You simply say $pid = pcntl_fork(), then check $pid. If it's -1, the fork failed for some reason (so you probably want to die() or something to end the whole script), if the $pid is not -1 and not 0 you're the parent process and you want to die() because that's how you become a daemon (you create a child process, then kill the parent process, and the child process continues to execute but detached from the terminal that created the parent process), and if the PID is 0, we are the child process, and we want to put all our code here.

Code example modified from php.net:

<?php

$pid = pcntl_fork();
if ($pid == -1) {
     die("fork() call asploded!\n");
} else if ($pid) {
     // we are the parent
     print "fork()ed successfully.\n");
     die();
}

// We'd only leave the if/then block if we're the child
// Put your existing code here

?>

I didn't test the above code however, so see how it goes. Beware that since it should work as a daemon, you're going to want to check your system for stray copies of php lurking around if/as you test it since it'll be forking itself and remaining resident like a daemon should!

-dav7

so I just paste that code, put my code down, run it, and it should work as daemon?

Offline

#9 2008-10-05 17:09:45

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Writing a daemon

It should. It might not, but it definately should.

-dav7


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#10 2008-10-05 17:17:22

mfolnovic
Member
From: Zagreb, Croatia
Registered: 2008-01-03
Posts: 104

Re: Writing a daemon

how can I check is it running as daemon?

Offline

#11 2008-10-05 17:43:16

mfolnovic
Member
From: Zagreb, Croatia
Registered: 2008-01-03
Posts: 104

Re: Writing a daemon

ok, done it, tnx everyone smile

Offline

#12 2008-10-05 17:46:15

dav7
Member
From: Australia
Registered: 2008-02-08
Posts: 674

Re: Writing a daemon

Awesome big_smile


Windows was made for looking at success from a distance through a wall of oversimplicity. Linux removes the wall, so you can just walk up to success and make it your own.
--
Reinventing the wheel is fun. You get to redefine pi.

Offline

#13 2008-10-05 17:51:07

Dieter@be
Forum Fellow
From: Belgium
Registered: 2006-11-05
Posts: 2,000
Website

Re: Writing a daemon

There are some more things you should/probably want to do in php ( set_time_limit (0);, declare (ticks=1);, binding signals with  pcntl_signal(),  posix_setsid (); in the child, write a pid file etc)


< Daenyth> and he works prolifically
4 8 15 16 23 42

Offline

Board footer

Powered by FluxBB