LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   HELP Please :) Looking for a couple pointers, Fork(), System(), C++ (http://www.linuxquestions.org/questions/programming-9/help-please-looking-for-a-couple-pointers-fork-system-c-4175455270/)

tbillion 03-23-2013 11:59 AM

HELP Please :) Looking for a couple pointers, Fork(), System(), C++
 
ok my question is complicatedly simple :)

i have a platform console only (no X) i run an SDL/C++ app on it, i am trying to get the app to execute mplayer and when i execute

Code:


    char buffer [50];
    sprintf(buffer,"mplayer -ao sdl -vo sdl -vf scale=%d:%d mv1.mp4",TRUE_Screen_X,TRUE_Screen_Y);
    //system call
    system(buffer);

my whole system hangs. and worst part i dont even get to watch the movie :)

Do i need to call fork to do this or something like that?

how id like it to work is id like;

open my program
my program opens and then opens mplayer
when mplayer exits go back to my program

how might i achieve this i have googled the crap out of it and found no real direct answer.

smallpond 03-23-2013 04:14 PM

system does call fork and exec, so that's not the problem.
Does this work if you run mplayer from the command line?

ntubski 03-23-2013 04:19 PM

Code:

    char buffer [50];
    sprintf(buffer,"mplayer -ao sdl -vo sdl -vf scale=%d:%d mv1.mp4",TRUE_Screen_X,TRUE_Screen_Y);

You are probably overflowing buffer (assuming resolution 1280x1024):
Code:

0        1        2        3        4        5
1234567890123456789012345678901234567890123456789012
mplayer -ao sdl -vo sdl -vf scale=1280:1024 mv1.mp4\0

That's 52 characters (including terminating NUL). Use snprintf() to avoid memory corruption and check the return value to see if you've run out of space. Or, use asprintf() which allocates the correct amount of space for you (this is specific to glibc).

theNbomr 03-24-2013 07:29 PM

system() always launches a shell, which isn't necessary for what you're doing. More efficient CPU/memory/speed wise to use fork() + exec(). system() is simpler to use, until you learn a little about exec(). fork() + exec() also provide you with more options for communicating between the parent and child, such as piping data to the child's standard input stream, which can be handy.

--- rod.

tbillion 03-27-2013 12:36 PM

thanks for the responses guys/gals(?)

Quote:

system does call fork and exec, so that's not the problem.
Does this work if you run mplayer from the command line?
yes the command does work on the command line with mplayer, which i do have installed but it messes up with mplayer2, which i discovered while trying to get it to work. i had a nother topic on here about what to use, i used mplayer in a script version and i like mplaer cuz well its pretty simple, and works. my other post

as for the overflow of the buffer that really wasnt causgin a noticable problem but thanks for the catch of the error, used an example from the net, i think it was just truncating the string. i didnt get a segment fault or even a warning.

Quote:

system() always launches a shell, which isn't necessary for what you're doing. More efficient CPU/memory/speed wise to use fork() + exec(). system() is simpler to use, until you learn a little about exec(). fork() + exec() also provide you with more options for communicating between the parent and child, such as piping data to the child's standard input stream, which can be handy.
why is it not necessary for it to launch its own shell? (other than memory and spu time and all that.) doesnt exec replace the process thats running with the process that you call, that is the only reason i havent went and learned that route extensivly im not trying to replace the "menu" window, as in after mplayer is done doing the good ness id like to go back into execution where it left off. i have played with fork and made and exaple that works with x11, but likt i say where this program is going there is no x11.

i originally wanted to embed the out from mplayer in my own programmer but its pretty much not designed to do that, id have to use libavc directly or use ffmpeg to move the data around. not really the ride im in for for my 5 year old dughter. :)

ntubski 03-27-2013 01:59 PM

Quote:

Originally Posted by tbillion (Post 4919974)
as for the overflow of the buffer that really wasnt causgin a noticable problem but thanks for the catch of the error, used an example from the net, i think it was just truncating the string. i didnt get a segment fault or even a warning.

snprintf() would truncate, sprintf() will cause buffer overflow after which your program might do just about anything, for example maybe the system() function never gets called...

Quote:

why is it not necessary for it to launch its own shell? (other than memory and spu time and all that.) doesnt exec replace the process thats running with the process that you call,
After fork() you have 2 processes, so the exec() replaces the second (child) process. You are only calling mplayer, not running a shell command so you don't need to run a shell.

theNbomr 03-27-2013 04:06 PM

If you want your parent process to continue after the child mplayer process has terminated, then you must use either fork()+exec(), or use system(). The shell launched by system() will be a child of your process, and the parent to the mplayer process, and will do nothing except consume resources. If you want your process to be replaced by mplayer, then you need only call exec() to launch it.
--- rod.


All times are GMT -5. The time now is 09:46 AM.