「译文」深入解析 Go 的信号处理---gsignal

本文详细探讨了Go语言中信号处理机制,包括如何使用channel监听信号、信号处理流程及内部实现原理。通过gsignal机制,Go程序能高效响应外部信号,如中断或终端大小调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文字数:720 字

精读时间:5 分钟

也可在 2 分钟内完成速读

原文地址:https://medium.com/a-journey-with-go/go-gsignal-master-of-signals-329f7ff39391

本文基于 Go 1.13 分析

signal提供信号处理机制,并允许我们的 Go 程序与传入的信号进行交互。让我们先从监听者开始,然后再深入了解内部。

订阅

信号的订阅是通过 channel 完成的。这是一个监听任何中断信号或调整终端大小的程序示例

每个os.Signal channel 都会收听自己相应的事件集。这是上一个示例里面带有订阅工作流程的图:

Go 还为通道提供了停止通知的功能Stop(os.Signal)-或忽略信号的功能Ignore(...os.Signal)。这是这两个功能的简短示例:

该程序不能被CTRL+C 中断并且永远不会停止,因为在第二次从该通道接收之前,该通道已停止监听用于终端调整大小的信号。现在让我们看一下处理传入信号的listenerprocess是如何构建的。

gsignal

在初始化阶段,signal产生一个goroutine,该 goroutine 循环运行并充当处理信号的使用者。此循环将一直休眠直到得到通知。这是第一步:


然后,当信号到达程序时,信号处理程序将其委托给称为gsignal的特殊 goroutine。此 goroutine 是使用固定的且无法增长的较大堆栈(32k,以满足不同操作系统的要求)创建的。每个线程(用 M 表示)都有一个内部gsignal goroutine 来处理信号。这是更新的图:

gsignal 分析信号以检查其是否可处理,并唤醒睡眠的goroutine并将信号发送到队列:

SIGBUSSIGFPE之类的同步信号无法管理,将转换为 panic

然后,此循环 goroutine 可以对其进行处理。它首先查找已预订此事件的 channel,并将信号推送给他们:

循环过程信号的 goroutine 可以通过以下工具在 trace 中可视化分析: go tool trace

锁定或阻塞gsignal会使信号处理陷入困境。由于其固定大小,它也无法分配内存。这就是为什么在信号处理链中具有两个独立的 goroutine 的重要性:一个在信号到达时立即将它们排队,另一个在同一队列中循环处理它们。

现在,我们可以使用新组件来更新第一部分的插图:

本文来源于 Golang China 论坛精华文章,请点击“阅读原文”跳转至论坛浏览。

