Linux多进程编程
Linux多进程
基本函数
fork()函数
用于创建一个进程,所创建的进程复制父进程的代码段/数据段/BSS段/堆/栈等所有用户空间信息;在内核中操作系统重新为其申请了一个PCB,并使用父进程的PCB进行初始化。
1 | pid_t fork(); // pid_t实际上时int类型 |
fork
时可以看作==父进程在fork执行的位置进行了一次分裂,分裂出一个子进程==。父进程从fork
的返回值得到子进程的pid
,子进程在fork
位置得到返回值0,并且从fork的下一句开始执行。
- 使用实例:
1 | int main() |
getpid()
和gitppid()
gitpid()
: 获取当前进程的pid
值gitppid()
: 获取当前进程的父进程的pid
值
进程间通信
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.