一、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. Android组件之BroadCast简单实践

    作为Android的四大组件之一,没有理由不介绍一下BroadCast,BroadCast中文简单翻译就是广播,前阵子浙江某大学的啦啦操,广场舞的大妈,其中大妈和学生从喇叭和音响上听到的声音就是事件源 ...

  2. 2013年度最新最佳CSS网页设计实例

    CSS在网页设计中扮演着举足轻重的角色,尤其是CSS3和HTML5的出现,让我们更加相信CSS能给我们带来无限的遐想和强有力的视觉冲击.今天我要给大家推荐几个最新CSS佳作,虽然本人没有设计头脑,但还 ...

  3. [Node.js]30. Level 6: Listen 'Question' from client, and then Answer the Question

    Clients can also answer each other questions, so let's build that feature by first listening for the ...

  4. jquery ui autocomplete 模拟百度搜索自动提示

    直接上代码 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=" ...

  5. UIFont可用字体

    label.font = [UIFont fontWithName:@"Arial-BoldItalicMT" size:24]; 字体名如下: Font Family: Amer ...

  6. uva 701 - The Archeologists' Dilemma

    题目链接:uva 701 - The Archeologists' Dilemma 题目大意:给出x,求一个e,使得x * 10 ^ y ≤ 2 ^ e < (x + 1) * 10 ^ y. ...

  7. MISRA-C 2012 C90规范和C99规范对比

  8. MySQL 联合索引测试2

    接上一篇文章: http://www.cnblogs.com/xiaoit/p/4430300.html 1:首先删掉上一篇建立的索引,重新建立一个. mysql> DROP INDEX idx ...

  9. glibc的几个有用的处理二进制位的内置函数(转)

    — Built-in Function: int __builtin_ffs (unsigned int x) Returns one plus the index of the least sign ...

  10. 新浪微博API使用初步介绍——解决回调地址的问题

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #新浪微博API使用初步介绍——解决回调地址的问题 #http://blog.csdn.net/monsion ...