LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-23-2013, 11:59 AM   #1
tbillion
LQ Newbie
 
Registered: Mar 2013
Distribution: Linux Mint, OpenSUSE, Arch, Debian
Posts: 11

Rep: Reputation: Disabled
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.
 
Old 03-23-2013, 04:14 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 (pre-systemd)
Posts: 1,773

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
system does call fork and exec, so that's not the problem.
Does this work if you run mplayer from the command line?
 
Old 03-23-2013, 04:19 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
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).

Last edited by ntubski; 03-23-2013 at 04:20 PM. Reason: typo
 
1 members found this post helpful.
Old 03-24-2013, 07:29 PM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
Old 03-27-2013, 12:36 PM   #5
tbillion
LQ Newbie
 
Registered: Mar 2013
Distribution: Linux Mint, OpenSUSE, Arch, Debian
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-27-2013, 01:59 PM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 2,541

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by tbillion View 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.
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.
 
Old 03-27-2013, 04:06 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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.
 
  


Reply

Tags
c++, console, fun, mplayer, sdl


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP Please :) Looking for a couple pointers, SDL, C++, Mplayer? tbillion Linux - Software 12 03-28-2013 06:04 AM
fork() exec() or system() kalleanka Programming 4 09-18-2007 05:16 AM
Installing new nVidia card on my system, need some pointers. waffles Linux - Hardware 3 11-16-2006 01:33 AM
Fork System call bangoram Linux - General 8 07-16-2006 03:24 AM
X Error with system()/fork() aaa Programming 3 08-18-2004 06:05 PM


All times are GMT -5. The time now is 03:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration