多进程程序
- 代码
// fork 生成子进程
// 父进程调用 fork 返回的是子进程的进程ID,子进程调用该函数返回的是 0
#include<iostream>
#include<unistd.h>
#include<cstdio>
int main()
{
pid_t pid = fork();
if( 0 == pid ) {
std::cout << "hi, I'am a child process and the process id is " << getpid() << std::endl;
}
else if(pid > 0){
std::cout << "hi, I'am a parent process and the process id is " << getpid() << std::endl;
}
else{
perror("fork");
}
return 0;
}
- 调试命令
follow-fork-mode | show detach-on-fork | Description |
---|---|---|
parent | on | 只调试父进程(GDB 模式) |
child | on | 只调试子进程 |
parent | off | 同时调试两个进程, GDB 跟父进程,子进程阻塞在 fork 处 |
child | off | 同时调试两个进程, GDB 跟子进程,父进程阻塞在 fork 处 |