启动新进程(fork和exec系列函数实现)
2017-09-10 00:00
471 查看
一、复制进程映像
1、fork函数介绍
此系统调用主要复制当前进程,在进程表中创建一个新的表项,新表项中的许多属性与当前进程是相同的。新进程几乎与原进程一模一样,执行的代码也完全相同,但新进程有自己的数据空间、环境和文件描述符。
2、典型使用fork的代码片段:
3、示例
示例代码:
View Code
运行效果如下:
二、替换进程映像
1、exec系列函数介绍
exec系列函数可以把当前进程替换为一个新的进程,函数列表如下:
2、头文件:
#include <unistd.h>
3、示例
示例代码:
运行效果:
三、启动新进程
1、system函数实现
1.1 说明
system函数可以在一个程序的内部启动另一个程序,从而创建一个新进程。
system函数的作用是,运行以字符串参数的形式传递给它的命令并等待该命令的完成。命令的执行情况就如同在shell中执行如下命令:
$sh -c string
1.2 示例
示例代码:
运行效果:
2、fork和exec系列函数实现
一般来说,使用system函数不是启动其它进程的理想手段,因为它必须用一个shell来启动需要的程序。由于启动之前需要先启动一个shell,而且对shell的安装情况及使用的环境的依赖也很大,所以使用system函数效率不高。鉴于此种情况,可以考虑使用fork启动子进程,然后用exec系列函数替换当前子进程满足此种需求。
示例代码(wait1.c):
运行效果:
1、fork函数介绍
此系统调用主要复制当前进程,在进程表中创建一个新的表项,新表项中的许多属性与当前进程是相同的。新进程几乎与原进程一模一样,执行的代码也完全相同,但新进程有自己的数据空间、环境和文件描述符。
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); }
运行效果如下:
二、替换进程映像
1、exec系列函数介绍
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); }
运行效果:
三、启动新进程
1、system函数实现
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); }
运行效果:
2、fork和exec系列函数实现
一般来说,使用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); }
运行效果:
相关文章推荐
- 启动新进程(fork和exec系列函数实现)
- 多进程函数系列fork(), wait(), exec()系列,system(), posix_spawn()实例详解
- linux下通过调用fork函数实现多进程
- Linux下的多进程编程(转) - fork(),exec()等函数的使用[zz]
- system()、exec()、fork()三个与进程有关的函数的比较
- linux的子进程调用exec( )系列函数
- Linux下的多进程编程——fork(),exec()等函数的使用
- linux系统编程之进程(三):exec系列函数和system函数
- exec系列函数的实现
- vfork()函数和fork()函数实现子进程的异同
- Linux进程的创建函数fork()及其fork内核实现解析【转】
- linux系统编程之进程(五):exec系列函数(execl,execlp,execle,execv,execvp)使用
- linux系统编程之进程(三):exec系列函数和system函数
- Linux基础学习系列:对于fork()函数的学习,及进程创建相关知识
- linux系统编程之进程(三):exec系列函数
- Linux基础学习系列:对于fork()函数的学习,及进程创建相关知识
- linux进程编程-替换进程映-exec系列函数(execl,execlp,execle,execv,execvp)使用
- fork 和 exec 对子进程继承父进程处理信号处理函数的影响
- linux系统编程之进程(五):exec系列函数(execl,execlp,execle,execv,execvp)使用
- 【经典转载】Linux进程学习系列之六 进程控制函数之exec()函数的学习