作用:直接调用其他可执行程序 ,代码简洁
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
pid_t pid;
int data;
while(1){
printf("input a data:\n");
scanf("%d",&data);
if(data == 1){
pid = fork();
if(pid > 0){
wait();//父进程等待子进程退出
}
if(pid==0){ //子进程干活(修改文件)
int fdSrc;
char *readBuf = NULL;
fdSrc = open("config.txt",O_RDWR);
int size = lseek(fdSrc,0,SEEK_END);
lseek(fdSrc,0,SEEK_SET);
readBuf =(char *) malloc(sizeof(char)*size + 8);
int n_read = read(fdSrc,readBuf,size);
char *p = strstr(readBuf,"LENG=5");
if(p==NULL){
printf("nou found\n");
exit(-1);
}
p = p + strlen("LENG=");
*p = '5';
lseek(fdSrc,0,SEEK_SET);
int n_write = write(fdSrc,readBuf,strlen(readBuf));
close(fdSrc);
}else{
printf("wait, do nothing\n");
}
}
}
return 0;
}
使用exec函数
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
pid_t pid;
int data;
while(1){
printf("input a data:\n");
scanf("%d",&data);
if(data == 1){
pid = fork();
if(pid > 0){
wait();
}
if(pid==0){
execl("./changData","changData","config.txt",NULL);
exit(0);
}
}else{
printf("wait, do nothing\n");
}
}
return 0;
}
函数族:
exec函数族分别是:execl, execlp, execle, execv, execvp, execvpe
函数原型:
#include <unistd.h>
extern char **environ;
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg,..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[],char *const envp[]);
返回值:
exec函数族的函数执行成功后不会返回,调用失败时,会设置errno并返回-1,然后从原程序的调用点接着往下执行。
参数说明:
path:可执行文件的路径名字
arg:可执行程序所带的参数,第一个参数为可执行文件名字,没有带路径且arg必须以NULL结束
file:如果参数file中包含/,则就将其视为路径名,否则就按 PATH环境变量,在它所指定的各目录中搜寻可执行文件。
exec族函数参数极难记忆和分辨,函数名中的字符会给我们一些帮助:
l : 使用参数列表
p:使用文件名,并从PATH环境进行寻找可执行文件
v:应先构造一个指向各参数的指针数组,然后将该数组的地址作为这些函数的参数。
e:多了envp[]数组,使用新的环境变量代替调用进程的环境变量
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execl\n");
if(execl("./bin/echoarg","echoarg","abc",NULL) == -1)
{
printf("execl failed!\n");
}
printf("after execl\n");
return 0;
}
CLC@Embed_Learn:~/2_jingCheng$ ./a.out
before execl
execl failed!
after execl
#include <stdio.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(int argc,char *argv[])
{
int i = 0;
for(i = 0; i < argc; i++)
{
printf("argv[%d]: %s\n",i,argv[i]);
}
return 0;
}
CLC@Embed_Learn:~/2_jingCheng$ ./execpro file file2
argv[0]: ./execpro
argv[1]: file
argv[2]: file2
ls
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execl\n");
//execl成功后不会返回,失败后返回-1
if(execl("/bin/ls","ls",NULL,NULL) == -1)
{
printf("execl failed!\n");
//错误原因,perror函数
perror("why");
}
printf("after execl\n");
return 0;
}
CLC@Embed_Learn:~/2_jingCheng$ ./a.out
before execl
execl failed!
why: No such file or directory
after execl
CLC@Embed_Learn:~/2_jingCheng$ gcc exec2.c
CLC@Embed_Learn:~/2_jingCheng$ ./a.out
before execl
a.out demo11.c demo13.c demo15.c demo1.c demo3.c demo5.c demo7.c demo9.c exec2.c execpro newpro
demo10.c demo12.c demo14.c demo16.c demo2.c demo4.c demo6.c demo8.c exec1.c exec.c forkPro
CLC@Embed_Learn:~/2_jingCheng$ ls
a.out demo11.c demo13.c demo15.c demo1.c demo3.c demo5.c demo7.c demo9.c exec2.c execpro newpro
demo10.c demo12.c demo14.c demo16.c demo2.c demo4.c demo6.c demo8.c exec1.c exec.c forkPro
CLC@Embed_Learn:~/2_jingCheng$
ls -l
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execl\n");
if(execl("/bin/ls","ls","-l",NULL) == -1)
{
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
where is date?时间 在/bin路径找到date
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("this pro get system date:\n");
if(execl("/bin/date","date",NULL,NULL) == -1)
{
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
CLC@Embed_Learn:~/2_jingCheng$ vi exec4.c
CLC@Embed_Learn:~/2_jingCheng$ gcc exec4.c
CLC@Embed_Learn:~/2_jingCheng$ ./a.out
this pro get system date:
Sun Mar 6 13:50:41 CST 2022
老是要找路径好烦,咋办?用execlp!!! p:使用文件名,并从PATH环境进行寻找可执行文件
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("this pro ps:\n");
if(execlp("ps","ps",NULL,NULL) == -1)
{
printf("execl failed!\n");
perror("why");
}
printf("after execl\n");
return 0;
}
this pro ps:
PID TTY TIME CMD
30589 pts/27 00:00:01 bash
31772 pts/27 00:00:00 ps
如何让程序在其他路径也可以运行可执行程序呢?
配置环境变量!!!
CLC@Embed_Learn:~/2_jingCheng$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/FriendlyARM/toolschain/4.5.1/bin
CLC@Embed_Learn:~/2_jingCheng$
CLC@Embed_Learn:~/2_jingCheng$ export PATH=$PATH:/home/CLC/2_jingCheng//添加环境变量路径
CLC@Embed_Learn:~/2_jingCheng$
CLC@Embed_Learn:~/2_jingCheng$ echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/opt/FriendlyARM/toolschain/4.5.1/bin:/home/CLC/2_jingCheng
配置好后,在其他路径运行不需要加./
CLC@Embed_Learn:~$ a.out
this pro ps:
PID TTY TIME CMD
30589 pts/27 00:00:01 bash
31846 pts/27 00:00:00 ps
CLC@Embed_Learn:~$
CLC@Embed_Learn:~$
CLC@Embed_Learn:~$ newpro
son print , pid = 31850
son print , pid = 31850
son print , pid = 31850
son print , pid = 31850
son print , pid = 31850
father print , pid = 31849
cnt = 5
father print , pid = 31849
cnt = 5
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
//函数原型:int execl(const char *path, const char *arg, ...);
int main(void)
{
printf("before execlp****\n");
char *argv[] = {"ps","-l",NULL};
if(execvp("ps",argv) == -1)
{
printf("execvp failed!\n");
}
printf("after execlp*****\n");
return 0;
}
CLC@Embed_Learn:~/2_jingCheng$ ./a.out
before execlp****
F S UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD
0 S 1000 30589 3796 0 80 0 - 6994 wait pts/27 00:00:01 bash
0 R 1000 31924 30589 0 80 0 - 3482 - pts/27 00:00:00 ps
CLC@Embed_Learn:~/2_jingCheng$ vi exec6.c