Fedora Linux Support Community & Resources Center
  #1  
Old 22nd May 2009, 06:12 AM
soxs060389 Offline
Registered User
 
Join Date: Mar 2009
Posts: 109
Question C++ daemon and talking to daemon

Hi

I am writing a private media player to get used to C++ and get some actual tings in shape I had ind years ago.

So my initial plan was, to run <dumbplayer> (my media player name) as daemon for an infinite time and send that process info via linux/unix bash commandline
Code:
$ dumplayer -playthissong /path/to/song.ext
the deamon itself should be called through just
Code:
$ dumplayer
So, the point where I require some help is, how can I make the first process run as an infinite daemon ( without doing an while(1){] loop) and without acquiring 100% cpu, abd still having the possibility to interfere via further commands?

Thx a lot
Sincerely
soxs

Note:
I allready did some research and read some pages about fork(), but the usage didn't come clear how I can later use that process ID again and read with later calls of <dumplayer -option ...> from bash/CLI
Reply With Quote
  #2  
Old 22nd May 2009, 09:48 AM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
Have a look at sockets
Reply With Quote
  #3  
Old 22nd May 2009, 03:36 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,696
First, when creating a daemon, look at "man 3 daemon" library call.

This causes your process (well actually a child copy) to remove itself from the current terminal session and close stdin, stdout ,stderr.

All daemons should be coded to respond to (service) external events. It's entirely up to YOU to arrange for your process to wait on these events without "hot spinning".

Normally a process is scheduled (given cpu time) periodically whenever it's in a "runnable" state. It's your job to make your process not be in a runnable state until the external event occurs.

One common mechanism is the use the "man 2 select" syscall to wait until there is data available.
The "man 2 poll" syscall is also used.
You could setup callback routines (in an X11 environment for example) and then let the main thread sleep forever.
In some cases you can wait for signals with "man 2 rt_sigtimedwait" or "man 2 pause".

All of the above tell the kernel to mark your process to not schedule (not be runnable) until the event has occurred.


[edit]
After scanning your post, perhaps your intention is to have the daemon "dumplayer" wait until you specify a file to play (how?) or alternatively to wait for the player copy to exit. The first sounds like an X11 callback event perhaps. The second ase perhaps you want to wait on a SIGCHLD (death of child pocess) event.
__________________
None are more hopelessly enslaved than those who falsely believe they are free.
Johann Wolfgang von Goethe

Last edited by stevea; 22nd May 2009 at 03:41 PM.
Reply With Quote
  #4  
Old 23rd May 2009, 12:29 PM
soxs060389 Offline
Registered User
 
Join Date: Mar 2009
Posts: 109
There is no gui part planed until furter notice, and thus I'd like to do it this way
Code:
dumplayer -play /path/to/song.ext #play this song, and if dumplayer deamon is not running, start it just ahead
dumplayer -start #just start the deamon an play nothing, waiting for further 'dumbplayer -play ... ' operatiorns
Reply With Quote
  #5  
Old 25th May 2009, 04:43 PM
soxs060389 Offline
Registered User
 
Join Date: Mar 2009
Posts: 109
Quote:
Originally Posted by giulix View Post
Have a look at sockets
If I use sockets, then I need somehwo to determine if there is allready a processs running, so I can start the deamon process, jsut in case there is none yet. Any proposals?
Reply With Quote
  #6  
Old 26th May 2009, 11:21 AM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
One option is to use xinetd
Reply With Quote
  #7  
Old 26th May 2009, 10:39 PM
soxs060389 Offline
Registered User
 
Join Date: Mar 2009
Posts: 109
Code:
int StartDaemon() {
    LimitOne limiter; //this creates a lock, so only one process can run at a time
    if (limiter.successfullyLocked()) { //checks wether lock was successfull
        ServerDaemon sound(GLOBAL_DEFAULT_PORT); //run a socket server with some extras 

        if (daemon(0, 0) == -1) {
            cout << "Evil Exit Detected" << endl;
            exit(1);
        }
        cout << "AFTER" << endl; // this never happens, as daemon(0,0) seems to cancel all remaining actions, this is 100% ebil
        sound.run(); //this ash to be run and is a cold spinning (infinite) wait for connects until explicit closeoure
    }
}
Any solution, so I can make it a deamon but still run the remaining actions


why does daemon(1,1) make such a difference to daemon(0,0)
with daemon (1,1) it works just fine, wtf? Are the german manpages wrong or .. well I must be too stupide to red them..

Last edited by soxs060389; 26th May 2009 at 10:45 PM.
Reply With Quote
  #8  
Old 27th May 2009, 10:42 AM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
Not sure what you mean, but the daemon man page says that, after fork()ing, the parent exits. With daemon(0, 0) the child redirects standard output to /dev/null, so output to a file instead of using cout
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char *argv[]) {

    FILE *f = NULL;

    if((f = fopen("log.txt", "w")) != NULL)

        if(daemon(0, 0) >= 0)

            fprintf(f, "%s\n", "Hello");

        else

            fprintf(f, "%s\n", "Error detaching terminal");
    else

        printf("%s\n", "Cannot open log file");

    return 0;
}

Last edited by giulix; 27th May 2009 at 11:07 AM. Reason: Added C example
Reply With Quote
  #9  
Old 27th May 2009, 04:55 PM
soxs060389 Offline
Registered User
 
Join Date: Mar 2009
Posts: 109
Ok, thx, I thought it was supposed as long as the terminal keeeps running t print info, and not do direct forwarding to /dev/null
Reply With Quote
Reply

Tags
c++, cli, deamon

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Daemon FC5 Herbert Taylor EOL (End Of Life) Versions 0 8th September 2006 07:42 PM
Avahi daemon and HAL daemon lsatenstein Fedora Core 5 - Dev 18 14th February 2006 01:37 AM
Can i get some I/O status of particular daemon?? diylinux Using Fedora 0 6th May 2005 02:40 AM
HAL daemon k5knt Using Fedora 4 16th March 2005 04:30 AM


Current GMT-time: 16:57 (Thursday, 24-07-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat