linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* How to execute a process in background by exec functions?
  2004-04-06 20:44 c++ compiling problem. undefined reference to main srg
@ 2004-04-08  5:41 ` Wen Guangcheng
  2004-04-08  8:56   ` Glynn Clements
  2004-04-08 13:25   ` John T. Williams
  0 siblings, 2 replies; 8+ messages in thread
From: Wen Guangcheng @ 2004-04-08  5:41 UTC (permalink / raw)
  To: linux-c-programming

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to execute a process in background by exec functions?
  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
  1 sibling, 0 replies; 8+ messages in thread
From: Glynn Clements @ 2004-04-08  8:56 UTC (permalink / raw)
  To: Wen Guangcheng; +Cc: linux-c-programming


Wen Guangcheng wrote:

> 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).  

That's because you don't have an executable called "daemon &" in
/home/wen.

> Would anyone tell me how to do it?

For the record, system() works roughly like this:

	int system(const char *command)
	{
		pid_t pid;
		int status;
	
		pid = fork();
	
		if (pid < 0)
			return -1;
	
		if (pid == 0)
		{
			execl("/bin/sh", "sh", "-c", command, 0);
			_exit(127);
		}
	
		if (waitpid(pid, &status, 0) < 0)
			return -1;
	
		return status;
	}

[The actual system() function also changes the signal handling and
checks for errors; for simplicity, these have been omitted from the
above code.]

First, it calls fork() to create a new process. The child process
calls one of the exec*() functions to execute the shell, with the
command as an argument (note: the various exec*() functions shouldn't
return; if they succeed, the specified program is run in place of your
program). The parent process calls one of the wait*() functions to
wait for the child process to terminate.

If you want to run a specific program directly (rather than via the
shell), you would just run that program in place of /bin/sh, e.g.:

	execl("/home/wen/daemon", "daemon", NULL);

Note that you have to specify argv[0] explicitly.

If you want that program to run in the background, the solution is
simply to omit the call to wait (or waitpid, etc) from the parent.

Also, note that features such as command pipelines, redirection, use
of the "~" syntax for the home directory etc are all provided by the
shell. You can't use shell syntax (e.g. "prog1 | prog2",
"prog <infile >outfile", "prog ~/file" etc) in the strings which are
passed to the exec*() functions unless you are actually executing the
shell (as system() does).

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to execute a process in background by exec functions?
       [not found] <3AA03342E913FA4BA6D8BD0732BFC74B05530BA8@pdsmsx402.pd.intel.com>
@ 2004-04-08  9:31 ` Wen Guangcheng
  0 siblings, 0 replies; 8+ messages in thread
From: Wen Guangcheng @ 2004-04-08  9:31 UTC (permalink / raw)
  To: Zhang, Yanmin, linux-c-programming

Hi,
Thanks a lot for your help.
Yes, I have tired this way. The daemon started, but I hope its PPID
is 1. We can do it by calling system("/home/wen/daemon &").
How can I do it by fork&exec way?

Best regards,

--Wen

----- Original Message ----- 
From: "Zhang, Yanmin" <yanmin.zhang@intel.com>
To: "Wen Guangcheng" <wen.guangcheng@cnt.sp.qnes.nec.co.jp>;
<linux-c-programming@vger.kernel.org>
Sent: Thursday, April 08, 2004 3:00 PM
Subject: RE: How to execute a process in background by exec functions?


> It doesn't work in this approach. Try this:
>
> pid=fork();
> if(pid == 0) {
> execv("/home/wen/daemon", NULL);
> exit(-1);
> }
>
> ... do parent process.
>
> Yanmin
>
>
> >-----Original Message-----
> >From: linux-c-programming-owner@vger.kernel.org [mailto:linux-c-
> >programming-owner@vger.kernel.org] On Behalf Of Wen Guangcheng
> >Sent: 2004Äê4ÔÂ8ÈĠ 13:41
> >To: linux-c-programming@vger.kernel.org
> >Subject: How to execute a process in background by exec functions?
> >
> >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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to execute a process in background by exec functions?
  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
  1 sibling, 0 replies; 8+ messages in thread
From: John T. Williams @ 2004-04-08 13:25 UTC (permalink / raw)
  To: Wen Guangcheng; +Cc: linux-c-programming

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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to execute a process in background by exec functions?
  2004-04-11 17:37 Jose Luis Alarcon Sanchez
@ 2004-04-11 17:05 ` John T. Williams
  2004-04-11 23:02   ` Glynn Clements
  0 siblings, 1 reply; 8+ messages in thread
From: John T. Williams @ 2004-04-11 17:05 UTC (permalink / raw)
  To: Jose Luis Alarcon Sanchez; +Cc: linux-c-programming

The single quotes need to be double quotes 
so the call should look like 

execl("/bin/ls", "ls", 0);  





