believe it or not but this is my first c program (i've worked with java, C#, php though) I am trying to make a daemon that checks if mplayer is running(it's for a projection room) and if it is not then to run mplayer with a file.. So far it's not working and I don't know why
Help and comments would be appreciated
#include <sys/types.h> /* include this before any other sys headers */
#include <sys/wait.h> /* header for waitpid() and various macros */
#include <signal.h> /* header for signal functions */
#include <stdio.h> /* header for fprintf() */
#include <unistd.h> /* header for fork() */
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
int main(void)
{ pid_t pid;
while(1){
int rc;
rc<<system("ps ux | awk '/mplayer/ && !/awk/ {print $2}'");
rc = WEXITSTATUS(rc); /* Check if mplayer is running */
if(rc >= 0 ){
pid = fork();
if(pid>=0){
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
}
else
{/*fork error*/}
}
return (0);
}
void ChildProcess(void)
{
execvp("mplayer ~/test.avi",NULL);
exit(0);
}
void ParentProcess(void)
{
sleep(2);
return;
}
Not exactly what a daemon is for IMO, but that's not for here to discuss
What exactly isn't working. I found a few things that might cause problems, but that could or couldn't be what you're looking for.
#include <sys/types.h> /* include this before any other sys headers */
#include <sys/wait.h> /* header for waitpid() and various macros */
#include <signal.h> /* header for signal functions */
#include <stdio.h> /* header for fprintf() */
#include <unistd.h> /* header for fork() */
void ChildProcess(void); /* child process prototype */
void ParentProcess(void); /* parent process prototype */
int main(void)
{ pid_t pid;
while(1){
int rc;
// I don't think this is standard C behaviour, execpt if you want to
// bit-shift left an uninitialized variable
rc<<system("ps ux | awk '/mplayer/ && !/awk/ {print $2}'");
// Where did that come from?
rc = WEXITSTATUS(rc); /* Check if mplayer is running */
if(rc >= 0 ){
pid = fork();
if(pid>=0){
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
}
else
{/*fork error*/}
}
return (0);
}
void ChildProcess(void)
{
execvp("mplayer ~/test.avi",NULL);
exit(0);
}
void ParentProcess(void)
{
sleep(2);
return;
}
As a guideline, a daemon usually has this workflow:
initialize
pid=fork
if(pid==0)
exit //parent
else
while(1)
do something
Your code, OTOH, never goes into background, and, in case mplayer keeps on crashing, will waste quite some time on process creation.
In your ChildProcess() function you could replace the execvp/exit combination with a simple exec, as this will replace the current process image (that of the child) with that of the program you're specifying.
while(1){
int rc;
rc=system("ps ux | awk '/mplayer/ && !/awk/ {print $2}'");
rc = WEXITSTATUS(rc); /* Check if mplayer is running */
if(rc > 0 ){
printf("i was here");
printf(rc);
pid = fork();
if(pid>=0){
if (pid == 0)
ChildProcess();
else
ParentProcess();
}
}
It never gets to "i was here"
i'm trying to get the process ID so I can check if it is running then relaunch it...right now it just doesn't launch anything. and for the daemon part, I am aware that it's not reall daemonesk but I want to be able to stop it