exec族
【摘要】 这个一直没搞太明白,相识度高是一回事,不会用又是另一回事儿。 所以今天又温故了一遍:
为什么需要exec函数?
fork子进程是为了执行新程序(fork创建了子进程后,子进程和父进程同时被OS调度执行,因此子进程可以单独的执行一个程序,这个程序宏观上将会和父进程程序同时进行)
可以直接在子进程的if中写入新程序打代码。但这样不够灵活,因为我们只能把子进程程序的源代...
这个一直没搞太明白,相识度高是一回事,不会用又是另一回事儿。
所以今天又温故了一遍:
为什么需要exec函数?
fork子进程是为了执行新程序(fork创建了子进程后,子进程和父进程同时被OS调度执行,因此子进程可以单独的执行一个程序,这个程序宏观上将会和父进程程序同时进行)
可以直接在子进程的if中写入新程序打代码。但这样不够灵活,因为我们只能把子进程程序的源代码贴过来执行(必须知道源代码,而且源代码太长了也不好控制)
使用exec族函数运行新的可执行程序。exec族函数可以直接把一个编译好的可执行程序直接加载运行。
有了exec族函数后,典型打父子进程程序是这样的:子进程需要运行的程序被单独编写、单独编译链接成一个可执行程序(hello)。主进程为父进程,fork创建了子进程后在子进程中exec来执行hello,达到父子进程分别做不同程序同时(宏观上)运行的效果。
#include<unistd.h>
int execve(const char *path,char *const argv[],char *const envp[]);//这个是真正的系统调用
//以下的函数最后都是调用这个函数
int execl(const char *path,char *const argv,···);
int execlp(const char *file,char *const argv,···);
int execle(const char *path,char *const argv,···· ,char *const envp[]);
int execv(const char *path,char *const argv[]);
int execvp(const char *file,char *const argv,);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
exec函数族装入并运行程序path/file,并将参数arg0(arg1, arg2, argv[], envp[])传递给子程序,出错返回-1.
看一下后缀:
后缀 | 功能 |
---|---|
l | 希望接收以逗号分隔的参数列表,列表以NULL指针作为结束标志 |
v | 希望接收到一个以NULL结尾的字符串数组的指针 |
p | 是一个以NULL结尾的字符串数组指针,函数可以DOS的PATH变量查找子程序文件 |
e | 函数传递指定参数envp,允许改变子进程的环境,无后缀e时,子进程使用当前程序的环境 |
下面我找到一些通俗易懂的栗子,算是让我明白了一点:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int argc, char *argv[])
{
//以NULL结尾的字符串数组的指针,适合包含v的exec函数参数
char *arg[] = {"ls", "-a", NULL}; /** * 创建子进程并调用函数execl * execl 中希望接收以逗号分隔的参数列表,并以NULL指针为结束标志 */
if( fork() == 0 )
{ // in clild printf( "1------------execl------------\n" ); if( execl( "/bin/ls", "ls","-a", NULL ) == -1 ) { perror( "execl error " ); exit(1); }
} /** *创建子进程并调用函数execv *execv中希望接收一个以NULL结尾的字符串数组的指针 */
if( fork() == 0 )
{ // in child printf("2------------execv------------\n"); if( execv( "/bin/ls",arg) < 0) { perror("execv error "); exit(1); }
} /** *创建子进程并调用 execlp *execlp中 *l希望接收以逗号分隔的参数列表,列表以NULL指针作为结束标志 *p是一个以NULL结尾的字符串数组指针,函数可以DOS的PATH变量查找子程序文件 */
if( fork() == 0 )
{ // in clhild printf("3------------execlp------------\n"); if( execlp( "ls", "ls", "-a", NULL ) < 0 ) { perror( "execlp error " ); exit(1); }
} /** *创建子里程并调用execvp *v 望接收到一个以NULL结尾的字符串数组的指针 *p 是一个以NULL结尾的字符串数组指针,函数可以DOS的PATH变量查找子程序文件 */
if( fork() == 0 )
{ printf("4------------execvp------------\n"); if( execvp( "ls", arg ) < 0 ) { perror( "execvp error " ); exit( 1 ); }
} /** *创建子进程并调用execle *l 希望接收以逗号分隔的参数列表,列表以NULL指针作为结束标志 *e 函数传递指定参数envp,允许改变子进程的环境,无后缀e时,子进程使用当前程序的环境 */
if( fork() == 0 )
{ printf("5------------execle------------\n"); if( execle("/bin/ls", "ls", "-a", NULL, NULL) == -1 ) { perror("execle error "); exit(1); }
} /** *创建子进程并调用execve * v 希望接收到一个以NULL结尾的字符串数组的指针 * e 函数传递指定参数envp,允许改变子进程的环境,无后缀e时,子进程使用当前程序的环境 */
if( fork() == 0 )
{ printf("6------------execve-----------\n"); if( execve( "/bin/ls", arg, NULL ) == 0) { perror("execve error "); exit(1); }
}
return EXIT_SUCCESS;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
文章来源: lion-wu.blog.csdn.net,作者:看,未来,版权归原作者所有,如需转载,请联系作者。
原文链接:lion-wu.blog.csdn.net/article/details/104296436
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)