On Sun, 2004-04-11 at 13:37, Jose Luis Alarcon Sanchez wrote:
> ---- Begin Original Message ----
> 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"
> ---- End Original Message ----
> 
> 
>   Hi John.
> 
>   I am trying compile the code you paste in your message,
> but i get an error with the line
> 
> execl(´/bin/ls¡, ´/bin/ls¡, 0 );
> 
> Process_Exec.c:15:11: warning: character constant too long for its type
> Process_Exec.c: In function `main':
> Process_Exec.c:15: error: `bin' undeclared (first use in this function)
> Process_Exec.c:15: error: (Each undeclared identifier is reported only
> once
> Process_Exec.c:15: error: for each function it appears in.)
> Process_Exec.c:15: error: stray '\241' in program
> Process_Exec.c:15: error: `ls' undeclared (first use in this function) 
> 
>   I'm afraid that it is for the character before the '/',
> i can't get it with any key. But i am not sure, i am a very 
> newbie C programmer.
> 
>   I hope you can help me.
> 
>   Thanks, very much, in advance.
> 
>   Regards.
> 
>   Jose.
> 
> 
> http://www.lordofunix.org
> 
> FreeBSD RELEASE 5.2.
> Mandrake Linux 9.2 Kernel 2.6.2 XFS.
> Registered BSD User 51101.
> Registered Linux User #213309.
> Memories..... You are talking about memories. 
> Rick Deckard. Blade Runner.
> 
> 
> Get your Free E-mail at http://bunniwerks.zzn.com
> ___________________________________________________________
> Get your own Web-based E-mail Service at http://www.zzn.com
> -
> 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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to execute a process in background by exec functions?
@ 2004-04-11 17:37 Jose Luis Alarcon Sanchez
  2004-04-11 17:05 ` John T. Williams
  0 siblings, 1 reply; 8+ messages in thread
From: Jose Luis Alarcon Sanchez @ 2004-04-11 17:37 UTC (permalink / raw)
  To: linux-c-programming; +Cc: jowillia

---- Begin Original Message ----
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"
---- End Original Message ----


  Hi John.

  I am trying compile the code you paste in your message,
but i get an error with the line

execl(´/bin/ls¡, ´/bin/ls¡, 0 );

Process_Exec.c:15:11: warning: character constant too long for its type
Process_Exec.c: In function `main':
Process_Exec.c:15: error: `bin' undeclared (first use in this function)
Process_Exec.c:15: error: (Each undeclared identifier is reported only
once
Process_Exec.c:15: error: for each function it appears in.)
Process_Exec.c:15: error: stray '\241' in program
Process_Exec.c:15: error: `ls' undeclared (first use in this function) 

  I'm afraid that it is for the character before the '/',
i can't get it with any key. But i am not sure, i am a very 
newbie C programmer.

  I hope you can help me.

  Thanks, very much, in advance.

  Regards.

  Jose.


http://www.lordofunix.org

FreeBSD RELEASE 5.2.
Mandrake Linux 9.2 Kernel 2.6.2 XFS.
Registered BSD User 51101.
Registered Linux User #213309.
Memories..... You are talking about memories. 
Rick Deckard. Blade Runner.


Get your Free E-mail at http://bunniwerks.zzn.com
___________________________________________________________
Get your own Web-based E-mail Service at http://www.zzn.com
-
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

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to execute a process in background by exec functions?
@ 2004-04-11 19:23 Jose Luis Alarcon Sanchez
  0 siblings, 0 replies; 8+ messages in thread
From: Jose Luis Alarcon Sanchez @ 2004-04-11 19:23 UTC (permalink / raw)
  To: jowillia; +Cc: linux-c-programming

---- Begin Original Message ----
The single quotes need to be double quotes 
so the call should look like 

execl("/bin/ls", "ls", 0);  
---- End Original Message ----


  Now it works fine!

  Thanks you very much, John. You're very kind.

  Regards.

  Jose.


http://www.lordofunix.org

Registered Linux User #213309.
Memories..... You are talking about memories. 
Rick Deckard. Blade Runner.


Get your Free E-mail at http://bunniwerks.zzn.com
___________________________________________________________
Get your own Web-based E-mail Service at http://www.zzn.com

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: How to execute a process in background by exec functions?
  2004-04-11 17:05 ` John T. Williams
@ 2004-04-11 23:02   ` Glynn Clements
  0 siblings, 0 replies; 8+ messages in thread
From: Glynn Clements @ 2004-04-11 23:02 UTC (permalink / raw)
  To: linux-c-programming; +Cc: Jose Luis Alarcon Sanchez


John T. Williams wrote:

> The single quotes need to be double quotes 
> so the call should look like 
> 
> execl("/bin/ls", "ls", 0);  

Actually:

	execl("/bin/ls", "ls", (void *) 0);

The difference matters on systems where sizeof(void*) != sizeof(int).

-- 
Glynn Clements <glynn.clements@virgin.net>

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2004-04-11 23:02 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <3AA03342E913FA4BA6D8BD0732BFC74B05530BA8@pdsmsx402.pd.intel.com>
2004-04-08  9:31 ` How to execute a process in background by exec functions? Wen Guangcheng
2004-04-11 19:23 Jose Luis Alarcon Sanchez
  -- 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-06 20:44 c++ compiling problem. undefined reference to main srg
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 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).