一、exec替换进程映象

在进程的创建上Unix采用了一个独特的方法,它将进程创建与加载一个新进程映象分离。这样的好处是有更多的余地对两种操作进行管理。当我们创建

了一个进程之后,通常将子进程替换成新的进程映象,这可以用exec系列的函数来进行。当然,exec系列的函数也可以将当前进程替换掉。

二、exec关联函数组

包含头文件<unistd.h>

功能用exec函数可以把当前进程替换为一个新进程。exec名下是由多个关联函数组成的一个完整系列,头文件<unistd.h>

原型
     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[]);

参数
path参数表示你要启动程序的名称包括路径名
arg参数表示启动程序所带的参数

返回值:成功返回0,失败返回-1

execl,execlp,execle(都带“l”)的参数个数是可变的,参数以一个空指针结束。
execv、execvp和execvpe的第二个参数是一个字符串数组,新程序在启动时会把在argv数组中给定的参数传递到main

名字含字母“p”的函数会搜索PATH环境变量去查找新程序的可执行文件。如果可执行文件不在PATH定义的路径上,就必须把包括子目录在内的绝对文件名做为一个参数传递给这些函数。

名字最后一个字母为"e"的函数可以自设环境变量。

这些函数通常都是用execve实现的,这是一种约定俗成的做法,并不是非这样不可。

int execve(const char *filename, char *const argv[], char *const envp[]);

注意,前面6个函数都是C库函数,而execve是一个系统调用。

三、执行exec函数,下面属性是不发生变化的:

  • 进程ID和父进程ID(pid, ppid)
  • 实际用户ID和实际组ID(ruid, rgid)
  • 附加组ID(sgid)
  • 会话ID
  • 控制终端
  • 闹钟余留时间
  • 当前工作目录
  • 根目录
  • umask
  • 文件锁
  • 进程信号屏蔽
  • 未处理信号
  • 资源限制
  • 进程时间

而下面属性是发生变化的:

  • 文件描述符如果存在close-on-exec标记的话,那么打开的文件描述符会被关闭。
  • 如果可执行程序文件存在SUID和SGID位的话,那么有效用户ID和组ID(euid, egid)会发生变化

程序启动的时候,所有的信号处理方式都是默认的。然后fork来说,因为子进程和父进程的地址空间是一样的,所以信号处理方式保留了下来。 接下来进行exec,会将所有设置成为捕捉的信号都修改成为默认处理,而原来已经设置成为忽略的信号就不发生改变。

示例程序:

为了演示自设环境变量的功能,先写个小程序,可以输出系统的环境变量

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 
/*************************************************************************
    > File Name: pid_env.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Sun 24 Feb 2013 07:52:09 PM CST
 ************************************************************************/

#include<stdio.h>
#include<unistd.h>

extern char **environ;

int main(void)
{
    printf("hello pid=%d\n", getpid());
    int i;
    for (i = 0; environ[i] != NULL; i++)
        printf("%s\n", environ[i]);
    return 0;
}

其中environ是全局变量但没有在头文件中声明,所以使用前需要外部声明一下。输出如下:

simba@ubuntu:~/Documents/code/linux_programming/APUE/process$ ./pid_env 
hello pid=5597
TERM=vt100
SHELL=/bin/bash
XDG_SESSION_COOKIE=0ba97773224d90f8e6cd57345132dfd0-1368605430.130657-1433620678
SSH_CLIENT=192.168.232.1 8740 22
SSH_TTY=/dev/pts/0
USER=simba

......................

即输出了一些系统环境的变量,变量较多,省略输出。

我们前面在讲到fcntl 函数时未讲到当cmd参数取F_SETFD时的情形,即设置文件描述符的标志,现结合exec系列函数讲解如下:

 C++ Code 
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
 
