exec函数族主要有:execl、execlp
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
//execl函数
int i = 200;
int main(int argc,char *argv[])
{
pid_t pid;
pid = fork();
//father process
if(pid > 0)
{
i += 400;
printf("this is father process %d\n",getpid());
printf("i = %d\n",i);
}
//child process
else if(pid == 0)
{
execl("/home/gyj/process/hello","hello",NULL);
i += 200;
printf("this is child process %d,ppid is %d\n",getpid(),getppid());
printf("i = %d\n",i);
}
return 0;
}
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
//execlp函数
int i = 200;
int main(int argc,char *argv[])
{
pid_t pid;
pid = fork();
//father process
if(pid > 0)
{
i += 400;
printf("this is father process %d\n",getpid());
printf("i = %d\n",i);
}
//child process
else if(pid == 0)
{
execlp("ps","ps","aux",NULL);
perror("execlp");
i += 200;
printf("this is child process %d,ppid is %d\n",getpid(),getppid());
printf("i = %d\n",i);
}
return 0;
}