Question

I have a program I wrote in C that starts mplayer with a pipe, I then control mplayer through that pipe. It seems that at times when I send a command over the pipe it crashes. The following are the relevant parts of my code and what I have done to debug it.

The original code:

FILE *mplayer_pipe;
char temp[256];

bzero(temp,256);
sprintf(temp,"mplayer -quiet -slave video.mkv > mplayer.log");
mplayer_pipe = popen((char*)temp, "w");

/* watch some values to see what to do next */

/* if I see a value that means I should stop mplayer I do this */

fputs("stop\n",mplayer_pipe);
fflush(mplayer_pipe);
pclose(mplayer_pipe);

I ran into the problem that if mplayer finished, thus making it so nothing is at the other end of the pipe and I still pipe over the stop my program crashes. To solve this I changed it to the following:

FILE *mplayer_pipe;
char temp[256];

bzero(temp,256);
sprintf(temp,"mplayer -quiet -slave video.mkv > mplayer.log");
mplayer_pipe = popen((char*)temp, "w");

/* watch some values to see what to do next */

/* if I see a value that means I should stop mplayer I do this */

FILE *mplayer_status;
int mplayer_pid = 0;
char status_read_char[256];
bzero(status_read_char,256);

mplayer_status = popen("pidof -s mplayer", "r");
fgets(status_read_char, 6, mplayer_status);
pclose(status_read_char);
mplayer_pid = atoi(status_read_char);

if(mplayer_pid > 0) {
fputs("stop\n",mplayer_pipe);
fflush(mplayer_pipe);
pclose(mplayer_pipe);
}
else {
pclose(mplayer_pipe);
}

This fix worked great for me and it stop crashing.

Then I let other use the program and I starting getting crashing every once in a while. As I could not get my program to crash myself I change it to the following to see where the program was crashing.

FILE *mplayer_pipe;
char temp[256];

bzero(temp,256);
sprintf(temp,"mplayer -quiet -slave video.mkv > mplayer.log");
mplayer_pipe = popen((char*)temp, "w");
printf("command: mplayer -quiet -slave video.mkv > mplayer.log");

/* watch some values to see what to do next */

/* if I see a value that means I should stop mplayer I do this */

FILE *mplayer_status;
int mplayer_pid = 0;
char status_read_char[256];
bzero(status_read_char,256);

mplayer_status = popen("pidof -s mplayer", "r");
fgets(status_read_char, 6, mplayer_status);
pclose(status_read_char);
mplayer_pid = atoi(status_read_char);
printf("mplayer_status: command: pidof -s mplayer. Got %s, value %d.",status_read_char,mplayer_pid);

if(mplayer_pid > 0) {
fputs("stop\n",mplayer_pipe);
fflush(mplayer_pipe);
printf("command: stop.");
pclose(mplayer_pipe);
}
else {
pclose(mplayer_pipe);
}

The last thing I got before my program crashed the next time was:

mplayer_status: command: pidof -s mplayer. Got 27323, value 27323.

To me this indicates that it is crashing while piping over the stop command to mplayer.

Does anyone know what is going wrong here? And any ideas on how to prevent it from crashing?

Was it helpful?

Solution

Instead of sending the stop command to mplayer, why don't you just send it SIGTERM and close the pipe? The mplayer will detect the signal and stop by itself.

kill(mplayer_pid, SIGTERM);
pclose(mplayer_pipe);

Don't forget to include sys/types.h and signal.h in your program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top