/*************************************************************************
    > File Name: process_.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
 ************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>

#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)
/* 这几个库函数都会调用execve这个系统调用 */
int main(int argc, char *argv[])
{
    char *const args[] = {"ls", "-l", NULL};
    printf("Entering main ... \n");
    //  execlp("ls", "ls", "-l", NULL); // 带p会搜索PATH
    //  execl("/bin/ls", "ls", "-l", NULL); // 带l为可变参数
    //  execvp("ls", args); //args数组参数传递给main
    //  execv("/bin/ls", args);

int ret;
    //  ret = fcntl(1, F_SETFD, FD_CLOEXEC);
    /* FD_CLOSEXEC被置位为1(在打开文件时标志为O_CLOEXEC也会置位),
     * 即在执行execve时将标准输出的文件描述符关闭,
     * 即下面替换的pid_env程序不会在屏幕上输出信息
     */
    //  if (ret == -1)
    //      perror("fcntl error");

char *const envp[] = {"AA=11", "BB=22", NULL};
    ret = execle("./pid_env", "pid_enV", NULL, envp); // 带e可以自带环境变量
    //  execvpe("ls", args, envp);
    if (ret == -1)
        perror("exec error");
    printf("Exiting main ... \n");

return 0;
}

我们使用了exec系列函数进行举例进程映像的替换,最后未被注释的execle函数需要替换的程序正是我们前面写的输出系统环境变量的小程序,但因为

execle可以自设环境变量,故被替换后的进程输出的环境变量不是系统的那些而是自设的,输出如下:

simba@ubuntu:~/Documents/code/linux_programming/APUE/process$ ./exec

Entering main ... 
hello pid=5643
AA=11
BB=22

如果我们将上面 fcntl 函数的注释打开了,即设置当执行exec操作时,关闭标准输出(fd=1)的文件描述符,也就是说

因为如果替换进程映像成功,那么直接到替换进程的main函数开始执行,不会返回,故不会输出Exiting main ...

原型:  int system(const char *command);

如果无法启动shell运行命令,system将返回127;出现不能执行system调用的其他错误时返回-1。如果system能够顺利执行,返回那个命令的退出

码。system函数执行时,会调用fork、execve、waitpid等函数。

我们可以自己实现一个my_system函数,如下:

 C++ Code 
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
 
/*************************************************************************
    > File Name: process_.c
    > Author: Simba
    > Mail: dameng34@163.com
    > Created Time: Sat 23 Feb 2013 02:34:02 PM CST
 ************************************************************************/
#include<sys/types.h>
#include<sys/stat.h>
#include<unistd.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<string.h>
#include<sys/wait.h>

#define ERR_EXIT(m) \
    do { \
        perror(m); \
        exit(EXIT_FAILURE); \
    } while(0)

int my_system(const char *command);

int main(int argc, char *argv[])
{
    /* 相当于调用 /bin/sh -c ls -l | wc -w */
    //  system("ls -l | wc -w");
    my_system("ls -l | wc -w");
    return 0;
}

int my_system(const char *command)
{
    pid_t pid;
    int status;
    if (command == NULL)
        return 1;

if ((pid = fork()) < 0)
        status = -1;
    else if (pid == 0)
    {
        execl("/bin/sh", "sh", "-c", command, NULL);
        exit(127);
    }
    else
    {
        while (waitpid(pid, &status, 0) < 0)
        {
            if (errno == EINTR)
                continue;
            status = -1;
            break;
        }
    }

return status;
}

需要说明的是在while循环中,如果waitpid返回-1错误,则还需要判断一下是否被信号处理函数所中断,如果是则继续等待,否则跳出循环。man 7

signal 有如下解释:

If a signal handler is invoked while a system call or library function call is blocked, then either:

* the call is automatically restarted after the signal handler returns; or

* the call fails with the error EINTR.

参考:《APUE》

