linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "John T. Williams" <jowillia@vt.edu>
To: Wen Guangcheng <wen.guangcheng@cnt.sp.qnes.nec.co.jp>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: How to execute a process in background by exec functions?
Date: 08 Apr 2004 09:25:05 -0400	[thread overview]
Message-ID: <1081430703.29492.33.camel@Marx.fesnel.no-ip.org> (raw)
In-Reply-To: <002001c41d2c$18e27f80$de01a8c0@qnessmphibiki>

running a process in the background simply means not waiting for it to
complete.  So a parent process that wanted to make a child process run
ls in the ´background¡ would do something like this: (again this should
be treated as sudo code that just looks a lot like C)

_______________________CODE__________________
int
main (int argC, char** argV) {
int bg = 1;  // test condition for running in the background
int pid; 
int status;

if( (pid = fork()) ==  0 )   {
	/* child code starts here */
	execl(´/bin/ls¡, ´/bin/ls¡, 0 ); 

} else {
	/* this is the critical point, if you call wait, the parent 	process
stops until the child process returns, which would be fg 	behavior (at
least in a shell).  If the 
	parent doesn't call wait (or waitpid), then both parent and 	child will
run at the same time. (i.e. The child runs in the 	background */ 
	if( !bg) {  
		wait(&status);
	}
	/* Parent Code Starts here */
}

return 0;
}
_________________________END CODE_____________________

There are also considerations about input and output, and bringing the
process from the background to the forground, and sending it back into
the background, and so forth.  If your really interested in how this
works and how to address the more complex issues, I suggest that you
read the source code for GNU BASH, which is freely available (at this
point everyone must think I'm a spokes person for GNU)  or Read the
O'Reilly books "Learning the bash Shell, 2nd Edition", and "Practical C
Programming, 3rd Edition"



On Thu, 2004-04-08 at 01:41, Wen Guangcheng wrote:
> Hello All,
> I am trying to execute a process in background by exec functions.
> But it failed when I call the function of execv by
> execv("/home/wen/daemon &", NULL);
> The error is ENOENT(No such file or directory).  
> Would anyone tell me how to do it?
> Thanks in advance.
> 
> Best regards,
> 
> --Wen
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2004-04-08 13:25 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-06 20:44 c++ compiling problem. undefined reference to main srg
2004-04-06 20:55 ` Jad Saklawi
2004-04-08  5:41 ` How to execute a process in background by exec functions? Wen Guangcheng
2004-04-08  8:56   ` Glynn Clements
2004-04-08 13:25   ` John T. Williams [this message]
     [not found] <3AA03342E913FA4BA6D8BD0732BFC74B05530BA8@pdsmsx402.pd.intel.com>
2004-04-08  9:31 ` Wen Guangcheng
  -- strict thread matches above, loose matches on Subject: below --
2004-04-11 17:37 Jose Luis Alarcon Sanchez
2004-04-11 17:05 ` John T. Williams
2004-04-11 23:02   ` Glynn Clements
2004-04-11 19:23 Jose Luis Alarcon Sanchez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1081430703.29492.33.camel@Marx.fesnel.no-ip.org \
    --to=jowillia@vt.edu \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=wen.guangcheng@cnt.sp.qnes.nec.co.jp \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).