/*
#include <unistd.h>
pid_t fork(void)
return value:
On success, the PID of the child process is returned in the parent,
and 0 is returned in the child. On failure, -1 is returned in the parent
, no child process is created, and errno is set appropriately.
父子进程之间的关系。
区别:
1.fork()函数的返回值不同。
2.pcb中的一些数据
当前进程的id pid
当前进程的父进程的 id pid
信号集
共同点:
某些状态下:子进程被创建,还没有进行如何写数据的操作
用户区的数据
文件描述符表
父子进程的变量:
读时共享,写时拷贝
*/
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
int main()
{
pid_t pid = fork();
if(pid == -1)
{
perror("fork");
exit(1);
}else if(pid > 0)
{
printf("我是父进程:pid = %d,ppid = %d\n",getpid(),getppid());
for(int i = 0 ;i < 10;i++)
{
printf("i=%d\n",i);
sleep(1);
}
}else if(pid==0)
{
printf("我是子进程:pid = %d,ppid = %d\n",getpid(),getppid());
for(int j = 0 ;j < 10;j++)
{
printf("j=%d\n",j);
sleep(1);
}
}
}
/*
GDB多进程调试
使用GDB调试的时候,GDB默认只能跟踪一个进程, 可以在fork函数调用之前,
通过指令设置GDB调试工具跟踪父进程或者是跟踪子进程,默认跟踪父进程。
设置调试父进程或者子进程: set follow-fork-mode [parent(默认) or child]
设置调试模式: set detach-on-fork [on - off]
默认为on,表示调试当前进程的时候,其它的进程继续运行,如果为off
调试当前进程的时候,其它进程被GDB挂起。
查看调试的进程: info inferiors
切换当前调试的程序:inferior id
使进程脱离GDB调试:detach inferior id
*/