In-depth Analysis of the execlp() System Call in Linux: Working Principles and Usage Patterns

Dec 01, 2025 · Programming · 10 views · 7.8

Keywords: execlp | Linux system call | variadic function

Abstract: This article provides a comprehensive examination of the execlp() function in Linux, focusing on its variadic argument nature, parameter passing mechanisms, and practical application scenarios, particularly in executing commands via shell. Based on authoritative Q&A data, it systematically explains the correspondence between function declaration and actual invocation, with multiple code examples illustrating proper argument list construction, including handling complex cases like shell command expansion.

Fundamental Concepts of the execlp() System Call

In Linux programming, execlp() is a critical system call function used to execute specified executable files. According to the function prototype int execlp(const char *file, const char *arg, ...);, this is a variadic function that accepts two required parameters of type const char *, followed by any number of additional arguments, all of which must be C strings, with the last argument being a NULL pointer.

Parameter Parsing and Semantic Mapping

The first parameter file specifies the pathname of the executable file to be executed. The second parameter arg corresponds to argv[0] of the executed program, conventionally set to the same value as file, i.e., the program name. Subsequent variadic arguments are passed as additional parameters to the target program.

For example, executing the ls command from the command line corresponds to the execlp() call: execlp("ls", "ls", (char *)NULL);. For the command ls -l /, the call should be: execlp("ls", "ls", "-l", "/", (char *)NULL);.

Complex Scenarios: Executing Commands via Shell

When commands need to be executed through a shell, argument construction becomes more intricate. For instance, the call execlp("/bin/sh", ..., "ls -l /bin/??", ...); actually requires passing the command to the shell for processing. The corresponding execlp() call for executing /bin/sh -c "ls -l /bin/??" from the command line is: execlp("/bin/sh","/bin/sh", "-c", "ls -l /bin/??", (char *)NULL);.

The key here is that pattern matching like /bin/?? is expanded by the shell. If directly calling execlp("ls","ls", "-l", "/bin/??", (char *)NULL);, without shell intervention, it would not produce the expected effect unless a file named /bin/?? actually exists.

Practical Applications and Considerations

In actual programming, proper use of execlp() requires ensuring the argument list ends with a NULL pointer to avoid memory access errors. Understanding how parameters map to the target program's argv array is crucial, especially when involving shell commands, where appropriate arguments (e.g., -c) must be used to pass command strings to the shell interpreter.

By deeply analyzing the working principles of execlp(), developers can more flexibly execute external commands within programs, enabling complex system interaction functionalities.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.