Linux C编程--进程介绍2--exec函数
2014-04-23 13:39
169 查看
exec函数族
fork()函数是用于创建一个子进程,该子进程几乎拷贝了父进程的全部内容,但是,这个新创建的进程如何执行呢?这个exec函数族就提供了一个在进程中启动另一个程序执行的方法。
exec函数族包括6个函数:
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, const char *envp[]);
int execv(const char *path, const char *argv[]);
int execve(const char *path, const char *argv[], const char *envp[];
int execvp(const char *file, const char *argv[]);
参数说明:
execl的第一个参数是包括路径的可执行文件,后面是列表参数,列表的第一个为命令path,接 着为参数列表,最后必须以NULL结束。
execlp的第一个参数可以使用相对路径或者绝对路径。
execle,最后包括指向一个自定义环境变量列表的指针,此列表必须以NULL结束。
execv,v表示path后面接收的是一个向量,即指向一个参数列表的指针,注意这个列表的最后 一项必须为NULL。
execve,path后面接收一个参数列表向量,并可以指定一个环境变量列表向量。
execvp,第一个参数可以使用相对路径或者绝对路径,v表示后面接收一个参数列表向量。
exec被调用时会替换调用它的进程的代码段和数据段(但是文件描述符不变),直接返回到调用它的进程的父进程,如果出错,返回-1并设置errno。
下面给出两个例子
1.这是一个非常简单的程序,只是为了说明exec的使用
[cpp] view
plaincopyprint?
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("===system call execl testing ===\n");
execlp("date","date",0);
printf("exec error !\n");
return 0;
}
===system call execl testing ===
2014年 04月 23日 星期三 13:36:55 CST
当execl调用成功时,printf函数并不执行。
2.这个例子是exec函数和fork函数的联合使用,这是一个实用工具,功能是对某一指定文件进行监视,当所监视的文件修改后,自动为它建立一个副本。
[cpp] view
plaincopyprint?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd;
int stat,pid;
struct stat stbuf;
time_t old_time=0;
if(argc!=3)
{
fprintf(stderr,"Usage: %s watchfile copyfile \n", argv[0]);
return 1;
}
if((fd=open(argv[1],O_RDONLY))==-1)
{
fprintf(stderr,"Watchfile: %s can't open \n", argv[1]);
return 2;
}
fstat(fd, &stbuf);
old_time=stbuf.st_mtime;
for(;;)
{
fstat(fd, &stbuf);
if(old_time!=stbuf.st_mtime)
{
while((pid=fork())==-1);
if(pid==0)
{
execl("/bin/cp","/bin/cp",argv[1],argv[2],0);
return 3;
}
wait(&stat);
old_time=stbuf.st_mtime;
}
else
sleep(30);
}
}
wait(&stat);
old_time=stbuf.st_mtime;
这两句的功能是:
父进程调用另一个系统函数wait等待子进程终止
进程被while((pid=fork())==-1);这条语句中断
fork()函数是用于创建一个子进程,该子进程几乎拷贝了父进程的全部内容,但是,这个新创建的进程如何执行呢?这个exec函数族就提供了一个在进程中启动另一个程序执行的方法。
exec函数族包括6个函数:
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg, const char *envp[]);
int execv(const char *path, const char *argv[]);
int execve(const char *path, const char *argv[], const char *envp[];
int execvp(const char *file, const char *argv[]);
参数说明:
execl的第一个参数是包括路径的可执行文件,后面是列表参数,列表的第一个为命令path,接 着为参数列表,最后必须以NULL结束。
execlp的第一个参数可以使用相对路径或者绝对路径。
execle,最后包括指向一个自定义环境变量列表的指针,此列表必须以NULL结束。
execv,v表示path后面接收的是一个向量,即指向一个参数列表的指针,注意这个列表的最后 一项必须为NULL。
execve,path后面接收一个参数列表向量,并可以指定一个环境变量列表向量。
execvp,第一个参数可以使用相对路径或者绝对路径,v表示后面接收一个参数列表向量。
exec被调用时会替换调用它的进程的代码段和数据段(但是文件描述符不变),直接返回到调用它的进程的父进程,如果出错,返回-1并设置errno。
下面给出两个例子
1.这是一个非常简单的程序,只是为了说明exec的使用
[cpp] view
plaincopyprint?
#include <stdio.h>
#include <unistd.h>
int main()
{
printf("===system call execl testing ===\n");
execlp("date","date",0);
printf("exec error !\n");
return 0;
}
===system call execl testing ===
2014年 04月 23日 星期三 13:36:55 CST
当execl调用成功时,printf函数并不执行。
2.这个例子是exec函数和fork函数的联合使用,这是一个实用工具,功能是对某一指定文件进行监视,当所监视的文件修改后,自动为它建立一个副本。
[cpp] view
plaincopyprint?
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd;
int stat,pid;
struct stat stbuf;
time_t old_time=0;
if(argc!=3)
{
fprintf(stderr,"Usage: %s watchfile copyfile \n", argv[0]);
return 1;
}
if((fd=open(argv[1],O_RDONLY))==-1)
{
fprintf(stderr,"Watchfile: %s can't open \n", argv[1]);
return 2;
}
fstat(fd, &stbuf);
old_time=stbuf.st_mtime;
for(;;)
{
fstat(fd, &stbuf);
if(old_time!=stbuf.st_mtime)
{
while((pid=fork())==-1);
if(pid==0)
{
execl("/bin/cp","/bin/cp",argv[1],argv[2],0);
return 3;
}
wait(&stat);
old_time=stbuf.st_mtime;
}
else
sleep(30);
}
}
wait(&stat);
old_time=stbuf.st_mtime;
这两句的功能是:
父进程调用另一个系统函数wait等待子进程终止
进程被while((pid=fork())==-1);这条语句中断
相关文章推荐
- Linux C编程--进程介绍2--exec函数
- Linux C编程--进程介绍2--exec函数
- Linux C编程--进程介绍4--errno
- Linux C编程--进程介绍5--system函数
- linux c编程:进程控制(三)_exec函数
- Linux C编程--进程介绍1--进程的创建
- Linux C编程--进程介绍5--system函数
- Linux C编程--进程介绍1--进程的创建
- Linux C编程--进程介绍1--进程的创建
- Linux C编程--进程介绍3--进程终止和等待
- Linux C编程--进程介绍3--进程终止和等待
- Linux C编程--进程介绍6--进程的各种标识
- Linux C编程--进程介绍6--进程的各种标识
- Linux C编程--进程介绍7--综合应用实例
- Linux C编程--进程介绍3--进程终止和等待
- Linux C编程--进程介绍7--综合应用实例
- Linux C编程--进程介绍3--进程终止和等待
- Linux C编程--进程介绍4--errno
- 关于linux服务器进程监控及自动重启的方法介绍
- linux c编程:进程环境