进程创建system、exec、fork
2017-05-01 16:51
344 查看
进程是操作系统进行资源分配和调度的基本单位。
进程号: 进程号可以通过getpid获取, 父进程号可以通过getppid获取。
其运行结果与sh -c “cat system1.c”类似。
函数原型如下:
前三个函数参数是可变的, 后面三个函数接收字符串数组。
全局变量environ用来把一个值传递到新的程序环境中。
编译执行后,看不到printf输出Done, 原因就是最初的进程被替换了。
父进程中fork调用返回的是新的子进程的pid, 子进程中的fork调用返回的是0。可以利用这个来判断父子进程。 fork失败将返回-1。
想要弄明白fork函数, 只需要弄懂下面几个程序即可,具体解释可以看下面几个相关博客
程序1:
输出:
程序2:
输出:
程序3:
输出:
从一道面试题谈linux下fork的运行机制
一个FORK的面试题
多线程与fork
论fork()函数与Linux中的多线程编程
进程号: 进程号可以通过getpid获取, 父进程号可以通过getppid获取。
通过system库函数创建进程
system的原型如下, 其作用是运行以字符串参数的形式传递给它的命令并等待该命令的完成。其作用等价于在shell中执行命令sh -c string。#include <stdlib.h> int system(const char *string);
#include <stdlib.h> #include <stdio.h> int main() { printf("running cat with system.\n"); system("cat system1.c"); printf("-----------\nDone.\n"); return 0; }
其运行结果与sh -c “cat system1.c”类似。
通过exec系列函数创建进程
exec函数可以把当前进程替换为一个新的进程, 新的进程由path或则file参数指定。 切换到新的进程以后,原来的进程就不再运行了。函数原型如下:
#include <unistd.h> char **environ; int execl(const char *path, const char *arg0, ..., (char*)0); int execlp(const char *file, const char *arg0, ..., (char*)0); int execle(const char *path, const char *arg0, ..., (char*)0, char *const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execve(const char *file, char *const argv[], char *const envp[]);
前三个函数参数是可变的, 后面三个函数接收字符串数组。
全局变量environ用来把一个值传递到新的程序环境中。
#include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { printf("Running ps with execlp.\n"); execlp("ps", "ps", "ax", 0); printf("Done.\n"); return 0; }
编译执行后,看不到printf输出Done, 原因就是最初的进程被替换了。
通过fork创建进程
fork函数复制当前线程, 父进程的内存数据会原封不动的拷贝到子进程中, 在进程表中创建一个新的表项, 新表项中的许多属性与当前进程是相同的, 但是新进程有自己的数据空间,环境和文件描述符。#include <sys/types.h> #include <unistd.h> pid_t fork(void);
父进程中fork调用返回的是新的子进程的pid, 子进程中的fork调用返回的是0。可以利用这个来判断父子进程。 fork失败将返回-1。
想要弄明白fork函数, 只需要弄懂下面几个程序即可,具体解释可以看下面几个相关博客
程序1:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t pid1; pid_t pid2; pid1 = fork(); pid2 = fork(); printf("pid1: %d, pid2: %d\n", pid1, pid2); return 0; }
输出:
$ ./test_fork pid1: 18585, pid2: 18586 pid1: 18585, pid2: 0 pid1: 0, pid2: 18587 pid1: 0, pid2: 0
程序2:
#include <sys/types.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { int res = fork() && fork() || fork(); printf("%d 's result is %d.\n", getpid(), res); return 0; }
输出:
$ ./test_fork2 18645 's result is 1. 18647 's result is 1. 18648 's result is 0. 18646 's result is 1. 18649 's result is 0.
程序3:
#include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { int i; for (i = 0; i < 2; i++) { fork(); printf("-"); } wait(NULL); wait(NULL); return 0; }
输出:
$ ./test_fork3 --------
相关博客
一个fork的面试题从一道面试题谈linux下fork的运行机制
一个FORK的面试题
多线程与fork
论fork()函数与Linux中的多线程编程
相关文章推荐
- Linux下进程的创建(system(); fork(); exec*())
- Linux c进程管理—创建进程 system、execl、execlp、fork
- GDB调试fork+exec创建的子进程的方法
- Linux c进程管理—创建进程 system、execl、execlp、fork
- linux学习---进程控制(fork,vfork,popen,exec,system)
- GDB调试fork+exec创建的子进程的方法
- 创建进程:fork/exec
- Linux 进程控制--- 编程 --- getpid --- fork --- exec --- system --- wait
- GDB调试fork+exec创建的子进程的方法
- LINUX进程控制(system(),fork(),exec())
- Linux 进程创建一(system和fork)
- Linux进程创建:fork,vfork,exec,clone总结
- 多进程函数系列fork(), wait(), exec()系列,system(), posix_spawn()实例详解
- 进程产生的三种方式:fork、system和exec
- 关于fork&exec之进程的创建和可执行程序的加载过程
- 进程控制:system, exec* and fork
- system()、exec()、fork()三个与进程有关的函数的比较
- linux进程创建与守护;exec 进程内核页表建立
- 进程的创建 —— do_fork()函数详解
- Linux基础学习系列:对于fork()函数的学习,及进程创建相关知识