exec系列函数和system函数的更多相关文章

  1. 16、cgminer学习之:popen函数和system函数详解(执行系统命令)

    1.popen函数我们先用man指令查一下popen函数: 函数说明: (1)popen()会调用fork()产生子进程,然后从子进程中调用/bin/sh -c来执行参数command的指令. (2) ...

  2. 一、进程与信号之exec函数system函数

    exec函数: 子进程调用exec函数执行另一个程序,exec函数进程完全由新程序代替,替换原有程序正文,数据,堆,栈段 #include <unistd.h> extern char * ...

  3. 对于linux下system()函数的深度理解(整理)

    原谅: http://blog.sina.com.cn/s/blog_8043547601017qk0.html 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同 ...

  4. 转:对于linux下system()函数的深度理解(整理)

    这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以为 ...

  5. system函数遇到的问题 - 程序死掉

    system函数遇到的问题 解决方案见最下边 http://blog.csdn.net/yangzhenzhen/article/details/51505176 这几天调程序(嵌入式linux),发 ...

  6. system函数遇到的问题

     这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数中调用的命令也都一切正常.就没理这个bug,以 ...

  7. (笔记)Linux下system()函数的深度理解(整理)

    注:从其它地方转的非常好的一篇文章,值得深究! 这几天调程序(嵌入式linux),发现程序有时就莫名其妙的死掉,每次都定位在程序中不同的system()函数,直接在shell下输入system()函数 ...

  8. linux系统编程之进程(七):system()函数使用

    一,system()理解 功能:system()函数调用"/bin/sh -c command"执行特定的命令,阻塞当前进程直到command命令执行完毕 原型: int syst ...

  9. linux系统编程之进程(七):system()函数使用【转】

    本文转载自:http://www.cnblogs.com/mickole/p/3187974.html 一,system()理解 功能:system()函数调用“/bin/sh -c command” ...

随机推荐

  1. Maximum Subarray leetcode java

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

  2. 深入理解VUE样式style层次分析

    刚开始使用vue的时候容易被里面的样式搞懵: 样式可以在main.js中引入,在模块js文件中引入,在组件中的style标签引入,在组件中的script标签引入,还可以在index.html的body ...

  3. apache 伪静态 .htaccess

    虽然网上有很多教程,但是我在这里进行简单对我用到的总结一下. 加载Rewrite模块: 在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/ ...

  4. Back Track 5 之 漏洞攻击 && 密码攻击 && Windows下渗透工具

    网络漏洞攻击工具 Metasploit 先msfupdate升级: 然后选择msfconsole: 接下来: set LHOST 本机IP地址 setLPORT setg PAYLOAD window ...

  5. JSON字符串转换为Map

    本文是利用阿里巴巴封装的FastJSON来转换json字符串的.例子如下: package com.zkn.newlearn.json; import com.alibaba.fastjson.JSO ...

  6. Linux上安装Bugzilla4.4小记

    因项目需要,我受命在一台Linux服务器上搭建一个Bugzilla,进过一天的调试,这项任务总算完成了.现在可以肯定的说,安装过程不复杂,基本就是解压,填参数,执行命令.Bugzilla要跑起来,本机 ...

  7. ZH奶酪:Yii PHP sum SQL查询语句

    例子: $sql = 'SELECT SUM(o.price) as `sum` FROM `order` o WHERE o.customer_id ='.$profile->id; $ret ...

  8. 使用Yii 1.1框架搭建第一个web应用程序

    我已经安装好了WampServer,web项目根目录是 D:\wamp\www. 1.下载Yii 1.1的源码 yii-1.1.14.f0fee9.tar.gz: 2.解压源码包,放在web项目的根目 ...

  9. SqlServer日常积累(二)

    1.Like运算符:将字符串表达式与 SQL表达式中的模式进行比较匹配. 语法 :expression Like 'pattern' ,expression为匹配字段,pattern为匹配字符串.可以 ...

  10. 上传app store 应用指南链接--2

    http://www.docin.com/p-166876874.html http://jamesli.cn/blog/?p=955 http://www.adobe.com/devnet/flas ...