 |
 |
 |
 |
| Programming & Packaging A place to discuss programming and packaging. |

22nd May 2009, 06:12 AM
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 109

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

22nd May 2009, 09:48 AM
|
 |
"Fixed" by (vague) request
|
|
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950

|
|
|
|

22nd May 2009, 03:36 PM
|
 |
Registered User
|
|
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,346

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

23rd May 2009, 12:29 PM
|
|
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
|

25th May 2009, 04:43 PM
|
|
Registered User
|
|
Join Date: Mar 2009
Posts: 109

|
|
Quote:
Originally Posted by giulix
|
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?
|

26th May 2009, 11:21 AM
|
 |
"Fixed" by (vague) request
|
|
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950

|
|
One option is to use xinetd
|

26th May 2009, 10:39 PM
|
|
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.
|

27th May 2009, 10:42 AM
|
 |
"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
|

27th May 2009, 04:55 PM
|
|
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 18:04 (Wednesday, 19-06-2013)
|
|
 |
 |
 |
 |
|
|