从SecureCRT无法执行Ctrl + C开始发散
问题:
1. 工作电脑windows使用SecureCRT连接开发机,Ctrl + C 无法使用,习惯性使用Ctrl + Z,表面停止输出
2. 每次Ctrl + Z再次执行相同gradlew,就会说已有在进程在运行
解决:
1. SecureCRT Ctrl + C 无法使用: 与复制热键冲突,会话选项中剔除CUA设置中的勾
2.方法1: 使用 Ctrl + C 终止线程
方法2:使用Ctrl + Z后去kill
#查看后台工作,-l选项用于显示PID jobs -l #输出示例 [1]- 10314 Stopped vim ~/.bashrc [2]+ 10833 Stopped find / -print #杀进程,-15为正常结束 kill -15 %1 or kill -15 10833
解析1:Ctrl+Z/Ctrl+C的区别(参考:stack)
Ctrl+Z: 发送信号SIGSTOP,用于挂起线程;如上显示的STOPPED状态,可切换到前台fg/切换到后台bg/杀掉kill。
Ctrl+C:发送信号SIGINT,用于杀线程;
说到这里,和理论学习的总算对上了,这就是suspend和kill
解析2:SIGSTOP/SIGINT信号机制
计算机系统层面/Linux层面,都存在线程/进程间通信机制,其中又都包含“信号机制”。
这是一种异步通信机制,某个线程/进程收到信号后对其做出响应。同时也有人将这与硬件层面处理器的中断机制类比。
信号来源:
1. 硬件层:如此处的各种快捷键;
2. 软件来源:系统函数提供信号相关函数,如sigqueue() 信号发送函数;
信号种类:
支持的信号种类都是在国际标准中指定的,逐步扩展。早期设计机制存在丢失信号的可能,被称为“不可靠信号”,之后扩展支持的信号值就对应称为“可靠信号”。
常用的是POSIX信号量规范。
#信号示例 SIGINT #The SIGINT signal is sent to a process by its controlling terminal when a user wishes to interrupt the process SIGKILL #The SIGKILL signal is sent to a process to cause it to terminate immediately (kill) SIGSTOP #The SIGSTOP signal instructs the operating system to stop a process for later resumption.
参考:POSIX signal
信号响应:
进程/线程收到信号后对此做出响应,一般有以下三种。
1. ignore,即不做任何处理,但SIGKILL及SIGSTOP信号是不能忽略的;
2. 执行缺省操作,Linux中定义了不同信号的默认处理动作(wiki中截图部分如下);
3. 捕捉信号,自定义信号处理函数。
Signal | Portable number | Default Action | Description |
---|---|---|---|
SIGABRT | 6 | Terminate (core dump) | Process abort signal |
SIGALRM | 14 | Terminate | Alarm clock |
SIGBUS | n/a | Terminate (core dump) | Access to an undefined portion of a memory object. |
SIGCHLD | n/a | Ignore | Child process terminated, stopped, or continued. |
SIGCONT | n/a | Continue | Continue executing, if stopped. |
SIGFPE | n/a | Terminate (core dump) | Erroneous arithmetic operation. |
SIGHUP | 1 | Terminate | Hangup. |
SIGILL | n/a | Terminate (core dump) | Illegal instruction. |
SIGINT | 2 | Terminate | Terminal interrupt signal. |
SIGKILL | 9 | Terminate | Kill (cannot be caught or ignored). |
SIGPIPE | n/a | Terminate | Write on a pipe with no one to read it. |
SIGPOLL | n/a | Terminate | Pollable event. |
SIGPROF | n/a | Terminate | Profiling timer expired. |
SIGQUIT | 3 | Terminate (core dump) | Terminal quit signal. |
SIGSEGV | n/a | Terminate (core dump) | Invalid memory reference. |
SIGSTOP | n/a | Stop | Stop executing (cannot be caught or ignored). |
SIGSYS | n/a | Terminate (core dump) | Bad system call. |
SIGTERM | 15 | Terminate | Termination signal. |
SIGTRAP | n/a | Terminate (core dump) | Trace/breakpoint trap. |
SIGTSTP | n/a | Stop | Terminal stop signal. |
SIGTTIN | n/a | Stop | Background process attempting read. |
SIGTTOU | n/a | Stop | Background process attempting write. |
SIGUSR1 | n/a | Terminate | User-defined signal 1. |
SIGUSR2 | n/a | Terminate | User-defined signal 2. |
SIGURG | n/a | Ignore | High bandwidth data is available at a socket. |
SIGVTALRM | n/a | Terminate | Virtual timer expired. |
SIGXCPU | n/a | Terminate (core dump) | CPU time limit exceeded. |
SIGXFSZ | n/a | Terminate (core dump) | File size limit exceeded |
参考:http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index1.html