execle - Linux
Overview
execle is a system call that replaces the current running program with a new program. It is typically used in conjunction with the fork()
system call to create new processes.
Syntax
int execle(const char *path, const char *arg0, ..., const char *argn, (char *)0);
- path: The path to the new program to execute.
- arg0: The name of the new program.
- …, argn: Optional arguments to pass to the new program.
- *(char )0: A null pointer to terminate the list of arguments.
Options/Flags
- None
Examples
Simple example:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
execle("/bin/ls", "ls", "-l", NULL);
// If execle succeeds, this line will not be executed.
perror("execle");
exit(EXIT_FAILURE);
}
Complex example:
This example creates a new process that runs the ls
command with the -l
and -a
arguments.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
char *args[] = {"/bin/ls", "-l", "-a", NULL};
execle(args[0], args[0], args[1], args[2], NULL);
// If execle succeeds, this line will not be executed.
perror("execle");
exit(EXIT_FAILURE);
}
Common Issues
- Permission denied: Ensure that the user has permission to execute the new program.
- File not found: Verify that the path to the new program is correct.
- Invalid arguments: Check that the arguments passed to the new program are valid.
Integration
- fork() and execle(): Fork creates a new process and execle replaces the current process with a new program. This is a common technique for creating new processes.
- Pipes: Pipes can be used to connect the output of one program to the input of another program. Execle can be used to launch a new program that reads from or writes to a pipe.
Related Commands
- fork
- exec
- execlp
- execv
- execvp