关于Linux编程中的execl参数简单说明。

    int execl(const char *pathname, const char *arg, ... /* (char  *) NULL */);
    int execlp(const char *file, const char *arg, ... /* (char  *) NULL */);

以上是从man手册中复制下来的。以下我们进行分析。

execl

比如我们使用execl执行ls进程,那么可以写成这样:

execlp("/bin/ls","ls","-al","/temp",NULL);

第一个参数就是ls命令的绝对路径,

第二个参数就是进程的名称,这个名字可以填写也可以不填写,甚至可以随便填写,你填写什么,到时候在ps中就可以显示这个进程的名称,比如你填写成LinuxC123,那么在ps中可以查看到如下

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
jishuge     5794  0.0  0.0   2772  1280 pts/0    S    10:32   0:00 LinuxC123

剩余的参数就是命令的选项。

最后一个参数一定是NULL。文档规定的:

The const char *arg and subsequent ellipses can be thought of as arg0, arg1, …, argn. To‐
gether they describe a list of one or more pointers to null-terminated strings that represent
the argument list available to the executed program. The first argument, by convention, should
point to the filename associated with the file being executed. The list of arguments must be
terminated by a null pointer, and, since these are variadic functions, this pointer must be
cast (char *) NULL.