随笔 - 272  文章 - 0  评论 - 283  阅读 - 142万

启动新进程(fork和exec系列函数实现)

一、复制进程映像

1fork函数介绍

  此系统调用主要复制当前进程,在进程表中创建一个新的表项,新表项中的许多属性与当前进程是相同的。新进程几乎与原进程一模一样,执行的代码也完全相同,但新进程有自己的数据空间、环境和文件描述符。

2、典型使用fork的代码片段: 

复制代码
pid_t pid;
pid = fork();

switch(pid)
{
case -1: // error occur
    perror("fork failed");
    exit(1);
case 0: // child             
    break;
default: // parent
    break;
}
复制代码

 3、示例

示例代码: 

复制代码
View Code
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
        pid_t pid;
        char *message;
        int n;

        printf("fork program starting \n");
        pid = fork();

        switch(pid)
        {
        case -1:
                perror("fork failed");
                exit(1);
        case 0:
                message = "This is the child";
                n = 5;
                break;
        default:
                message = "This is the parent";
                n = 3;
                break;
        }
        for(;n>0;n--)
        {
                puts(message);
                sleep(1);
        }
        exit(0);
}
复制代码

 运行效果如下:

二、替换进程映像

1exec系列函数介绍 

exec系列函数可以把当前进程替换为一个新的进程,函数列表如下: 

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

2、头文件:

#include <unistd.h>

3、示例

示例代码:

复制代码
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
        execlp("ls","ls","-l",0);
        printf("OK\n");
        exit(0);
}
复制代码

运行效果:

三、启动新进程 

1system函数实现 

1.1 说明 

system函数可以在一个程序的内部启动另一个程序,从而创建一个新进程。

#include <stdlib.h>
int system(const char *string);

system函数的作用是,运行以字符串参数的形式传递给它的命令并等待该命令的完成。命令的执行情况就如同在shell中执行如下命令: 

$sh -c string 

1.2 示例 

示例代码:

复制代码
#include <stdlib.h>
#include <stdio.h>

int main()
{
        system("ls -l");
        printf("OK\n");
        exit(0);
}
复制代码

运行效果:

2forkexec系列函数实现

  一般来说,使用system函数不是启动其它进程的理想手段,因为它必须用一个shell来启动需要的程序。由于启动之前需要先启动一个shell,而且对shell的安装情况及使用的环境的依赖也很大,所以使用system函数效率不高。鉴于此种情况,可以考虑使用fork启动子进程,然后用exec系列函数替换当前子进程满足此种需求。

示例代码(wait1.c:

复制代码
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

int main()
{
        pid_t pid;
        char *message;
        int exit_code;

        printf("fork program starting\n");
        pid = fork();
        switch(pid)
        {
        case -1:
                perror("fork failed");
                exit(1);
        case 0:
                message = "This is the child";
                sleep(3); //sleep 
                execlp("ls","ls","-l",0);
                exit_code = 37;
                break;
        default:
                message = "This is the parent";
                exit_code = 0;
                break;
        }
        if(pid != 0) //wait child 
        {
                int stat_val;
                pid_t child_pid;

                child_pid = wait(&stat_val);

                printf("Child has finished : PID = %d\n",child_pid);
                if(WIFEXITED(stat_val))
                        printf("Child exited with code %d\n",WEXITSTATUS(stat_val));
                else
                        printf("Child terminated abnormally\n");
        }
        exit(exit_code);
}
复制代码

运行效果:

posted on   Mike_Zhang  阅读(2339)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2012年6月 >
27 28 29 30 31 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
1 2 3 4 5 6 7

点击右上角即可分享
微信分享提示