exec is used for start another program in the c code of linux
1.execl
int execl(const char*path,const char* arg,...)
the last parameter must end up with NULL.
#include <stdio.h>
#include <unistd.h>
int main()
{
execl("/home/cascais/code/hello","hello","3",NULL);
return 0;
}
the path can be absolutely path or relative path like "./hello"
2.execlp
int execl(const char*path,const char* arg,...)
it won't work with execl. execl need the full path "/bin/pwd"#include <stdio.h> #include <unistd.h> int main() { execlp("pwd","pwd",NULL); return 0; }
3.execv
int execv(const char* path,const char* argv[])
use string array instead of string list.
#include <stdio.h> #include <unistd.h> int main() { char* argv[] = {"ls" , "-al", NULL}; execv("/bin/ls", argv); return 0; }
must has NULL
4.execvint execve(const char* path,const char* argv[], char * const evnp[]);
it turns to be "$PATH" , not "/bin"#include <stdio.h> #include <unistd.h> int main() { char* argv[] = {"echo" , "$PATH", NULL}; char * evnp[]={"PATH=/bin",0}; execve("/bin/echo", argv, evnp); return 0; }
I havn't found what "evnp" is for.
5. ececvp
like ececlpint execv(const char* path,const char* argv[])
but use argv instead of sting list.
#include <stdio.h> #include <unistd.h> int main() { char* argv[] = {"ls" , "-al", NULL}; execvp("ls", argv); return 0; }
#include <unistd.h> #include <stdio.h> #include <sys/syscall.h> #include <unistd.h> #define gettid() ((pid_t) syscall(SYS_gettid)) int main() { printf("start pid=%d,tid=%d\n", getpid(),gettid()); }