fieldMinMax minMax1 write: min(mag(U)) = 0 in cell 10374 at location (-3.4429682 -0.090098124 0.23775803) on processor 0 max(mag(U)) = 1.0090687e+10 in cell 92364 at location (-3.4499167 0.042261877 0.31423332) on processor 0 min(p) = -8.5433438e+18 in cell 176645 at location (-3.4510738 0.042347136 0.30694779) on processor 1 max(p) = 1.3762896e+18 in cell 109725 at location (-3.4612187 0.038585752 0.098657823) on processor 0 Courant Number mean: 350.32697 max: 5.2802636e+09 Interface Courant Number mean: 4.9352854 max: 1.2251106e+09 deltaT = 6.4082859e-12 Time = 0.37330708 PIMPLE: iteration 1 forces forces: rho: rho Not including porosity effects forces forces: rho: rho Not including porosity effects forces forces: rho: rho Not including porosity effects forces forces: rho: rho Not including porosity effects forces forces: rho: rho Not including porosity effects forces forces: rho: rho Not including porosity effects Rigid-body motion of the hull Centre of rotation: (0.16349067 -1.7917345e-05 0.22340704) Orientation: (0.99867444 0 -0.051471928 0 1 0 0.051471928 0 0.99867444) Linear velocity: (22700.721 -13375.19 -34361.595) Angular velocity: (0 -36697.788 0) Rigid-body motion of the propeller Centre of rotation: (-3.4251935 -6.1917345e-05 0.20539365) Orientation: (0.99867444 0 -0.051471928 -0.011922237 0.97280491 -0.23131896 0.050072145 0.23162599 0.9715154) Linear velocity: (23361.772 -13375.19 -166058.37) Angular velocity: (5.7306863e+08 -36697.788 -29536099) Number of layers : 6 Average volumetric ratio : 6(5.4603137 2.4267546 1.2332875 0.92958303 0.81943721 0.79304185) Number of holes cells : 6(23816 23804 332694 329820 328427 313353) Using layer : 3 Overset analysis : nCells : 1045876 calculated : 620238 interpolated : 39630 (interpolated from local:1769 mixed local/remote:325 remote:37536 special:0) hole : 383986 special : 0 porous : 2022 Execution time for mesh.update() = 11.5 s smoothSolver: Solving for alpha.water, Initial residual = 7.5117029e-05, Final residual = 6.8554831e-13, No Iterations 36 Phase-1 volume fraction = -1.5105804 Min(alpha.water) = -1.1396338e+14 Max(alpha.water) = 1.3228164e+14 MULES: Correcting alpha.water MULES: Correcting alpha.water Phase-1 volume fraction = -4.7432515e+18 Min(alpha.water) = -7.3767414e+29 Max(alpha.water) = 1.2621233e+30 smoothSolver: Solving for alpha.water, Initial residual = 0.00019340757, Final residual = 8.5262051e-13, No Iterations 41 Phase-1 volume fraction = -1.0268587e+19 Min(alpha.water) = -4.4204431e+29 Max(alpha.water) = 1.181677e+30 MULES: Correcting alpha.water MULES: Correcting alpha.water Phase-1 volume fraction = 5.5765537e+72 Min(alpha.water) = -1.1502567e+92 Max(alpha.water) = 5.3062382e+92 velocityDampingConstraint damp damped 654786 (62.61%) of cells, with max limit 14 DILUPBiCGStab: Solving for p_rgh, Initial residual = 1, Final residual = 2.8577882e+37, No Iterations 1000 time step continuity errors : sum local = 1.6921523e+81, global = 1.4543152e+81, cumulative = 1.4543152e+81 [0] [stack trace] [0] ============= [0] #[1] [stack trace] [1] ============= [1] #1 1 Foam::sigFpe::sigHandler(int)Foam::sigFpe::sigHandler(int) in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so [1] #2 in ?~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so [0] #2 ? in /lib/x86_64-linux-gnu/libc.so.6 in /lib/x86_64-linux-gnu/libc.so.6 [0] #3 Foam::PBiCGStab::scalarSolve(Foam::Field<double>&, Foam::Field<double> const&, unsigned char) const[1] #3 Foam::PBiCGStab::scalarSolve(Foam::Field<double>&, Foam::Field<double> const&, unsigned char) const in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so [1] #4 Foam::PBiCGStab::solve(Foam::Field<double>&, Foam::Field<double> const&, unsigned char) const in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so [0] #4 Foam::PBiCGStab::solve(Foam::Field<double>&, Foam::Field<double> const&, unsigned char) const in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so [1] #5 Foam::fvMatrix<double>::solveSegregated(Foam::dictionary const&)[0] #5 Foam::fvMatrix<double>::solveSegregated(Foam::dictionary const&) in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so [1] #6 Foam::fvMatrix<double>::solveSegregatedOrCoupled(Foam::dictionary const&) in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so [0] #6 Foam::fvMatrix<double>::solveSegregatedOrCoupled(Foam::dictionary const&) in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so [1] #7 Foam::fvMesh::solve(Foam::fvMatrix<double>&, Foam::dictionary const&) const in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so [0] #7 Foam::fvMesh::solve(Foam::fvMatrix<double>&, Foam::dictionary const&) const in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so [0] #8 in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so Foam::SolverPerformance<double> Foam::oversetFvMeshBase::solveOverset<double>(Foam::fvMatrix<double>&, Foam::dictionary const&) const[1] #8 Foam::SolverPerformance<double> Foam::oversetFvMeshBase::solveOverset<double>(Foam::fvMatrix<double>&, Foam::dictionary const&) const in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/liboverset.so [1] #9 Foam::dynamicOversetFvMesh::solve(Foam::fvMatrix<double>&, Foam::dictionary const&) const in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/liboverset.so [0] #9 Foam::dynamicOversetFvMesh::solve(Foam::fvMatrix<double>&, Foam::dictionary const&) const in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/liboverset.so [1] #10 in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/liboverset.so [0] #10 ?? in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/bin/overInterDyMFoam [1] #11 ? in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/bin/overInterDyMFoam [0] #11 ? in /lib/x86_64-linux-gnu/libc.so.6 [1] #12 __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6 [0] #12 __libc_start_main in /lib/x86_64-linux-gnu/libc.so.6 [1] #13 in /lib/x86_64-linux-gnu/libc.so.6 [0] #13 ?? in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/bin/overInterDyMFoam [0] ============= [ecs-lj-openfoam:1114988] *** Process received signal *** [ecs-lj-openfoam:1114988] Signal: Floating point exception (8) [ecs-lj-openfoam:1114988] Signal code: (-6) [ecs-lj-openfoam:1114988] Failing at address: 0x3ea0011036c [ecs-lj-openfoam:1114988] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x45330)[0x70e8b2c45330] [ecs-lj-openfoam:1114988] [ 1] /lib/x86_64-linux-gnu/libc.so.6(pthread_kill+0x11c)[0x70e8b2c9eb2c] [ecs-lj-openfoam:1114988] [ 2] /lib/x86_64-linux-gnu/libc.so.6(gsignal+0x1e)[0x70e8b2c4527e] [ecs-lj-openfoam:1114988] [ 3] /lib/x86_64-linux-gnu/libc.so.6(+0x45330)[0x70e8b2c45330] [ecs-lj-openfoam:1114988] [ 4] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZNK4Foam9PBiCGStab11scalarSolveERNS_5FieldIdEERKS2_h+0xc88)[0x70e8b3bc04e8] [ecs-lj-openfoam:1114988] [ 5] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZNK4Foam9PBiCGStab5solveERNS_5FieldIdEERKS2_h+0x3f)[0x70e8b3bc1a4f] [ecs-lj-openfoam:1114988] [ 6] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so(_ZN4Foam8fvMatrixIdE15solveSegregatedERKNS_10dictionaryE+0x52c)[0x70e8b78b6cdc] [ecs-lj-openfoam:1114988] [ 7] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so(_ZN4Foam8fvMatrixIdE24solveSegregatedOrCoupledERKNS_10dictionaryE+0x4ab)[0x70e8b6c5d97b] [ecs-lj-openfoam:1114988] [ 8] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so(_ZNK4Foam6fvMesh5solveERNS_8fvMatrixIdEERKNS_10dictionaryE+0x27)[0x70e8b6beac97] [ecs-lj-openfoam:1114988] [ 9] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/liboverset.so(_ZNK4Foam17oversetFvMeshBase12solveOversetIdEENS_17SolverPerformanceIT_EERNS_8fvMatrixIS3_EERKNS_10dictionaryE+0x396)[0x70e8b457ca76] [ecs-lj-openfoam:1114988] [10] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/liboverset.so(_ZNK4Foam20dynamicOversetFvMesh5solveERNS_8fvMatrixIdEERKNS_10dictionaryE+0x28)[0x70e8b457d5e8] [ecs-lj-openfoam:1114988] [11] overInterDyMFoam(+0x5323c)[0x558e3d82023c] [ecs-lj-openfoam:1114988] [12] /lib/x86_64-linux-gnu/libc.so.6(+0x2a1ca)[0x70e8b2c2a1ca] [ecs-lj-openfoam:1114988] [13] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x8b)[0x70e8b2c2a28b] [ecs-lj-openfoam:1114988] [14] overInterDyMFoam(+0x59875)[0x558e3d826875] [ecs-lj-openfoam:1114988] *** End of error message *** in ~/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/bin/overInterDyMFoam [1] ============= [ecs-lj-openfoam:1114989] *** Process received signal *** [ecs-lj-openfoam:1114989] Signal: Floating point exception (8) [ecs-lj-openfoam:1114989] Signal code: (-6) [ecs-lj-openfoam:1114989] Failing at address: 0x3ea0011036d [ecs-lj-openfoam:1114989] [ 0] /lib/x86_64-linux-gnu/libc.so.6(+0x45330)[0x77fb12a45330] [ecs-lj-openfoam:1114989] [ 1] /lib/x86_64-linux-gnu/libc.so.6(pthread_kill+0x11c)[0x77fb12a9eb2c] [ecs-lj-openfoam:1114989] [ 2] /lib/x86_64-linux-gnu/libc.so.6(gsignal+0x1e)[0x77fb12a4527e] [ecs-lj-openfoam:1114989] [ 3] /lib/x86_64-linux-gnu/libc.so.6(+0x45330)[0x77fb12a45330] [ecs-lj-openfoam:1114989] [ 4] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZNK4Foam9PBiCGStab11scalarSolveERNS_5FieldIdEERKS2_h+0xc88)[0x77fb139c04e8] [ecs-lj-openfoam:1114989] [ 5] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libOpenFOAM.so(_ZNK4Foam9PBiCGStab5solveERNS_5FieldIdEERKS2_h+0x3f)[0x77fb139c1a4f] [ecs-lj-openfoam:1114989] [ 6] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so(_ZN4Foam8fvMatrixIdE15solveSegregatedERKNS_10dictionaryE+0x52c)[0x77fb176b6cdc] [ecs-lj-openfoam:1114989] [ 7] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so(_ZN4Foam8fvMatrixIdE24solveSegregatedOrCoupledERKNS_10dictionaryE+0x4ab)[0x77fb16a5d97b] [ecs-lj-openfoam:1114989] [ 8] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/libfiniteVolume.so(_ZNK4Foam6fvMesh5solveERNS_8fvMatrixIdEERKNS_10dictionaryE+0x27)[0x77fb169eac97] [ecs-lj-openfoam:1114989] [ 9] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/liboverset.so(_ZNK4Foam17oversetFvMeshBase12solveOversetIdEENS_17SolverPerformanceIT_EERNS_8fvMatrixIS3_EERKNS_10dictionaryE+0x396)[0x77fb1437ca76] [ecs-lj-openfoam:1114989] [10] /home/ymtan/OpenFOAM/OpenFOAM-v2412/platforms/linux64GccDPInt32Opt/lib/liboverset.so(_ZNK4Foam20dynamicOversetFvMesh5solveERNS_8fvMatrixIdEERKNS_10dictionaryE+0x28)[0x77fb1437d5e8] [ecs-lj-openfoam:1114989] [11] overInterDyMFoam(+0x5323c)[0x644ba875423c] [ecs-lj-openfoam:1114989] [12] /lib/x86_64-linux-gnu/libc.so.6(+0x2a1ca)[0x77fb12a2a1ca] [ecs-lj-openfoam:1114989] [13] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0x8b)[0x77fb12a2a28b] [ecs-lj-openfoam:1114989] [14] overInterDyMFoam(+0x59875)[0x644ba875a875] [ecs-lj-openfoam:1114989] *** End of error message *** -------------------------------------------------------------------------- Primary job terminated normally, but 1 process returned a non-zero exit code. Per user-direction, the job has been aborted.
最新发布
05-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值