Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Closed Thread
Old 02-24-2008, 02:15 AM   #1 (permalink)
simple's Avatar
 
True Techie

Join Date: Jan 2006

Location: Earth

Posts: 171

simple

Default Whats the difference???

Whats the difference between execve() and system() in C??
presumably both are used to execute external executable file??
i searched up on google but i thought guys in here would be better at this....
thanks in advance...c ya!!
__________________
<A HREF=http://ubanimator.com><IMG SRC=http://img182.imageshack.us/img182/5168/userbar511415cb2.gif><A>
simple is offline  
Old 02-24-2008, 06:09 AM   #2 (permalink)
void's Avatar
 
True Techie

Join Date: Oct 2005

Posts: 198

void

Default Re: Whats the difference???

system() takes a char *argument that is passed to the command line. In Linux this is passed to the shell and is equivalent to "/bin/sh -c [argument]". For example:
Code:
system("cat myfile.txt")
/bin/sh -c cat myfile.txt
A call to system() blocks. The calling process is forked and waits for the child process (the program just invoked) to finish.

execve is different, a call to execve allows the calling process to pass arguments and environment variables to the program to be invoked. Your program can access these environment variable when main is defined as
Code:
int main(int argc, char *argv[], char *envp[])
The most important point is execve only returns on error, therefore a successful call does not return because it overwrites the calling process, including stack. Therefore, calling cat in the example above using execve would replace your program with the cat program. This is why you have to fork your programs process before calling execve. The system() function does this for you.

Code:
child_process_id = fork();
if (child_process_id != 0) {
	//Is parent process
	return;
}
//Is child process
execve()
//If you get here execve() has failed
You may also want to look at similar functions but all related:
int execl(const char *path, const char *arg, ...);
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, ..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
void is offline  
 
Closed Thread

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Whats the difference between espresso and coffee? KCRMYZ Off Topic Discussion 10 10-07-2007 06:11 PM
Difference between Q6600 and QX6700 Sydzy Building, Buying, or Upgrading High Performance PC Systems 19 09-10-2007 03:56 PM
Samsung D900 vs D900i, is there a difference? Sydzy Phones – PDA’s – Bluetooth – Other handhelds 2 08-07-2007 04:12 AM
difference between e6600 and e6320 siddanth3 Building, Buying, or Upgrading High Performance PC Systems 16 05-22-2007 12:39 AM