LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-06-2013, 09:08 AM   #1
henryyao
LQ Newbie
 
Registered: Jul 2013
Posts: 23

Rep: Reputation: 0
why system(cmd) function need to set the command string with the & background paramet


I'm going to run my QT program on a Linux-arm embedded device.

I tried to use system(cmd) function to run linux shell cmd in QT program.

cmd would be a audio playing command "aplay -N sound.wav"

If cmd is as above, there will be no sound come out of my linux device, and the process of the QT program will in the T state (traced or stopped).

If cmd is set as "aplay -N sound.wav &", things will work just fine.

My question is what caused that, why does the "&" background parameter matter in this case.

Thanks.
 
Old 09-06-2013, 04:45 PM   #2
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,711

Rep: Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279Reputation: 1279
don't use system.

The system library function is designed to wait for the command to complete before it returns.

The way the system library function works is to:

1. fork a child process (the parent process waits for the child to exit)
2. the child process then uses the exec command to execute the shell to interpret the command.
3. the shell command parses and provides parameter expansion to the command, and sets up a parameter list
4. the shell then forks a child process (the parent NORMALLY waits for the process to terminate unless it is directed to not wait - the & on the command).
5. When this second child exits the exit status is used by the shell to set its exit status.
6. The exit status of the shell process (the child of step 1) is then used to create the return status from the system function.

The side effect of step 1, and step 4/5 is that the child process is cleaned up and the resources used released. Putting the & on the command only does half the job - the command process itself is not cleaned up until after the parent (in step 1) exits, where the shell that started that command does the clean up.

This is not a secure function - environment variables come into play that can affect what command is actually executed, and not the one you necessarily want. Even the shell used by the system command changes depending on environment variables, and this can cause additional side effects.

One of the side effects is how stdin/stdout/stderr are handled. The command may/may not have stdin/out redirected (it is a shell command after all) and different shells use slightly different syntax (especially when stderr is being redirected - look up the difference between cshell and bash).

For this reason it is usually better to directly use the fork system call, and create the exact parameter list you want. and decide what you want to do for the clean up (either handle it, or wait for its parent to handle it). It also turns out to be much faster in execution. (see SIGCHLD for how to catch and handle these exits).

Last edited by jpollard; 09-06-2013 at 04:52 PM. Reason: trying to clarify... but maybe failing?
 
1 members found this post helpful.
  


Reply


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
Can you put the output of a system function to a string baronobeefdip Programming 4 09-28-2012 04:02 AM
Command or function in C to make the process run in background sowpra Linux - Newbie 4 05-02-2012 01:56 AM
[SOLVED] Problem with a function having a background process inside a command substitution rylphs Linux - Newbie 2 10-05-2010 11:36 AM
poepn or system function call executed by a background process kshkid Programming 1 08-24-2006 04:04 AM
alias or function in .bashrc, which sends command to background ngomong Linux - General 1 04-23-2002 10:50 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:04 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration