kill bgwriter 的小实验

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介:

如果 我直接 kill 掉 bgwriter 的进程,会发生什么呢?

复制代码
[root@localhost postgresql-9.2.0]# ps -ef|grep post
root      2928  2897  0 10:34 pts/1    00:00:00 su - postgres
postgres  2929  2928  0 10:34 pts/1    00:00:00 -bash
postgres  3101  2929  0 11:09 pts/1    00:00:00 ./postgres -D /usr/local/pgsql/data
postgres  3103  3101  0 11:09 ?        00:00:00 postgres: checkpointer process     
postgres  3104  3101  0 11:09 ?        00:00:00 postgres: writer process           
postgres  3105  3101  0 11:09 ?        00:00:00 postgres: wal writer process       
postgres  3106  3101  0 11:09 ?        00:00:00 postgres: autovacuum launcher process   
postgres  3107  3101  0 11:09 ?        00:00:00 postgres: stats collector process   
root      3109  2977  0 11:10 pts/2    00:00:00 grep post
[root@localhost postgresql-9.2.0]# kill 3104
[root@localhost postgresql-9.2.0]# ps -ef|grep post
root      2928  2897  0 10:34 pts/1    00:00:00 su - postgres
postgres  2929  2928  0 10:34 pts/1    00:00:00 -bash
postgres  3101  2929  0 11:09 pts/1    00:00:00 ./postgres -D /usr/local/pgsql/data
postgres  3103  3101  0 11:09 ?        00:00:00 postgres: checkpointer process     
postgres  3105  3101  0 11:09 ?        00:00:00 postgres: wal writer process       
postgres  3106  3101  0 11:09 ?        00:00:00 postgres: autovacuum launcher process   
postgres  3107  3101  0 11:09 ?        00:00:00 postgres: stats collector process   
postgres  3110  3101  0 11:10 ?        00:00:00 postgres: writer process           
root      3112  2977  0 11:10 pts/2    00:00:00 grep post
[root@localhost postgresql-9.2.0]# kill 3110
[root@localhost postgresql-9.2.0]# ps -ef|grep post
root      2928  2897  0 10:34 pts/1    00:00:00 su - postgres
postgres  2929  2928  0 10:34 pts/1    00:00:00 -bash
postgres  3101  2929  0 11:09 pts/1    00:00:00 ./postgres -D /usr/local/pgsql/data
postgres  3103  3101  0 11:09 ?        00:00:00 postgres: checkpointer process     
postgres  3105  3101  0 11:09 ?        00:00:00 postgres: wal writer process       
postgres  3106  3101  0 11:09 ?        00:00:00 postgres: autovacuum launcher process   
postgres  3107  3101  0 11:09 ?        00:00:00 postgres: stats collector process   
postgres  3114  3101  0 11:10 ?        00:00:00 postgres: writer process           
root      3116  2977  0 11:10 pts/2    00:00:00 grep post
[root@localhost postgresql-9.2.0]# 
复制代码

我删除了几次 bgwriter 的进程,都再次生成了。

那么其原因是什么呢?

这和 postmaster.c 的监控有关。来看代码吧:为简化起见,吧postmaster 与 postgres 当成一个东西。

postmaster 生成了各个子进程以后,会在一旁进行监控:

复制代码
/*                                    
 * Reaper -- signal handler to cleanup after a child process dies.                                    
 */                                    
static void                                    
reaper(SIGNAL_ARGS)                                    
{                                    
    int            save_errno = errno;                    
    int            pid;        /* process id of dead child process */            
    int            exitstatus;        /* its exit status */            
                                    
    /* These macros hide platform variations in getting child status */                                
#ifdef HAVE_WAITPID                                    
    int            status;            /* child exit status */        
                                    
#define LOOPTEST()            ((pid = waitpid(-1, &status, WNOHANG)) > 0)                        
#define LOOPHEADER()            (exitstatus = status)                        
#else                            /* !HAVE_WAITPID */        
#ifndef WIN32                                    
    union wait    status;            /* child exit status */                
                                    
#define LOOPTEST()        ((pid = wait3(&status, WNOHANG, NULL)) > 0)                            
#define LOOPHEADER()    (exitstatus = status.w_status)                                
#else                            /* WIN32 */        
#define LOOPTEST()        ((pid = win32_waitpid(&exitstatus)) > 0)                            
#define LOOPHEADER()                                    
#endif   /* WIN32 */                                    
#endif   /* HAVE_WAITPID */                                    
                                    
    PG_SETMASK(&BlockSig);                                
                                    
    ereport(DEBUG4,                                
            (errmsg_internal("reaping dead processes")));                        
                                    
    while (LOOPTEST())                                
    {                                
        LOOPHEADER();                            
                                    
        ……                            
                                    
        /*                            
         * Was it the bgwriter?  Normal exit can be ignored; we'll start a new                            
         * one at the next iteration of the postmaster's main loop, if                            
         * necessary.  Any other exit condition is treated as a crash.                            
         */                            
        if (pid == BgWriterPID)                            
        {                            
            BgWriterPID = 0;                        
            if (!EXIT_STATUS_0(exitstatus))                        
                HandleChildCrash(pid, exitstatus,                    
                         _("background writer process"));            
            continue;                        
        }                            
                                    
        ……                            
    }                                
                                    
……                                    
}                                    
复制代码

[作者:技术者高健@博客园  mail: luckyjackgao@gmail.com ]

由于我所使用的是 linux 平台,

[root@localhost postgresql-9.2.0]# find ./ -name "*.h"|xargs grep "HAVE_WAITPID"

./src/include/pg_config.h:#define HAVE_WAITPID 1
[root@localhost postgresql-9.2.0]# 
所以,循环程序可以认为是:

复制代码
while (((pid = waitpid(-1, &status, WNOHANG)) > 0))                                
{                                
    exitstatus = status;                            
                                
    ……                            
                                
    /*                            
     * Was it the bgwriter?  Normal exit can be ignored; we'll start a new                            
     * one at the next iteration of the postmaster's main loop, if                            
     * necessary.  Any other exit condition is treated as a crash.                            
     */                            
    if (pid == BgWriterPID)                            
    {                            
        BgWriterPID = 0;                        
        if (!(exitstatus==0))                        
            HandleChildCrash(pid, exitstatus,                    
                     _("background writer process"));            
        continue;                        
    }                            
                                
    ……                            
}                                
复制代码

waitpid 用于监控子进程的结束。

其参数:

pid=-1 就是 等待任何子进程,相当于 wait()。 

WNOHANG 就是  若pid指定的子进程没有结束,则waitpid()函数返回0,不予以等待。若结束,则返回该子进程的ID

而 HandleChildCrash 会完成重新建立子进程的工作。







本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/10/31/2747711.html,如需转载请自行联系原作者

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
4月前
|
安全 Linux 开发工具
linux(三十二)系统信息命令kill终止进程
linux(三十二)系统信息命令kill终止进程
39 2
|
8月前
|
Linux
Linux进程查看与控制:掌握ps、top、kill等关键命令
在Linux系统中,进程是程序执行的实例,系统中的所有任务都以进程形式运行。了解如何查看和控制进程对于系统管理和故障排除至关重要。本文将介绍Linux下常用的进程查看与控制命令,包括ps、top、kill等,帮助读者熟练使用这些命令来监视和管理系统中的进程。
415 0
|
10月前
|
Kubernetes PyTorch 算法框架/工具
pytorch任务无法kill,导致pod退出失败
问题现象pytorch进程通过k8s方式部署,由于该任务无法退出,导致所在的pod也无法退出;再次向pytorch任务发送SIGKILL信号也无法杀死。kenrel版本5.10。任务状态信息pytorch进程状态可以看到pending了一个SIGKILL信号(0x100表示第9为被置1,即SIGKILL信号),man手册中也明确了SIGKILL无法被捕获、阻塞忽略等。另外即使是两个不同的pid n
133 0
pytorch任务无法kill,导致pod退出失败
|
传感器 算法
利用PID控制算法实现恒温实验的方法
大家好。 今天和大家聊一聊PID算法的控制原理。 ​ 在讲解PID算法之前,在此抛出一个问题,如何通过算法控制加热器使水温稳定在50摄氏度?
206 1
利用PID控制算法实现恒温实验的方法
|
存储 关系型数据库 MySQL
Linux:1.进程介绍+2.Linux父子进程+3.终止进程kill和killall+4.查看进程树pstree+5.service服务管理
Linux:1.进程介绍+2.Linux父子进程+3.终止进程kill和killall+4.查看进程树pstree+5.service服务管理
148 0
Linux:1.进程介绍+2.Linux父子进程+3.终止进程kill和killall+4.查看进程树pstree+5.service服务管理
|
物联网 Linux 开发者
Kill 函数|学习笔记
快速学习 Kill 函数
324 0
|
Oracle 关系型数据库 Linux
centos批量kill进程
centos批量kill进程
220 0
|
自然语言处理 算法 Linux
Linux中的pstree 查看进程树、netstat 显示网络状态和端口占用信息、kill 终止进程、进程管理类、ps 查看当前系统进程状态、内存置换算法LRU、查看与sshd相关进程
这时候没有了sshd守护进程了,那么远程登录就都登录不上了,只有再把sshd守护进程开启才可以再次远程登录,如果没有开启sshd守护进程,把所有的root进程的远程登录关掉,那么只能用主机操作了。比如将sshd全部杀死,这样的话远程登录的全部停了,而且也不能再次远程登录了,只能在主机上打开sshd守护进程才可以,这个就不演示了。killall 进程名称 (功能描述:通过进程名称杀死进程,也支持通配符,这 在系统因负载过大而变得很慢时很有用)linux常用命令下。linux常用命令中。linux常用命令上。.netstat –nlp | grep 端口号 (功能描述:查看网络端口号占用情况)ne
340 1
Linux中的pstree 查看进程树、netstat 显示网络状态和端口占用信息、kill 终止进程、进程管理类、ps 查看当前系统进程状态、内存置换算法LRU、查看与sshd相关进程
|
Linux
12.10 Linux终止进程(kill命令)
kill 从字面来看,就是用来杀死进程的命令,但事实上,这个或多或少带有一定的误导性。从本质上讲,kill 命令只是用来向进程发送一个信号,至于这个信号是什么,是用户指定的。
377 0
12.10 Linux终止进程(kill命令)
|
Java Linux 应用服务中间件
kill -3 PID命令获取java应用堆栈信息
kill -3 PID命令获取java应用堆栈信息
932 0