现在的位置: 首页 > 综合 > 正文

exec and get pid

2014年02月16日 ⁄ 综合 ⁄ 共 1307字 ⁄ 字号 评论关闭

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,...)


#include <stdio.h>
#include <unistd.h>
int main()
{
    execlp("pwd","pwd",NULL);
    return 0;
}

it won't work with execl. execl need the full path "/bin/pwd"

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.execv

int execve(const char* path,const char* argv[], char * const evnp[]);

#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;
}

it turns to be "$PATH" , not "/bin"

I havn't found what "evnp" is for.

5. ececvp

int execv(const char* path,const char* argv[])

like ececlp

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());
    
}












抱歉!评论已关闭.