Whats the difference???

Status
Not open for further replies.

simple

In Runtime
Messages
171
Location
Earth
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!!
 
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[]);
 
Status
Not open for further replies.
Back
Top Bottom